-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.ts
executable file
·75 lines (62 loc) · 2.33 KB
/
server.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env -S deno run --watch --check --allow-sys=hostname --allow-read --allow-net --allow-run=deno,dot --allow-env
import { httpTracer, trace } from "./deps.ts";
import { serveFont, servePublic, serveTemplatedHtml } from './lib/request-handling.ts';
// The different HTTP surfaces we expose
import * as DependenciesOf from './feat/dependencies-of/api.ts';
import * as Shields from './feat/shields/api.ts';
import * as RegistryKey from './feat/registry-key/api.ts';
let port = 5000;
try {
port = parseInt(Deno.env.get('PORT') || port.toString());
} catch (err) {
console.error(`WARN: failed to read $PORT due to ${err.name}`);
}
console.log('Setting up on', { port });
Deno.serve({
port,
}, httpTracer(async request => {
const resp = await handleReq(request);
return resp ?? new Response('404 Not Found', {
status: 404,
});
}));
async function handleReq(req: Request): Promise<Response | undefined> {
const url = new URL(req.url);
{ // feature: dependencies-of
const match = url.pathname.match(/^\/dependencies-of\/(.*)$/);
if (match && req.method === 'GET') {
trace.getActiveSpan()?.setAttribute('http.route', 'dependencies-of');
return await DependenciesOf.handleRequest(req, match[1], url.searchParams);
}
}
{ // feature: shields
const match = url.pathname.match(/^\/shields\/([^\/]+)\/(.+)$/);
if (match && req.method === 'GET') {
trace.getActiveSpan()?.setAttribute('http.route', 'shield.'+match[1]);
return await Shields.handleRequest(req, match[1], match[2]);
}
}
{ // feature: registry-key
if (url.pathname === '/registry-key' && req.method === 'GET') {
trace.getActiveSpan()?.setAttribute('http.route', 'registry-key');
return await RegistryKey.handleRequest(req);
}
}
if (url.pathname === '/') {
trace.getActiveSpan()?.setAttribute('http.route', '/');
return await serveTemplatedHtml(req, 'public/index.html');
}
if ([
'/global.css',
'/icon-deps.png',
'/interactive-graph.js',
].includes(url.pathname)) {
trace.getActiveSpan()?.setAttribute('http.route', 'public');
return await servePublic(req, url.pathname);
}
if (url.pathname.startsWith('/fonts/') &&
url.pathname.endsWith('.woff2')) {
trace.getActiveSpan()?.setAttribute('http.route', 'fonts');
return await serveFont(req, url.pathname.slice(6));
}
}