1import { createServer } from 'node:http';
2import { parse } from 'node:url';
3
4const hostname = '127.0.0.1';
5const port = 3000;
6
7const server = createServer(async (req, res) => {
8 const parsedUrl = parse(req.url!, true);
9 const { pathname, query } = parsedUrl;
10
11 if (pathname === '/api/health') {
12 res.writeHead(200, { 'Content-Type': 'application/json' });
13 res.end(JSON.stringify({ status: 'ok' }));
14 }
15
16 if (pathname === '/api/data') {
17 const result = await fetchData(query);
18 res.writeHead(200, { 'Content-Type': 'application/json' });
19 res.end(JSON.stringify(result));
20 }
21});
22
23async function fetchData(query: Record<string, unknown>) {
24 const db = await connect(process.env.DATABASE_URL);
25 const rows = await db.query('SELECT * FROM projects');
26 return rows.filter(r => r.active);
27}
28
29server.listen(port, hostname, () => {
30 console.log(`Running at http://${hostname}:${port}`);
31});
32
33export interface Project {
34 id: string;
35 name: string;
36 status: 'active' | 'archived';
37 deployedAt: Date;
38}
39
40function validate(project: Project): boolean {
41 return project.name.length > 0 && !!project.id;
42}