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:
73
scripts/generate-ecosystem-config.sh
Executable file
73
scripts/generate-ecosystem-config.sh
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Generate ecosystem.config.js from package.json
|
||||
# Usage: ./generate-ecosystem-config.sh <user> <app_path>
|
||||
|
||||
user=$1
|
||||
app_path=$2
|
||||
|
||||
if [ -z "$user" ] || [ -z "$app_path" ]; then
|
||||
echo "Usage: $0 <user> <app_path>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
package_json="$app_path/package.json"
|
||||
ecosystem_config="$app_path/ecosystem.config.js"
|
||||
|
||||
# Check if package.json exists
|
||||
if [ ! -f "$package_json" ]; then
|
||||
echo "Error: package.json not found at $package_json"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Extract values from package.json
|
||||
app_name=$(node -p "try { require('$package_json').name || 'node-app' } catch(e) { 'node-app' }")
|
||||
main_script=$(node -p "try { require('$package_json').main || 'index.js' } catch(e) { 'index.js' }")
|
||||
start_script=$(node -p "try { const scripts = require('$package_json').scripts; if (scripts && scripts.start) { scripts.start.replace(/^node\s+/, '') } else { null } } catch(e) { null }")
|
||||
|
||||
# Use start script if available, otherwise use main field
|
||||
if [ "$start_script" != "null" ] && [ -n "$start_script" ]; then
|
||||
script_file="$start_script"
|
||||
else
|
||||
script_file="$main_script"
|
||||
fi
|
||||
|
||||
# Clean up the script file name (remove any node command prefix)
|
||||
script_file=$(echo "$script_file" | sed 's/^node\s\+//')
|
||||
|
||||
# Generate ecosystem.config.js
|
||||
cat > "$ecosystem_config" << EOF
|
||||
module.exports = {
|
||||
apps: [{
|
||||
name: '${app_name}',
|
||||
script: '${script_file}',
|
||||
instances: 1,
|
||||
autorestart: true,
|
||||
watch: false,
|
||||
max_memory_restart: '256M',
|
||||
kill_timeout: 3000,
|
||||
wait_ready: true,
|
||||
listen_timeout: 3000,
|
||||
env: {
|
||||
NODE_ENV: 'development',
|
||||
PORT: 3000,
|
||||
NODE_OPTIONS: '--max-old-space-size=200'
|
||||
},
|
||||
env_production: {
|
||||
NODE_ENV: 'production',
|
||||
PORT: 3000,
|
||||
NODE_OPTIONS: '--max-old-space-size=200'
|
||||
},
|
||||
log_file: '/home/${user}/logs/nodejs/app.log',
|
||||
error_file: '/home/${user}/logs/nodejs/error.log',
|
||||
out_file: '/home/${user}/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'
|
||||
}]
|
||||
};
|
||||
EOF
|
||||
|
||||
echo "Generated ecosystem.config.js for app: $app_name (script: $script_file)"
|
Reference in New Issue
Block a user