File size: 1,387 Bytes
1c64d9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}`);
});