Implement auto-generation of ecosystem.config.js and improve container setup
- Add automatic ecosystem.config.js generation from package.json - Create app directory automatically if missing - Copy simple-website example when app directory is empty - Remove redundant default app files from configs/ - Add HAProxy support with proper real IP forwarding - Configure nginx to trust proxy headers from private networks - Simplify entrypoint logic - always use /home/$user/app This makes the container more user-friendly by eliminating the need for manual PM2 configuration and ensuring the server always has a working app. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
module.exports = {
|
||||
apps: [{
|
||||
name: 'node-app',
|
||||
script: 'index.js',
|
||||
instances: 1,
|
||||
autorestart: true,
|
||||
watch: false,
|
||||
max_memory_restart: '256M', // Restart if app uses more than 256MB
|
||||
kill_timeout: 3000,
|
||||
wait_ready: true,
|
||||
listen_timeout: 3000,
|
||||
env: {
|
||||
NODE_ENV: 'development',
|
||||
PORT: 3000,
|
||||
NODE_OPTIONS: '--max-old-space-size=200' // Limit V8 heap to 200MB
|
||||
},
|
||||
env_production: {
|
||||
NODE_ENV: 'production',
|
||||
PORT: 3000,
|
||||
NODE_OPTIONS: '--max-old-space-size=200'
|
||||
},
|
||||
log_file: '/home/myuser/logs/nodejs/app.log',
|
||||
error_file: '/home/myuser/logs/nodejs/error.log',
|
||||
out_file: '/home/myuser/logs/nodejs/out.log',
|
||||
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
|
||||
log_type: 'json',
|
||||
merge_logs: true,
|
||||
max_restarts: 5,
|
||||
min_uptime: '10s'
|
||||
}]
|
||||
};
|
101
configs/index.js
101
configs/index.js
@@ -1,101 +0,0 @@
|
||||
const express = require('express');
|
||||
const session = require('express-session');
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
// Middleware
|
||||
app.use(express.json());
|
||||
app.use(express.static('public'));
|
||||
|
||||
// Session configuration with Memcache (only in DEV mode when memcached is available)
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
try {
|
||||
const MemcachedStore = require('connect-memcached')(session);
|
||||
app.use(session({
|
||||
store: new MemcachedStore({
|
||||
hosts: ['localhost:11211']
|
||||
}),
|
||||
secret: process.env.SESSION_SECRET || 'your-secret-key-change-in-production',
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
cookie: {
|
||||
secure: false, // Allow HTTP in development
|
||||
maxAge: 24 * 60 * 60 * 1000 // 24 hours
|
||||
}
|
||||
}));
|
||||
console.log('Memcached session store initialized');
|
||||
} catch (err) {
|
||||
console.log('Memcached not available, using memory store');
|
||||
app.use(session({
|
||||
secret: process.env.SESSION_SECRET || 'your-secret-key-change-in-production',
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
cookie: {
|
||||
secure: false,
|
||||
maxAge: 24 * 60 * 60 * 1000
|
||||
}
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
// Production session configuration (expects external session store)
|
||||
app.use(session({
|
||||
secret: process.env.SESSION_SECRET || 'your-secret-key-change-in-production',
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
cookie: {
|
||||
secure: true, // HTTPS only in production
|
||||
maxAge: 24 * 60 * 60 * 1000
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
// Health check endpoint
|
||||
app.get('/ping', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
timestamp: new Date().toISOString(),
|
||||
uptime: process.uptime(),
|
||||
version: process.env.npm_package_version || '1.0.0'
|
||||
});
|
||||
});
|
||||
|
||||
// Default route
|
||||
app.get('/', (req, res) => {
|
||||
res.json({
|
||||
message: 'Cloud Node Container is running!',
|
||||
nodeVersion: process.version,
|
||||
environment: process.env.NODE_ENV || 'development',
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
});
|
||||
|
||||
// Info endpoint
|
||||
app.get('/info', (req, res) => {
|
||||
res.json({
|
||||
nodeVersion: process.version,
|
||||
platform: process.platform,
|
||||
arch: process.arch,
|
||||
uptime: process.uptime(),
|
||||
memory: process.memoryUsage(),
|
||||
env: process.env.NODE_ENV || 'development'
|
||||
});
|
||||
});
|
||||
|
||||
// Session demo endpoint
|
||||
app.get('/session', (req, res) => {
|
||||
if (!req.session.visits) {
|
||||
req.session.visits = 0;
|
||||
}
|
||||
req.session.visits++;
|
||||
|
||||
res.json({
|
||||
sessionId: req.sessionID,
|
||||
visits: req.session.visits,
|
||||
message: 'Session is working with ' + (process.env.NODE_ENV !== 'production' ? 'Memcached' : 'default store')
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running on port ${port}`);
|
||||
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
|
||||
});
|
@@ -17,6 +17,14 @@ http {
|
||||
client_max_body_size 8m;
|
||||
large_client_header_buffers 2 1k;
|
||||
|
||||
# Real IP configuration for HAProxy
|
||||
set_real_ip_from 10.0.0.0/8; # Private network range
|
||||
set_real_ip_from 172.16.0.0/12; # Private network range
|
||||
set_real_ip_from 192.168.0.0/16; # Private network range
|
||||
set_real_ip_from 127.0.0.1; # Localhost
|
||||
real_ip_header X-Forwarded-For;
|
||||
real_ip_recursive on;
|
||||
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||
|
@@ -1,27 +0,0 @@
|
||||
{
|
||||
"name": "cnoc-default-app",
|
||||
"version": "1.0.0",
|
||||
"description": "Default Node.js application for Cloud Node Container",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js",
|
||||
"dev": "nodemon index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"express-session": "^1.17.3",
|
||||
"connect-memcached": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.0.1"
|
||||
},
|
||||
"keywords": [
|
||||
"nodejs",
|
||||
"express",
|
||||
"container",
|
||||
"docker"
|
||||
],
|
||||
"author": "",
|
||||
"license": "MIT"
|
||||
}
|
Reference in New Issue
Block a user