-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
46 lines (37 loc) · 1.68 KB
/
Copy pathserver.ts
File metadata and controls
46 lines (37 loc) · 1.68 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
import { createServer } from "node:http";
import nextEnv from "@next/env";
import next from "next";
import { applyAppConfigToProcessEnv } from "./src/server/app-config";
import { createLabTerminalGateway } from "./src/server/labs/terminal-gateway";
const dev = process.env.NODE_ENV !== "production";
const { loadEnvConfig } = nextEnv;
loadEnvConfig(process.cwd(), dev);
applyAppConfigToProcessEnv();
const hostname = process.env.APP_HOST ?? process.env.HOST ?? "127.0.0.1";
const port = Number(process.env.PORT ?? "3210");
const useWebpack = process.env.NEXT_DEV_BUNDLER === "webpack";
const app = next({ dev, hostname, port, ...(useWebpack ? { webpack: true } : {}) });
const handle = app.getRequestHandler();
const terminalGateway = createLabTerminalGateway({
mode: (process.env.PROJECT_LAB_TERMINAL_MODE as "auto" | "docker" | "local" | undefined) ?? "auto",
});
await app.prepare();
const server = createServer((request, response) => {
void handle(request, response);
});
// Node defaults requestTimeout to five minutes. A local model may spend that
// long prefilling a wide context before its first streamed token, so the HTTP
// transport must not undercut the controller's two-hour maximum run budget.
server.requestTimeout = 2 * 60 * 60 * 1000;
server.timeout = 0;
server.on("upgrade", (request, socket, head) => {
if (terminalGateway.handleUpgrade(request, socket, head)) {
return;
}
// Leave Next.js dev/prod upgrade requests, including HMR, to Next's own
// upgrade handler registered by getRequestHandler().
});
server.listen(port, hostname, () => {
console.log(`> Ready on http://${hostname}:${port}`);
console.log("> Lab terminal WebSocket mounted on the Next.js server");
});