ardasen commited on
Commit
9b90c04
·
1 Parent(s): cabb076

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +28 -2
server.js CHANGED
@@ -2,7 +2,7 @@ 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;
@@ -10,8 +10,34 @@ const baseUrl = getExternalUrl(process.env.SPACE_ID);
10
 
11
  app.use(bodyParser.json({ limit: '50mb' }));
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- app.use('/api', proxy(targetUrl, {
15
  proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
16
  // Modify the request headers if necessary
17
 
 
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;
 
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
+ app.use('/api', authenticateProxyKeyAndModel, proxy(targetUrl, {
41
  proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
42
  // Modify the request headers if necessary
43