forked from easychen/openai-api-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (59 loc) · 2.14 KB
/
app.js
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
const express = require('express')
const path = require('path')
const fetch = require('cross-fetch')
const app = express()
var multer = require('multer');
var forms = multer({limits: { fieldSize: 10*1024*1024 }});
app.use(forms.array());
const bodyParser = require('body-parser')
app.use(bodyParser.json({limit : '50mb' }));
app.use(bodyParser.urlencoded({ extended: true }));
const fetchSSE = require('./fetchsse.js');
app.all(`*`, async (req, res) => {
const url = `https://api.openai.com${req.url}`;
// 从 header 中取得 Authorization': 'Bearer 后的 token
const token = req.headers.authorization?.split(' ')[1];
if( !token ) return res.status(403).send('Forbidden');
const openai_key = token.split(':')[0];
if( !openai_key ) return res.status(403).send('Forbidden');
const proxy_key = token.split(':')[1]||"";
if( process.env.PROXY_KEY && proxy_key !== process.env.PROXY_KEY )
return res.status(403).send('Forbidden');
const options = {
method: req.method,
timeout: process.env.TIMEOUT||5000,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Bearer '+ openai_key,
},
};
if( req.method.toLocaleLowerCase() === 'post' && req.body ) options.body = JSON.stringify(req.body);
console.log({url, options});
try {
const response = await myFetch(url, options);
console.log(response);
const data = await response.json();
console.log( data );
res.json(data);
} catch (error) {
console.error(error);
res.status(500).json({"error":error.toString()});
}
})
async function myFetch(url, options) {
const {timeout, ...fetchOptions} = options;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout||5000)
const res = await fetch(url, {...fetchOptions,signal:controller.signal});
clearTimeout(timeoutId);
return res;
}
// Error handler
app.use(function(err, req, res, next) {
console.error(err)
res.status(500).send('Internal Serverless Error')
})
const port = process.env.PORT||9000;
app.listen(port, () => {
console.log(`Server start on http://localhost:${port}`);
})