-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathserver.ts
51 lines (42 loc) · 1.46 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
import { isIP } from "node:net";
import { serve } from "@hono/node-server";
import { behindProxy } from "x-forwarded-fetch";
import { configureSentry } from "../src/sentry";
import app from "../src/index";
// biome-ignore lint/complexity/useLiteralKeys: tsc complains about this (TS4111)
configureSentry(process.env["SENTRY_DSN"]);
// biome-ignore lint/complexity/useLiteralKeys: tsc complains about this (TS4111)
const BEHIND_PROXY = process.env["BEHIND_PROXY"] === "true";
// biome-ignore lint/complexity/useLiteralKeys: tsc complains about this (TS4111)
const BIND = process.env["BIND"];
// biome-ignore lint/complexity/useLiteralKeys: tsc complains about this (TS4111)
const PORT = Number.parseInt(process.env["PORT"] ?? "3000", 10);
if (!Number.isInteger(PORT)) {
console.error("Invalid PORT: must be an integer");
process.exit(1);
}
if (BIND && BIND !== "localhost" && !isIP(BIND)) {
console.error(
"Invalid BIND: must be an IP address or localhost, if specified",
);
process.exit(1);
}
serve(
{
fetch: BEHIND_PROXY
? behindProxy(app.fetch.bind(app))
: app.fetch.bind(app),
port: PORT,
hostname: BIND,
},
(info) => {
let host = info.address;
// We override it here to show localhost instead of what it resolves to:
if (BIND === "localhost") {
host = "localhost";
} else if (info.family === "IPv6") {
host = `[${info.address}]`;
}
console.log(`Listening on http://${host}:${info.port}/`);
},
);