-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
52 lines (49 loc) · 1.8 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
import { serve, Server } from "https://deno.land/std/http/server.ts";
import { printRequest } from "./actions/print.ts";
import { warningLog, successLog } from "https://deno.land/x/colorlog/mod.ts";
import Router from "./router.ts";
import { constructResponse, constructHeaders } from "./actions/respond.ts";
import Watcher from "./fileWatcher.ts";
import { handleUnknownModel } from "./actions/generate.ts";
import { modelsFolder } from "./CLA.ts";
const port = Number.parseInt(Deno.env.get("PORT") ?? "8000") || 8000;
const server = serve({ port });
successLog(`[+] Server running on port: ${port}`);
const router = new Router();
const watcher = new Watcher(modelsFolder());
let handleRequest = async (req: any) => {
const urlMethod =
req.url.split("/")[req.url.split("/").length - 1].split("?")[0] + ".ts"; // last piece of url (test/some/stuff) -> (stuff)
if (router.contains(urlMethod)) {
const data = JSON.stringify(
await constructResponse(urlMethod, req.headers, req.url),
);
successLog(`[+] Request was received with path ${req.url} with method ${req.method}`)
successLog("[+] Responding with: ");
printRequest(JSON.parse(data));
req.respond({ body: data, headers: constructHeaders(req) });
} else {
warningLog(
`[+] Printing unknown request (${urlMethod}) and responding with an empty object`,
);
handleUnknownModel(urlMethod);
printRequest({
Method: req.method,
Headers: req.headers,
Body: req.body,
URL: req.url,
Length: req.contentLength,
});
req.respond({ headers: constructHeaders(req) });
}
};
for await (const req of server) {
if (watcher.updated()) {
// models directory has been changed
router.reload(); // reload the models
watcher.ack();
handleRequest(req);
} else {
handleRequest(req);
}
}