Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const proxy = require('express-http-proxy');
|
| 3 |
+
const app = express();
|
| 4 |
+
const bodyParser = require('body-parser');
|
| 5 |
+
const targetUrl = 'https://openrouter.ai';
|
| 6 |
+
const openaiKey = process.env.OPENAI_KEY;
|
| 7 |
+
const proxyKey = process.env.PROXY_KEY; // Your secret proxy key
|
| 8 |
+
const port = 7860;
|
| 9 |
+
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
| 10 |
+
|
| 11 |
+
app.use(bodyParser.json({ limit: '50mb' }));
|
| 12 |
+
|
| 13 |
+
// Middleware to log request details
|
| 14 |
+
app.use((req, res, next) => {
|
| 15 |
+
console.log(`Incoming Request - Method: ${req.method}, URL: ${req.url}`);
|
| 16 |
+
console.log('Headers:', req.headers);
|
| 17 |
+
console.log('Body:', req.body);
|
| 18 |
+
next();
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
// Middleware to authenticate requests with the proxy key and check the model
|
| 23 |
+
function authenticateProxyKeyAndModel(req, res, next) {
|
| 24 |
+
const providedKey = req.headers['auro']; // Assuming the key is sent in the 'x-proxy-key' header
|
| 25 |
+
const requestedModel = req.body.model;
|
| 26 |
+
|
| 27 |
+
// List of allowed models
|
| 28 |
+
const allowedModels = ['gryphe/mythomist-7b', 'gryphe/mythomax-l2-13b'];
|
| 29 |
+
|
| 30 |
+
if (providedKey && providedKey === proxyKey && allowedModels.includes(requestedModel)) {
|
| 31 |
+
// If the provided key matches the expected key and the requested model is allowed, allow the request to proceed
|
| 32 |
+
next();
|
| 33 |
+
} else {
|
| 34 |
+
// If the key is missing or incorrect, or the model is not allowed, reject the request with an error response
|
| 35 |
+
res.status(401).json({ error: 'Unauthorized or invalid model' });
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
app.use('/api', authenticateProxyKeyAndModel, proxy(targetUrl, {
|
| 42 |
+
proxyReqPathResolver: (req) => '/api/v1/chat/completions',
|
| 43 |
+
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
| 44 |
+
// Modify the request headers if necessary
|
| 45 |
+
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
| 46 |
+
return proxyReqOpts;
|
| 47 |
+
},
|
| 48 |
+
}));
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
app.get("/", (req, res) => {
|
| 52 |
+
// res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
function getExternalUrl(spaceId) {
|
| 56 |
+
try {
|
| 57 |
+
const [username, spacename] = spaceId.split("/");
|
| 58 |
+
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
|
| 59 |
+
} catch (e) {
|
| 60 |
+
return "";
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
app.listen(port, () => {
|
| 65 |
+
console.log(`Reverse proxy server running on ${baseUrl}`);
|
| 66 |
+
});
|