-
-
Notifications
You must be signed in to change notification settings - Fork 814
Expand file tree
/
Copy pathdeno-server.ts
More file actions
50 lines (40 loc) · 1.42 KB
/
deno-server.ts
File metadata and controls
50 lines (40 loc) · 1.42 KB
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
import "#nitro/virtual/polyfills";
import type { ServerRequest } from "srvx";
import { serve } from "srvx/deno";
import wsAdapter from "crossws/adapters/deno";
import { useNitroApp } from "nitro/app";
import { startScheduleRunner } from "#nitro/runtime/task";
import { trapUnhandledErrors } from "#nitro/runtime/error/hooks";
import { resolveGracefulShutdownConfig, setupShutdownHooks } from "#nitro/runtime/shutdown";
import { resolveWebsocketHooks } from "#nitro/runtime/app";
const _parsedPort = Number.parseInt(process.env.NITRO_PORT ?? process.env.PORT ?? "");
const port = Number.isNaN(_parsedPort) ? 3000 : _parsedPort;
const host = process.env.NITRO_HOST || process.env.HOST;
const cert = process.env.NITRO_SSL_CERT;
const key = process.env.NITRO_SSL_KEY;
const gracefulShutdown = resolveGracefulShutdownConfig();
const nitroApp = useNitroApp();
let _fetch = nitroApp.fetch;
if (import.meta._websocket) {
const { handleUpgrade } = wsAdapter({ resolve: resolveWebsocketHooks });
_fetch = (req: ServerRequest) => {
if (req.headers.get("upgrade") === "websocket") {
return handleUpgrade(req, req.runtime!.deno!.info);
}
return nitroApp.fetch(req);
};
}
serve({
port,
hostname: host,
tls: cert && key ? { cert, key } : undefined,
fetch: _fetch,
gracefulShutdown,
});
trapUnhandledErrors();
setupShutdownHooks();
// Scheduled tasks
if (import.meta._tasks) {
startScheduleRunner();
}
export default {};