-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
38 lines (32 loc) · 1.28 KB
/
main.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
#!/usr/bin/env -S deno run --allow-read=. --allow-write=database.sqlite --allow-net --allow-env=SQLITE,PORT --config=./deno.jsonc --no-check
import { config } from "./src/config.ts";
import { log } from "./src/log.ts";
import { Application } from "./deps.ts";
import { router } from "./src/routes/mod.ts";
import "./src/models/database.ts";
export const app = new Application();
// `controller.abort()` can close the server gracefully
// Would use it with the `Deno.addSignalListener` API but it's quite unstable
// https://github.com/denoland/deno/issues/13271
export const controller = new AbortController();
app.addEventListener("listen", ({ hostname, port, secure }) => {
log.info(
`Listening on: ${secure ? "https://" : "http://"}${
hostname ??
"localhost"
}:${port}`,
);
});
app.addEventListener("error", (evt) => log.error(evt.error));
app.use(router.routes());
app.use((ctx, next) => {
ctx.response.headers.set("Access-Control-Allow-Credentials", "*");
ctx.response.headers.set("Access-Control-Allow-Headers", "*");
ctx.response.headers.set("Access-Control-Allow-Methods", "*");
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
next();
});
app.use(router.allowedMethods());
if (import.meta.main) {
app.listen({ port: config.port, signal: controller.signal });
}