-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (72 loc) · 2.32 KB
/
server.js
File metadata and controls
83 lines (72 loc) · 2.32 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
#!/usr/bin/env node
import { createServer } from 'http';
import worker from './index.js';
const PORT = process.env.PORT || 3000;
// Mock environment from .env
const env = {
TOKEN_POOL_SIZE: process.env.TOKEN_POOL_SIZE || '5',
API_KEY: process.env.API_KEY || 'sk-z2api-key-2024',
SHOW_THINK_TAGS: process.env.SHOW_THINK_TAGS || 'true'
};
const server = createServer(async (req, res) => {
try {
// Convert Node.js request to Cloudflare Workers Request
const url = `http://${req.headers.host}${req.url}`;
let body = null;
if (req.method !== 'GET' && req.method !== 'HEAD') {
const chunks = [];
for await (const chunk of req) {
chunks.push(chunk);
}
body = Buffer.concat(chunks).toString();
}
const request = new Request(url, {
method: req.method,
headers: req.headers,
body: body
});
// Call worker
const response = await worker.fetch(request, env);
// Convert Cloudflare Workers Response to Node.js response
res.statusCode = response.status;
for (const [key, value] of response.headers) {
res.setHeader(key, value);
}
if (response.body) {
const reader = response.body.getReader();
const pump = async () => {
const { done, value } = await reader.read();
if (done) {
res.end();
return;
}
res.write(value);
pump();
};
pump();
} else {
res.end();
}
} catch (error) {
console.error('Server error:', error);
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: 'Internal server error' }));
}
});
server.listen(PORT, () => {
console.log(`🚀 Z.AI Proxy Server running on http://localhost:${PORT}`);
console.log(`📋 Available endpoints:`);
console.log(` GET /health - Health check`);
console.log(` GET /v1/models - List models`);
console.log(` POST /v1/chat/completions - Chat completions`);
console.log(`🔑 API Key: ${env.API_KEY}`);
console.log(`🎯 Token pool size: ${env.TOKEN_POOL_SIZE}`);
console.log(`🔄 Load balancing: Enabled for token pool`);
});
process.on('SIGINT', () => {
console.log('\n👋 Shutting down server...');
server.close(() => {
process.exit(0);
});
});