Skip to content

Commit 9ef205a

Browse files
committed
更新增强代码稳定性
1 parent c263d84 commit 9ef205a

File tree

5 files changed

+67
-276
lines changed

5 files changed

+67
-276
lines changed

bin/www

Lines changed: 0 additions & 104 deletions
This file was deleted.

build-images/zerocat-ubuntu/Dockerfile

Lines changed: 0 additions & 115 deletions
This file was deleted.

middleware/auth.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

server.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env node
2+
3+
const { app, server } = require('../app');
4+
const config = require('../services/config');
5+
const debug = require('debug')('coderun:server');
6+
7+
// 获取端口
8+
const port = normalizePort(config.server.port);
9+
app.set('port', port);
10+
11+
// 启动服务器
12+
server.listen(port);
13+
server.on('error', onError);
14+
server.on('listening', onListening);
15+
16+
// 端口标准化
17+
function normalizePort(val) {
18+
const port = parseInt(val, 10);
19+
20+
if (isNaN(port)) {
21+
return val;
22+
}
23+
24+
if (port >= 0) {
25+
return port;
26+
}
27+
28+
return false;
29+
}
30+
31+
// 错误处理
32+
function onError(error) {
33+
if (error.syscall !== 'listen') {
34+
throw error;
35+
}
36+
37+
const bind = typeof port === 'string'
38+
? 'Pipe ' + port
39+
: 'Port ' + port;
40+
41+
switch (error.code) {
42+
case 'EACCES':
43+
console.error(bind + ' requires elevated privileges');
44+
process.exit(1);
45+
break;
46+
case 'EADDRINUSE':
47+
console.error(bind + ' is already in use');
48+
process.exit(1);
49+
break;
50+
default:
51+
throw error;
52+
}
53+
}
54+
55+
// 监听回调
56+
function onListening() {
57+
const addr = server.address();
58+
const bind = typeof addr === 'string'
59+
? 'pipe ' + addr
60+
: 'port ' + addr.port;
61+
debug('Listening on ' + bind);
62+
console.log(`[Server] 🚀 服务器启动成功,监听端口: ${addr.port}`);
63+
}

config/index.js renamed to services/config.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ const config = {
2020

2121
docker: {
2222
containerPoolSize: parseInt(process.env.CONTAINER_POOL_SIZE, 10) || 2,
23-
customImage: process.env.CUSTOM_IMAGE || 'zerocat-ubuntu:latest',
23+
customImage: process.env.CUSTOM_IMAGE || 'zerocat-coderunner:latest',
2424
},
2525

2626
logging: {
27-
level: process.env.LOG_LEVEL || 'info',
28-
format: process.env.LOG_FORMAT || 'dev',
27+
level: 'info',
28+
format: 'dev',
2929
},
3030

3131
admin: {
32-
enabled: process.env.ADMIN_ENABLED !== 'false', // 默认为true
3332
poolSize: parseInt(process.env.ADMIN_POOL_SIZE, 10) || 2,
3433
reportInterval: parseInt(process.env.ADMIN_REPORT_INTERVAL, 10) || 60000,
3534
lastConfigUpdate: new Date(),
@@ -47,29 +46,16 @@ function validateConfig() {
4746
if (config.server.env === 'production') {
4847
for (const key of requiredInProduction) {
4948
if (!process.env[key]) {
50-
throw new Error(`Missing required environment variable in production: ${key}`);
49+
throw new Error(`未找到必须的环境变量: ${key}`);
5150
}
5251
}
5352
}
5453
}
5554

56-
// 打印配置信息(隐藏敏感信息)
57-
function logConfig() {
58-
const sanitizedConfig = JSON.parse(JSON.stringify(config));
59-
// 隐藏敏感信息
60-
sanitizedConfig.site.authToken = '***';
61-
sanitizedConfig.jwt.secret = '***';
62-
63-
console.log('[Config] 📝 当前配置');
64-
//console.log(JSON.stringify(sanitizedConfig, null, 2));
65-
}
6655

6756
// 初始化配置
6857
try {
6958
validateConfig();
70-
if (config.server.env !== 'test') {
71-
logConfig();
72-
}
7359
} catch (error) {
7460
console.error('[Config] ❌ 配置错误:', error.message);
7561
process.exit(1);

0 commit comments

Comments
 (0)