Repository navigation
Expand file tree
/
Copy pathdev-server.mjs
More file actions
43 lines (40 loc) · 2.28 KB
/
Copy pathdev-server.mjs
File metadata and controls
43 lines (40 loc) · 2.28 KB
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
// dev-server.mjs — local shim that hosts the Vercel /api functions on :3131.
// Vite dev proxies /api here (see vite.config.js). Not used in production.
// Env needed: RIOT_API_KEY (optional if BYOK), TURSO_DATABASE_URL, TURSO_AUTH_TOKEN.
import { createServer } from 'node:http';
import { readFileSync } from 'node:fs';
// Load .env (gitignored) so RIOT_API_KEY etc. don't need to be exported by hand.
try {
for (const line of readFileSync(new URL('./.env', import.meta.url), 'utf8').split('\n')) {
const m = /^\s*([A-Z0-9_]+)\s*=\s*(.+?)\s*$/.exec(line);
if (m && !line.trimStart().startsWith('#') && !process.env[m[1]]) process.env[m[1]] = m[2];
}
} catch { /* no .env — fine, env vars may be set externally */ }
import matches from './api/matches.mjs';
import analyze from './api/analyze.mjs';
import history from './api/history.mjs';
import bookmarks from './api/bookmarks.mjs';
import live from './api/live.mjs';
import promotion from './api/promotion.mjs';
import promotionFeed from './api/promotion-feed.mjs';
import shareEvent from './api/share-event.mjs';
const PORT = process.env.PORT || 3131;
const routes = { '/api/matches': matches, '/api/analyze': analyze, '/api/history': history, '/api/bookmarks': bookmarks, '/api/live': live, '/api/promotion': promotion, '/api/promotion-feed': promotionFeed, '/api/share-event': shareEvent };
createServer(async (req, res) => {
const url = new URL(req.url, 'http://x');
// minimal Vercel-style req/res polyfill
req.query = Object.fromEntries(url.searchParams);
if (req.method === 'POST' && url.pathname === '/api/share-event') {
let body = '';
for await (const chunk of req) {
body += chunk;
if (body.length > 1000) return res.writeHead(413).end();
}
try { req.body = JSON.parse(body); } catch { return res.writeHead(400).end(); }
}
res.status = code => { res.statusCode = code; return res; };
res.json = obj => { res.setHeader('content-type', 'application/json'); res.setHeader('access-control-allow-origin', '*'); res.end(JSON.stringify(obj)); };
const fn = routes[url.pathname];
if (!fn) return res.status(404).json({ error: 'not found' });
try { await fn(req, res); } catch (e) { res.status(500).json({ error: e.message }); }
}).listen(PORT, () => console.log(`fairness dev API on http://localhost:${PORT}`));