incognitolm commited on
Commit
d0b572e
·
1 Parent(s): 6cce35a

Use JSDelivr CDN for Public stuff

Browse files
Files changed (1) hide show
  1. server/index.js +12 -8
server/index.js CHANGED
@@ -21,9 +21,10 @@ export const supabaseAnon = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
21
  const PORT = process.env.PORT || 7860;
22
  const app = express();
23
 
24
- app.use(express.static('/public/public'));
25
  app.use(express.json({ limit: '10mb' }));
26
- // Require turnstile cookie for API routes (except the verification endpoint itself)
 
27
  app.use('/api', (req, res, next) => {
28
  const exempt = ['/turnstile', '/health'];
29
  if (exempt.includes(req.path)) return next();
@@ -31,6 +32,7 @@ app.use('/api', (req, res, next) => {
31
  if (cookieHeader.includes('turnstile=1')) return next();
32
  return res.status(403).json({ error: 'turnstile:required' });
33
  });
 
34
  app.get('/health', (_req, res) => res.json({ ok: true }));
35
 
36
  app.get('/api/share/:token', async (req, res) => {
@@ -48,7 +50,6 @@ app.get('/api/share/:token', async (req, res) => {
48
  } catch { res.status(500).json({ error: 'Server error' }); }
49
  });
50
 
51
- // Turnstile verification endpoint - accepts token and verifies with Cloudflare
52
  app.post('/api/turnstile', async (req, res) => {
53
  try {
54
  const token = req.body?.token;
@@ -65,7 +66,6 @@ app.post('/api/turnstile', async (req, res) => {
65
  });
66
  const j = await r.json();
67
  if (j?.success) {
68
- // Set a short-lived cookie to allow access to other endpoints
69
  res.cookie('turnstile', '1', { maxAge: 24 * 3600 * 1000, path: '/', sameSite: 'lax' });
70
  return res.json({ success: true });
71
  }
@@ -73,9 +73,14 @@ app.post('/api/turnstile', async (req, res) => {
73
  } catch (e) { console.error('turnstile verify', e); return res.status(500).json({ error: 'Server error' }); }
74
  });
75
 
 
 
76
  app.get('*', (req, res) => {
77
- if (!req.path.startsWith('/api/'))
78
- res.sendFile(path.join('/public/public', 'index.html'));
 
 
 
79
  });
80
 
81
  const httpServer = createServer(app);
@@ -87,7 +92,6 @@ wss.on('connection', (ws, req) => {
87
  const ip = (req.headers['x-forwarded-for'] || '').split(',')[0].trim()
88
  || req.socket.remoteAddress || 'unknown';
89
  const userAgent = req.headers['user-agent'] || 'unknown';
90
- // Mark verified if request included a turnstile cookie
91
  const cookies = (req.headers.cookie || '').split(';').map(s => s.trim()).filter(Boolean);
92
  const cookieMap = Object.fromEntries(cookies.map(c => { const i = c.indexOf('='); return [c.slice(0, i), c.slice(i+1)]; }));
93
  const verified = cookieMap.turnstile === '1';
@@ -109,4 +113,4 @@ wss.on('connection', (ws, req) => {
109
  safeSend(ws, { type: 'connected', tempId: wsClients.get(ws)?.tempId });
110
  });
111
 
112
- httpServer.listen(PORT, '0.0.0.0', () => console.log(`Running on port ${PORT}`));
 
21
  const PORT = process.env.PORT || 7860;
22
  const app = express();
23
 
24
+
25
  app.use(express.json({ limit: '10mb' }));
26
+
27
+ // API turnstile check (unchanged)
28
  app.use('/api', (req, res, next) => {
29
  const exempt = ['/turnstile', '/health'];
30
  if (exempt.includes(req.path)) return next();
 
32
  if (cookieHeader.includes('turnstile=1')) return next();
33
  return res.status(403).json({ error: 'turnstile:required' });
34
  });
35
+
36
  app.get('/health', (_req, res) => res.json({ ok: true }));
37
 
38
  app.get('/api/share/:token', async (req, res) => {
 
50
  } catch { res.status(500).json({ error: 'Server error' }); }
51
  });
52
 
 
53
  app.post('/api/turnstile', async (req, res) => {
54
  try {
55
  const token = req.body?.token;
 
66
  });
67
  const j = await r.json();
68
  if (j?.success) {
 
69
  res.cookie('turnstile', '1', { maxAge: 24 * 3600 * 1000, path: '/', sameSite: 'lax' });
70
  return res.json({ success: true });
71
  }
 
73
  } catch (e) { console.error('turnstile verify', e); return res.status(500).json({ error: 'Server error' }); }
74
  });
75
 
76
+ // Serve all public files via CDN
77
+ const CDN_BASE = 'https://cdn.jsdelivr.net/gh/incognitolm/InferencePort-Pages';
78
  app.get('*', (req, res) => {
79
+ if (!req.path.startsWith('/api/')) {
80
+ const filePath = req.path === '/' ? '/index.html' : req.path;
81
+ const url = `${CDN_BASE}${filePath}`;
82
+ res.redirect(url);
83
+ }
84
  });
85
 
86
  const httpServer = createServer(app);
 
92
  const ip = (req.headers['x-forwarded-for'] || '').split(',')[0].trim()
93
  || req.socket.remoteAddress || 'unknown';
94
  const userAgent = req.headers['user-agent'] || 'unknown';
 
95
  const cookies = (req.headers.cookie || '').split(';').map(s => s.trim()).filter(Boolean);
96
  const cookieMap = Object.fromEntries(cookies.map(c => { const i = c.indexOf('='); return [c.slice(0, i), c.slice(i+1)]; }));
97
  const verified = cookieMap.turnstile === '1';
 
113
  safeSend(ws, { type: 'connected', tempId: wsClients.get(ws)?.tempId });
114
  });
115
 
116
+ httpServer.listen(PORT, '0.0.0.0', () => console.log(`Running on port ${PORT}`));