Skip to content

Commit e03a66e

Browse files
committed
Reapply "Supports local execution via npx"
This reverts commit daf6eff.
1 parent daf6eff commit e03a66e

File tree

7 files changed

+418
-351
lines changed

7 files changed

+418
-351
lines changed

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ ENV NODE_ENV=production
2929
# copy production dependencies and source code into final image
3030
FROM base AS release
3131
COPY --from=install /temp/prod/node_modules node_modules
32-
COPY --from=prerelease /usr/src/app/app.js .
33-
COPY --from=prerelease /usr/src/app/package.json .
32+
COPY --from=prerelease /usr/src/app .
3433

3534
# run the app
3635
USER bun

app.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
1-
const express = require('express')
2-
const { createProxyMiddleware } = require('http-proxy-middleware');
3-
const app = express()
4-
const port = process.env.PORT || 9017
5-
const target = process.env.TARGET || 'https://api.openai.com'
1+
const createProxyApp = require('./core');
2+
const { defaultPort, defaultTarget } = require('./config');
63

7-
app.use('/', createProxyMiddleware({
8-
target: target,
9-
changeOrigin: true,
10-
on: {
11-
proxyReq: (proxyReq, req, res) => {
12-
/* handle proxyReq */
13-
proxyReq.removeHeader('x-forwarded-for');
14-
proxyReq.removeHeader('x-real-ip');
15-
},
16-
proxyRes: (proxyRes, req, res) => {
17-
/* handle proxyRes */
18-
proxyRes.headers['Access-Control-Allow-Origin'] = '*';
19-
proxyRes.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
20-
},
21-
error: (err, req, res) => {
22-
/* handle error */
23-
},
24-
},
25-
}));
4+
const port = process.env.PORT || defaultPort;
5+
const target = process.env.TARGET || defaultTarget;
6+
7+
const app = createProxyApp(target);
268

279
app.listen(port, () => {
28-
console.log(`Proxy agent started: http://localhost:${port}`)
29-
})
10+
console.log(`Proxy agent started: http://localhost:${port}`);
11+
console.log(`Target: ${target}`);
12+
});

cli.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
const createProxyApp = require('./core');
3+
const { defaultPort, defaultTarget } = require('./config');
4+
5+
const args = process.argv.slice(2);
6+
const portArg = args.find(arg => arg.startsWith('--port='));
7+
const targetArg = args.find(arg => arg.startsWith('--target='));
8+
9+
const port = portArg ? portArg.split('=')[1] : defaultPort;
10+
const target = targetArg ? targetArg.split('=')[1] : defaultTarget;
11+
12+
const app = createProxyApp(target);
13+
14+
app.listen(port, () => {
15+
console.log(`OpenAI Proxy running:`);
16+
console.log(`Local: http://localhost:${port}`);
17+
console.log(`Target: ${target}`);
18+
console.log(`\nPress Ctrl+C to stop`);
19+
});

config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
defaultPort: 9017,
3+
defaultTarget: 'https://api.openai.com'
4+
};

core.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const express = require('express');
2+
const { createProxyMiddleware } = require('http-proxy-middleware');
3+
4+
module.exports = function createProxyApp(target) {
5+
const app = express();
6+
7+
app.use('/', createProxyMiddleware({
8+
target: target,
9+
changeOrigin: true,
10+
on: {
11+
proxyReq: (proxyReq, req, res) => {
12+
proxyReq.removeHeader('x-forwarded-for');
13+
proxyReq.removeHeader('x-real-ip');
14+
},
15+
proxyRes: (proxyRes, req, res) => {
16+
proxyRes.headers['Access-Control-Allow-Origin'] = '*';
17+
proxyRes.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
18+
},
19+
error: (err, req, res) => {
20+
console.error('Proxy error:', err);
21+
res.status(500).send('Proxy error');
22+
},
23+
},
24+
}));
25+
26+
return app;
27+
}

0 commit comments

Comments
 (0)