Skip to content

Commit b900b11

Browse files
committed
Update index.js
1 parent 1557c86 commit b900b11

File tree

8 files changed

+121
-73
lines changed

8 files changed

+121
-73
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ FROM node:22-alpine AS release
1010

1111
WORKDIR /app
1212

13-
COPY --from=builder /app/*.js /app/
13+
COPY --from=builder /app/index.js /app/index.js
1414
COPY --from=builder /app/package.json /app/package.json
1515
COPY --from=builder /app/package-lock.json /app/package-lock.json
1616

1717
ENV NODE_ENV=production
1818

1919
RUN npm ci --ignore-scripts --omit-dev
2020

21-
CMD ["node", "app.js"]
21+
CMD ["node", "index.js"]

app.js

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

cli.js

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

config.js

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

core.js

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

index.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env node
2+
3+
import express from 'express';
4+
import { createProxyMiddleware } from 'http-proxy-middleware';
5+
6+
// ========== Default Config ==========
7+
const defaultPort = 9017;
8+
const defaultTarget = 'https://api.openai.com';
9+
10+
// ========== Core Proxy App ==========
11+
function createProxyApp(target) {
12+
const app = express();
13+
14+
app.use('/', createProxyMiddleware({
15+
target: target,
16+
changeOrigin: true,
17+
on: {
18+
proxyReq: (proxyReq, req, res) => {
19+
proxyReq.removeHeader('x-forwarded-for');
20+
proxyReq.removeHeader('x-real-ip');
21+
},
22+
proxyRes: (proxyRes, req, res) => {
23+
proxyRes.headers['Access-Control-Allow-Origin'] = '*';
24+
proxyRes.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
25+
},
26+
error: (err, req, res) => {
27+
console.error('Proxy error:', err);
28+
res.status(500).send('Proxy error');
29+
},
30+
},
31+
}));
32+
33+
return app;
34+
}
35+
36+
// ========== Main App ==========
37+
const parseArguments = () => {
38+
const args = process.argv.slice(2);
39+
const result = {};
40+
41+
args.forEach(arg => {
42+
if (arg.startsWith('--port=')) {
43+
result.port = arg.split('=')[1];
44+
} else if (arg.startsWith('--target=')) {
45+
result.target = arg.split('=')[1];
46+
}
47+
});
48+
49+
return result;
50+
};
51+
52+
const validatePort = (port) => {
53+
const portNum = parseInt(port);
54+
if (isNaN(portNum)) throw new Error(`Invalid port: ${port}`);
55+
if (portNum < 1 || portNum > 65535) throw new Error(`Port out of range: ${port}`);
56+
return portNum;
57+
};
58+
59+
const validateTarget = (target) => {
60+
try {
61+
new URL(target);
62+
return target;
63+
} catch {
64+
throw new Error(`Invalid target URL: ${target}`);
65+
}
66+
};
67+
68+
const runProxy = () => {
69+
try {
70+
const args = parseArguments();
71+
const port = args.port || process.env.PORT || defaultPort;
72+
const target = args.target || process.env.TARGET || defaultTarget;
73+
74+
const validatedPort = validatePort(port);
75+
const validatedTarget = validateTarget(target);
76+
77+
const app = createProxyApp(validatedTarget);
78+
79+
const server = app.listen(validatedPort, () => {
80+
console.log('API Proxy running:');
81+
console.log(` Local: http://localhost:${validatedPort}`);
82+
console.log(` Target: ${validatedTarget}`);
83+
console.log('\nPress Ctrl+C to stop');
84+
});
85+
86+
server.on('error', (err) => {
87+
if (err.code === 'EADDRINUSE') {
88+
console.error(`❌ Port ${validatedPort} is already in use`);
89+
} else {
90+
console.error(`❌ Server error: ${err.message}`);
91+
}
92+
process.exit(1);
93+
});
94+
95+
process.on('SIGINT', () => {
96+
console.log('\nShutting down proxy...');
97+
server.close(() => process.exit(0));
98+
});
99+
100+
} catch (error) {
101+
console.error(`❌ Error: ${error.message}`);
102+
process.exit(1);
103+
}
104+
};
105+
106+
runProxy();

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
{
22
"name": "@ai-ql/api-proxy",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"description": "OpenAI API Proxy by Docker",
5-
"main": "app.js",
5+
"main": "index.js",
66
"keywords": [],
77
"author": "",
88
"license": "Apache-2.0",
9+
"type": "module",
10+
"files": [
11+
"README.md",
12+
"index.js"
13+
],
914
"bin": {
10-
"openai-proxy": "cli.js"
15+
"openai-proxy": "index.js"
1116
},
1217
"scripts": {
13-
"start": "node app.js",
14-
"build": "shx chmod +x cli.js",
18+
"start": "node index.js",
19+
"build": "shx chmod +x index.js",
1520
"prepare": "npm run build",
1621
"update-deps": "npx npm-check-updates -u && npm install"
1722
},
@@ -23,4 +28,4 @@
2328
"devDependencies": {
2429
"shx": "^0.4.0"
2530
}
26-
}
31+
}

0 commit comments

Comments
 (0)