Babypotaterr commited on
Commit
cc2f511
1 Parent(s): 5c5cfe8

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +33 -0
server.js ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ const express = require('express');
3
+ const proxy = require('express-http-proxy');
4
+ const app = express();
5
+ const targetUrl = 'https://api.openai.com';
6
+ const openaiKey = process.env.OPENAI_KEY
7
+ const port = 7860;
8
+ const baseUrl = getExternalUrl(process.env.SPACE_ID);
9
+
10
+ app.use('/api', proxy(targetUrl, {
11
+ proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
12
+ // Modify the request headers if necessary
13
+ proxyReqOpts.headers['Authorization'] = 'Bearer '+openaiKey;
14
+ return proxyReqOpts;
15
+ },
16
+ }));
17
+
18
+ app.get("/", (req, res) => {
19
+ res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
20
+ });
21
+
22
+ function getExternalUrl(spaceId) {
23
+ try {
24
+ const [username, spacename] = spaceId.split("/");
25
+ return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
26
+ } catch (e) {
27
+ return "";
28
+ }
29
+ }
30
+
31
+ app.listen(port, () => {
32
+ console.log(`Reverse proxy server running on ${baseUrl}`);
33
+ });