import express from 'express'; import { createProxyMiddleware } from 'http-proxy-middleware'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const app = express(); const PORT = 7860; const DIST_DIR = join(__dirname, 'dist'); // Proxy /api/anthropic/* to Anthropic API app.use('/api/anthropic', createProxyMiddleware({ target: 'https://api.anthropic.com', changeOrigin: true, pathRewrite: { '^/api/anthropic': '' }, onProxyReq: (proxyReq, req) => { // Inject API key from environment (never exposed to client) proxyReq.setHeader('x-api-key', process.env.ANTHROPIC_API_KEY); proxyReq.setHeader('anthropic-version', '2023-06-01'); proxyReq.setHeader('anthropic-beta', 'mcp-client-2025-04-04'); }, onError: (err, req, res) => { console.error('Proxy error:', err); res.status(502).json({ error: 'Proxy error to Anthropic' }); } })); // Health check for HF Spaces app.get('/healthz', (req, res) => res.send('ok')); // Serve static files from dist app.use(express.static(DIST_DIR, { index: false })); // SPA fallback - serve index.html for non-API routes app.get('*', (req, res) => { res.sendFile(join(DIST_DIR, 'index.html')); }); app.listen(PORT, '0.0.0.0', () => { console.log(`Scribin' server running on port ${PORT}`); });