去除代理配置,自动检测代理
This commit is contained in:
+15
-75
@@ -25,63 +25,33 @@ const logger = defaultLogger.child('Server');
|
||||
|
||||
class PixivServer {
|
||||
constructor() {
|
||||
this.app = express();
|
||||
this.server = null;
|
||||
this.backend = null;
|
||||
this.server = null; // 添加server实例引用
|
||||
this.port = 3000; // 默认端口,会在init时重新设置
|
||||
this.logLevel = process.env.LOG_LEVEL || 'info'; // 获取日志级别
|
||||
this.isVerboseMode = ['debug', 'trace'].includes(this.logLevel.toLowerCase()); // 检查是否为详细模式
|
||||
|
||||
// 保存启动时的命令行参数和环境变量
|
||||
this.startupArgs = {
|
||||
argv: [...process.argv], // 复制命令行参数
|
||||
env: {
|
||||
PORT: process.env.PORT,
|
||||
PROXY_PORT: process.env.PROXY_PORT,
|
||||
LOG_LEVEL: process.env.LOG_LEVEL,
|
||||
NODE_ENV: process.env.NODE_ENV,
|
||||
AUTO_OPEN_BROWSER: process.env.AUTO_OPEN_BROWSER,
|
||||
UV_THREADPOOL_SIZE: process.env.UV_THREADPOOL_SIZE
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务器
|
||||
*/
|
||||
async init() {
|
||||
logger.info('正在初始化 Pixiv 后端服务器...');
|
||||
this.app = express();
|
||||
this.port = parseInt(process.env.PORT) || 3000;
|
||||
this.logLevel = process.env.LOG_LEVEL || 'info';
|
||||
this.isVerboseMode = ['debug', 'trace'].includes(this.logLevel.toLowerCase());
|
||||
|
||||
// 重新设置端口(从环境变量获取)
|
||||
this.port = process.env.PORT || 3000;
|
||||
logger.info('初始化 Pixiv 服务器...');
|
||||
logger.info(`服务器端口: ${this.port}`);
|
||||
logger.info(`日志级别: ${this.logLevel}`);
|
||||
|
||||
// 如果启用了详细模式,输出调试信息
|
||||
if (this.isVerboseMode) {
|
||||
logger.info(`详细模式已启用 (日志级别: ${this.logLevel.toUpperCase()})`);
|
||||
logger.debug('环境变量:', {
|
||||
NODE_ENV: process.env.NODE_ENV,
|
||||
PORT: process.env.PORT,
|
||||
PROXY_PORT: process.env.PROXY_PORT,
|
||||
LOG_LEVEL: process.env.LOG_LEVEL
|
||||
});
|
||||
}
|
||||
|
||||
// 设置代理
|
||||
proxyConfig.setEnvironmentVariables();
|
||||
|
||||
// 初始化 Pixiv 后端
|
||||
// 初始化后端核心
|
||||
this.backend = new PixivBackend();
|
||||
await this.backend.init();
|
||||
|
||||
// 配置中间件
|
||||
// 设置中间件
|
||||
this.setupMiddleware();
|
||||
|
||||
// 配置路由
|
||||
// 设置路由
|
||||
this.setupRoutes();
|
||||
|
||||
// 配置错误处理
|
||||
this.setupErrorHandling();
|
||||
|
||||
logger.info('服务器初始化完成');
|
||||
}
|
||||
|
||||
@@ -216,41 +186,11 @@ class PixivServer {
|
||||
await this.backend.cleanup?.();
|
||||
}
|
||||
|
||||
logger.info('正在使用原始启动参数重新启动服务器...');
|
||||
|
||||
// 使用spawn重新启动进程,保持原始参数
|
||||
const { spawn } = require('child_process');
|
||||
const path = require('path');
|
||||
|
||||
// 构建启动命令
|
||||
const nodeExecutable = process.execPath;
|
||||
const startScript = path.join(__dirname, 'start.js');
|
||||
|
||||
// 获取原始命令行参数(排除node和脚本路径)
|
||||
const originalArgs = this.startupArgs.argv.slice(2);
|
||||
|
||||
logger.info('重启命令:', nodeExecutable, [startScript, ...originalArgs]);
|
||||
|
||||
// 启动新进程
|
||||
const child = spawn(nodeExecutable, [startScript, ...originalArgs], {
|
||||
detached: true,
|
||||
stdio: 'inherit',
|
||||
env: {
|
||||
...process.env,
|
||||
...this.startupArgs.env // 恢复原始环境变量
|
||||
}
|
||||
});
|
||||
|
||||
// 分离子进程
|
||||
child.unref();
|
||||
|
||||
logger.info('新进程已启动,当前进程即将退出');
|
||||
|
||||
// 延迟退出当前进程
|
||||
setTimeout(() => {
|
||||
process.exit(0);
|
||||
}, 500);
|
||||
// 重新初始化并启动
|
||||
await this.init();
|
||||
await this.start();
|
||||
|
||||
logger.info('服务器重启成功');
|
||||
return { success: true, message: '服务器重启成功' };
|
||||
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user