Spaces:
Sleeping
Sleeping
| 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}`); | |
| }); | |