Something went wrong. Try again.
Show your Tangled contribution graph on your GitHub profile. tangled-activity.vercel.app/api/graph?account=nove-b.dev&range=12
Something went wrong. Try again.
1.6 kB · 68 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import http from 'node:http';import { URL } from 'node:url';
import { buildGraphSvg, clampRange, fetchActivityByDay, normalizeAccount,} from './tangledGraph';
const PORT = Number(process.env.PORT ?? 3000);
const server = http.createServer(async (req, res) => { if (!req.url) { res.statusCode = 400; res.end('Bad Request'); return; }
const url = new URL(req.url, `http://localhost:${PORT}`);
if (url.pathname !== '/api/graph') { res.statusCode = 404; res.setHeader('Content-Type', 'text/plain; charset=utf-8'); res.end('Not Found'); return; }
const account = normalizeAccount(url.searchParams.get('account') ?? undefined); const range = clampRange(url.searchParams.get('range') ?? undefined);
res.setHeader('Content-Type', 'image/svg+xml; charset=utf-8'); res.setHeader('Cache-Control', 'no-store');
if (!account) { res.statusCode = 400; res.end( buildGraphSvg({ account: 'unknown', range, days: [], error: 'Missing or invalid account parameter', }), ); return; }
try { const days = await fetchActivityByDay(account, range); res.statusCode = 200; res.end(buildGraphSvg({ account, range, days })); } catch (error) { const message = error instanceof Error ? error.message : 'Failed to load feed'; res.statusCode = 502; res.end( buildGraphSvg({ account, range, days: [], error: message, }), ); }});
server.listen(PORT, () => { process.stdout.write(`Local server running at http://localhost:${PORT}\n`);});