-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
82 lines (69 loc) · 2.42 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
76
77
78
79
80
81
82
import { listenAndServe, ServerRequest } from "../deps.ts";
import importMap from "../importmap.ts";
import { removePathQuery, removeSlashes, serveStatic } from "./functions.ts";
import pageNotFound from "./routes/404.ts";
async function serveRequest(
request: ServerRequest,
) {
const { url } = request;
try {
// Try if manual override exists
const module = await import(importMap[url]);
const response = await module.default(request);
request.respond(response);
} catch (error) {
try {
// Try if static file exists
const response = await serveStatic(request);
request.respond(response);
} catch (error) {
try {
// Try if explicit route handler exists
const path = removeSlashes(removePathQuery(url));
const modulePath = path === "" ? "./routes.ts" : `./routes/${path}.ts`;
const module = await import(modulePath);
const response = await module.default(request);
request.respond(response);
} catch (error) {
try {
// Try if scoped route handler exists
if (url.startsWith("/comics")) {
const module = await import(`./routes/comics.ts`);
const response = await module.default(request);
request.respond(response);
} else if (url.startsWith("/api/comics")) {
const module = await import(`./routes/api/comics.ts`);
const response = await module.default(request);
request.respond(response);
} else if (url.startsWith("/api/")) {
const module = await import(`./routes/api.ts`);
const response = await module.default(request);
request.respond(response);
} else {
// Remove the duplicate as this also exist in the catch block of next step
const response = await pageNotFound(request);
request.respond(response);
}
} catch (error) {
const response = await pageNotFound(request);
request.respond(response);
}
}
}
}
}
type IRequestHandler = (req: ServerRequest) => void;
async function main(requestHandler: IRequestHandler) {
// 1729
const port = Number(Deno.env.get("PORT") || 1729);
const address = { port };
console.log(`Dev Server started at http://localhost:${port}`);
listenAndServe(
address,
requestHandler,
);
}
if (import.meta.main) {
main(serveRequest);
}
export default main;