Update server.js
Browse files
server.js
CHANGED
|
@@ -1,23 +1,27 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const proxy = require('express-http-proxy');
|
| 3 |
-
const app = express();
|
| 4 |
const FormData = require('form-data');
|
| 5 |
-
const
|
| 6 |
const fs = require('fs');
|
| 7 |
|
|
|
|
| 8 |
const targetUrl = 'https://api.openai.com';
|
| 9 |
const openaiKey = process.env.OPENAI_KEY;
|
| 10 |
const proxyKey = process.env.PROXY_KEY; // Your secret proxy key
|
| 11 |
const port = 7860;
|
| 12 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
| 13 |
|
|
|
|
|
|
|
| 14 |
// Middleware to authenticate requests with the proxy key
|
| 15 |
function authenticateProxyKey(req, res, next) {
|
| 16 |
-
const providedKey = req.headers['auro'];
|
| 17 |
|
| 18 |
if (providedKey && providedKey === proxyKey) {
|
|
|
|
| 19 |
next();
|
| 20 |
} else {
|
|
|
|
| 21 |
res.status(401).json({ error: 'Unauthorized' });
|
| 22 |
}
|
| 23 |
}
|
|
@@ -55,40 +59,49 @@ app.post('/api/proxy', async (req, res) => {
|
|
| 55 |
form.append('model', 'whisper-1');
|
| 56 |
form.append('response_format', 'text');
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
try {
|
| 59 |
-
const
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
}
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
});
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
res.status(200).json({ transcription: text });
|
| 71 |
-
} else {
|
| 72 |
-
const errorText = await response.text();
|
| 73 |
-
res.status(response.status).json({ error: errorText });
|
| 74 |
-
}
|
| 75 |
} catch (error) {
|
|
|
|
| 76 |
res.status(500).json({ error: 'Internal Server Error' });
|
| 77 |
}
|
| 78 |
});
|
| 79 |
|
| 80 |
-
app.use('/api', authenticateProxyKey, proxy(targetUrl, {
|
| 81 |
-
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
| 82 |
-
// Modify the request headers if necessary
|
| 83 |
-
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
| 84 |
-
return proxyReqOpts;
|
| 85 |
-
},
|
| 86 |
-
}));
|
| 87 |
-
|
| 88 |
-
app.get("/", (req, res) => {
|
| 89 |
-
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
|
| 90 |
-
});
|
| 91 |
-
|
| 92 |
function getExternalUrl(spaceId) {
|
| 93 |
try {
|
| 94 |
const [username, spacename] = spaceId.split("/");
|
|
@@ -98,6 +111,10 @@ function getExternalUrl(spaceId) {
|
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
app.listen(port, () => {
|
| 102 |
console.log(`Reverse proxy server running on ${baseUrl}`);
|
| 103 |
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const proxy = require('express-http-proxy');
|
|
|
|
| 3 |
const FormData = require('form-data');
|
| 4 |
+
const https = require('https'); // Use the built-in 'https' module
|
| 5 |
const fs = require('fs');
|
| 6 |
|
| 7 |
+
const app = express();
|
| 8 |
const targetUrl = 'https://api.openai.com';
|
| 9 |
const openaiKey = process.env.OPENAI_KEY;
|
| 10 |
const proxyKey = process.env.PROXY_KEY; // Your secret proxy key
|
| 11 |
const port = 7860;
|
| 12 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
| 13 |
|
| 14 |
+
app.use(express.json({ limit: '50mb' })); // Adjust the limit as needed
|
| 15 |
+
|
| 16 |
// Middleware to authenticate requests with the proxy key
|
| 17 |
function authenticateProxyKey(req, res, next) {
|
| 18 |
+
const providedKey = req.headers['auro']; // Assuming the key is sent in the 'x-proxy-key' header
|
| 19 |
|
| 20 |
if (providedKey && providedKey === proxyKey) {
|
| 21 |
+
// If the provided key matches the expected key, allow the request to proceed
|
| 22 |
next();
|
| 23 |
} else {
|
| 24 |
+
// If the key is missing or incorrect, reject the request with an error response
|
| 25 |
res.status(401).json({ error: 'Unauthorized' });
|
| 26 |
}
|
| 27 |
}
|
|
|
|
| 59 |
form.append('model', 'whisper-1');
|
| 60 |
form.append('response_format', 'text');
|
| 61 |
|
| 62 |
+
// Configure the request options
|
| 63 |
+
const requestOptions = {
|
| 64 |
+
method: 'POST',
|
| 65 |
+
headers,
|
| 66 |
+
path: '/audio/transcriptions',
|
| 67 |
+
host: targetUrl.replace('https://', ''), // Extract the host without 'https://'
|
| 68 |
+
port: 443, // HTTPS port
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
try {
|
| 72 |
+
const request = https.request(requestOptions, (response) => {
|
| 73 |
+
let data = '';
|
| 74 |
+
|
| 75 |
+
response.on('data', (chunk) => {
|
| 76 |
+
data += chunk;
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
response.on('end', () => {
|
| 80 |
+
if (response.statusCode === 200) {
|
| 81 |
+
res.status(200).json({ transcription: data });
|
| 82 |
+
} else {
|
| 83 |
+
res.status(response.statusCode).json({ error: data });
|
| 84 |
+
}
|
| 85 |
+
});
|
| 86 |
+
});
|
| 87 |
+
|
| 88 |
+
// Pipe the form data into the request
|
| 89 |
+
form.pipe(request);
|
| 90 |
+
|
| 91 |
+
// Handle errors
|
| 92 |
+
request.on('error', (error) => {
|
| 93 |
+
console.error(error);
|
| 94 |
+
res.status(500).json({ error: 'Internal Server Error' });
|
| 95 |
});
|
| 96 |
|
| 97 |
+
// End the request
|
| 98 |
+
request.end();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
} catch (error) {
|
| 100 |
+
console.error(error);
|
| 101 |
res.status(500).json({ error: 'Internal Server Error' });
|
| 102 |
}
|
| 103 |
});
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
function getExternalUrl(spaceId) {
|
| 106 |
try {
|
| 107 |
const [username, spacename] = spaceId.split("/");
|
|
|
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
+
app.get("/", (req, res) => {
|
| 115 |
+
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
app.listen(port, () => {
|
| 119 |
console.log(`Reverse proxy server running on ${baseUrl}`);
|
| 120 |
});
|