Spaces:
Runtime error
Runtime error
| require('dotenv').config(); | |
| const express = require('express'); | |
| const { createProxyMiddleware } = require('http-proxy-middleware'); | |
| const cors = require('cors'); | |
| const app = express(); | |
| const PORT = process.env.PORT || 8501; | |
| const STREAMLIT_URL = process.env.STREAMLIT_URL || 'http://localhost:8501'; | |
| // CORS configuration from environment variables | |
| const corsOptions = { | |
| origin: process.env.CORS_ORIGIN || '*', | |
| credentials: process.env.CORS_CREDENTIALS === 'true', | |
| methods: (process.env.CORS_METHODS || 'GET,POST,PUT,DELETE,OPTIONS,PATCH').split(','), | |
| allowedHeaders: (process.env.CORS_ALLOWED_HEADERS || 'Content-Type,Authorization,X-Requested-With,Accept,Origin').split(','), | |
| optionsSuccessStatus: 200 | |
| }; | |
| app.use(cors(corsOptions)); | |
| // Additional CORS headers | |
| app.use((req, res, next) => { | |
| res.header('Access-Control-Allow-Origin', '*'); | |
| res.header('Access-Control-Allow-Methods', '*'); | |
| res.header('Access-Control-Allow-Headers', '*'); | |
| res.header('X-Frame-Options', 'ALLOWALL'); | |
| if (req.method === 'OPTIONS') { | |
| return res.status(200).end(); | |
| } | |
| next(); | |
| }); | |
| // Replace this URL with your actual Streamlit app URL | |
| // Proxy configuration | |
| const proxyOptions = { | |
| target: STREAMLIT_URL, | |
| changeOrigin: true, | |
| secure: true, | |
| ws: true, // WebSocket support | |
| logLevel: 'info', | |
| onProxyReq: (proxyReq, req, res) => { | |
| console.log(`Proxying: ${req.method} ${req.url}`); | |
| // Clean up headers | |
| proxyReq.removeHeader('x-forwarded-host'); | |
| proxyReq.setHeader('host', STREAMLIT_URL.replace('https://', '')); | |
| }, | |
| onProxyRes: (proxyRes, req, res) => { | |
| console.log(`Response: ${proxyRes.statusCode}`); | |
| // Remove restrictive headers | |
| delete proxyRes.headers['x-frame-options']; | |
| delete proxyRes.headers['content-security-policy']; | |
| delete proxyRes.headers['x-content-type-options']; | |
| // Add permissive headers | |
| proxyRes.headers['x-frame-options'] = 'ALLOWALL'; | |
| proxyRes.headers['access-control-allow-origin'] = '*'; | |
| }, | |
| onError: (err, req, res) => { | |
| console.error('Proxy error:', err); | |
| res.status(500).send('Proxy error: ' + err.message); | |
| } | |
| }; | |
| // Apply proxy to all routes | |
| app.use('/', createProxyMiddleware(proxyOptions)); | |
| // Health check endpoint | |
| app.get('/health', (req, res) => { | |
| res.json({ status: 'OK', target: STREAMLIT_URL }); | |
| }); | |
| app.listen(PORT, () => { | |
| console.log(`π Proxy server running on port ${PORT}`); | |
| console.log(`π― Proxying to: ${STREAMLIT_URL}`); | |
| console.log(`π CORS enabled for ALL origins`); | |
| }); |