auto-deploy-demo/server/config/index.js

88 lines
2.3 KiB
JavaScript
Raw Normal View History

const { execSync } = require('child_process');
2026-02-23 06:31:59 +00:00
const config = {
port: parseInt(process.env.PORT, 10) || 8888,
2026-02-23 06:31:59 +00:00
baseDomain: process.env.BASE_DOMAIN || '',
2026-02-23 06:31:59 +00:00
projectPortStart: parseInt(process.env.PROJECT_PORT_START, 10) || 9000,
projectPortEnd: parseInt(process.env.PROJECT_PORT_END, 10) || 9100,
projectBindAddress: process.env.PROJECT_BIND_ADDRESS || '127.0.0.1',
useNginx: process.env.USE_NGINX || 'auto',
nginxConfigDir: process.env.NGINX_CONFIG_DIR || './nginx/sites-enabled',
nginxTemplatePath: process.env.NGINX_TEMPLATE_PATH || './server/templates/nginx',
nginxReloadCmd: process.env.NGINX_RELOAD_CMD || 'nginx -s reload',
nginxTestCmd: process.env.NGINX_TEST_CMD || 'nginx -t',
nginxAvailable: null,
actualMode: null,
checkNginxMode() {
if (this.actualMode !== null) {
return this.actualMode;
}
if (this.useNginx === true || this.useNginx === 'true') {
this.nginxAvailable = true;
this.actualMode = 'nginx';
return 'nginx';
}
if (this.useNginx === false || this.useNginx === 'false') {
this.nginxAvailable = false;
this.actualMode = 'multiport';
return 'multiport';
}
if (this.useNginx === 'auto') {
this.nginxAvailable = this._testNginxAvailable();
this.actualMode = this.nginxAvailable ? 'nginx' : 'multiport';
return this.actualMode;
2026-02-23 06:31:59 +00:00
}
this.nginxAvailable = this._testNginxAvailable();
this.actualMode = this.nginxAvailable ? 'nginx' : 'multiport';
return this.actualMode;
2026-02-23 06:31:59 +00:00
},
_testNginxAvailable() {
try {
execSync('nginx -v 2>&1', { stdio: 'pipe' });
return true;
} catch (error) {
return false;
}
},
isNginxMode() {
if (this.actualMode === null) {
this.checkNginxMode();
}
return this.actualMode === 'nginx';
},
getProjectUrl(projectId, port) {
if (this.isNginxMode()) {
const baseUrl = this.getBaseUrl();
return `${baseUrl}/project/${projectId}/`;
}
const domain = this.baseDomain || 'localhost';
return `http://${domain}:${port}`;
},
2026-02-23 06:31:59 +00:00
getBaseUrl() {
if (this.baseDomain) {
return `http://${this.baseDomain}`;
}
return `http://localhost:${this.port}`;
},
2026-02-23 06:31:59 +00:00
isProduction() {
return !!this.baseDomain;
}
};
module.exports = config;