-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.ts
53 lines (44 loc) · 1.13 KB
/
deploy.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
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
import {
createRouteMap,
createRouter,
MissingRouteError,
} from 'https://deno.land/x/[email protected]/reno/mod.ts';
const PORT = 8000;
function createErrorResponse(status: number, { message }: Error) {
return new Response(message, {
status,
});
}
export const routes = createRouteMap([
[
'/skyclient.json',
async () => new Response(await Deno.readTextFile('repos/skyclient.json')),
],
[
'/feather.json',
async () => new Response(await Deno.readTextFile('repos/feather.json')),
],
[
'/std.json',
async () => new Response(await Deno.readTextFile('repos/std.json')),
],
]);
const notFound = (e: MissingRouteError) => createErrorResponse(404, e);
const serverError = (e: Error) => createErrorResponse(500, e);
const mapToErrorResponse = (e: Error) =>
e instanceof MissingRouteError ? notFound(e) : serverError(e);
const router = createRouter(routes);
console.log(`Listening for requests on port ${PORT}...`);
await serve(
async (req) => {
try {
return await router(req);
} catch (e) {
return mapToErrorResponse(e);
}
},
{
port: PORT,
}
);