-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathtest-server-simple.js
More file actions
103 lines (86 loc) · 3.01 KB
/
test-server-simple.js
File metadata and controls
103 lines (86 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
/**
* 简单的测试服务器
* 只加载统一代理功能,用于测试
*/
import Fastify from 'fastify';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// 创建Fastify实例
const fastify = Fastify({
logger: true
});
// 添加简单的CORS头
fastify.addHook('onRequest', async (request, reply) => {
reply.header('Access-Control-Allow-Origin', '*');
reply.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, HEAD');
reply.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
});
// 加载统一代理控制器
async function loadUnifiedProxy() {
try {
const unifiedProxyPath = join(__dirname, '..', 'controllers', 'unified-proxy.js');
const fileUrl = `file:///${unifiedProxyPath.replace(/\\/g, '/')}`;
const { default: unifiedProxyController } = await import(fileUrl);
// 注册统一代理路由
await fastify.register(unifiedProxyController, {});
console.log('✅ 统一代理控制器加载成功');
return true;
} catch (error) {
console.error('❌ 统一代理控制器加载失败:', error.message);
return false;
}
}
// 添加根路径处理
fastify.get('/', async (request, reply) => {
return {
service: 'Unified Proxy Test Server',
version: '1.0.0',
status: 'running',
endpoints: [
'GET / - This info',
'GET /unified-proxy/health - Health check',
'GET /unified-proxy/status - Service status',
'GET /unified-proxy/proxy - Smart proxy'
]
};
});
// 启动服务器
async function start() {
try {
console.log('🚀 启动统一代理测试服务器...');
// 加载统一代理
const loaded = await loadUnifiedProxy();
if (!loaded) {
console.error('❌ 无法加载统一代理,服务器启动失败');
process.exit(1);
}
// 启动服务器
const port = process.env.PORT || 3001;
const host = process.env.HOST || '0.0.0.0';
await fastify.listen({ port, host });
console.log(`✅ 服务器启动成功!`);
console.log(`📍 地址: http://localhost:${port}`);
console.log(`🔗 统一代理: http://localhost:${port}/unified-proxy/status`);
console.log(`💡 使用 Ctrl+C 停止服务器`);
} catch (error) {
console.error('❌ 服务器启动失败:', error);
process.exit(1);
}
}
// 优雅关闭
process.on('SIGINT', async () => {
console.log('\n🛑 正在关闭服务器...');
try {
await fastify.close();
console.log('✅ 服务器已关闭');
process.exit(0);
} catch (error) {
console.error('❌ 关闭服务器时出错:', error);
process.exit(1);
}
});
// 启动服务器
start();