Spaces:
Sleeping
Sleeping
File size: 3,712 Bytes
26ab438 f5e7c6b 26ab438 f5e7c6b 26ab438 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
const express = require('express');
const request = require('request');
const rateLimit = require('express-rate-limit');
const path = require('path');
const bodyParser = require('body-parser');
const multer = require('multer');
const fs=require('fs');
let limiter_max=50;
const HOST='0.0.0.0';
const PORT = process.env.PORT || 7860; // Port number for the proxy server
const app = express();
// Enable CORS with various options
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
// Serve static files from the ./public directory
app.use(express.static(path.join(__dirname, 'public')));
// Rate limit configuration
const limiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: limiter_max,
message: 'Too many requests from this IP, please try again after a minute'
});
// Apply the rate limit to all requests
app.use(limiter);
// Endpoint handling all requests
app.use('/openai', (req, res) => {
// Target URL is read from the query string
let targetUrl = req.query.url;
if (!targetUrl) {
targetUrl = process.env.AI_SERVER_URL +'/openai/'+ req.path;
}
console.log(`app.use('/openai'): Proxying request to: ${targetUrl}`);
try {
// Pipe the request to the target URL and back to the client
const options = {
url: targetUrl,
headers: {
'User-Agent': 'Node.js Request Proxy',
'Authorization': `Bearer ${process.env.AI_API_KEY}`
}
};
// Append original headers to maintain the request integrity
Object.keys(req.headers).forEach(key => {
if (!['host', 'cookie'].includes(key)) {
options.headers[key] = req.headers[key];
}
});
req.pipe(request(options)).pipe(res);
} catch (error) {
console.error(`Error: ${error.message}`);
res.status(500).send('Error occurred while proxying');
}
});
//kv server
// Middleware to handle form-data
const upload = multer();
// Directory to store key-value pairs
const storeDir = path.join(__dirname, 'kvStore');
// Ensure the store directory exists
if (!fs.existsSync(storeDir)) {
fs.mkdirSync(storeDir);
}
// Helper function to get file path for a key
const getFilePath = (key) => path.join(storeDir, `${key}.json`);
// Endpoint to set key-value pairs
app.post('/kv/set', upload.none(), (req, res) => {
const key = req.body.key;
const value = req.body.value;
if (!key || !value) {
return res.status(400).json({ error: 'Missing key or value' });
}
const filePath = getFilePath(key);
fs.writeFile(filePath, value, (err) => {
if (err) {
return res.status(500).json({ error: 'Failed to save key-value pair' });
}
res.status(200).json({ message: 'Key-value pair set successfully' });
});
});
// Endpoint to get key-value pairs
app.get('/kv/get', (req, res) => {
const key = req.query.key;
if (!key) {
return res.status(400).json({ error: 'Missing key' });
}
const filePath = getFilePath(key);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
return res.status(404).json({ error: 'Key not found' });
}
return res.status(500).json({ error: 'Failed to retrieve key-value pair' });
}
res.status(200).json({ key: key, value: JSON.parse(data) });
});
});
app.listen(PORT,HOST, () => {
console.log(`CORS Proxy running on http://${HOST}:${PORT}`);
});
|