-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
97 lines (80 loc) · 2.15 KB
/
index.ts
File metadata and controls
97 lines (80 loc) · 2.15 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { createServer as createServerHttps } from "https";
import { parse } from "url";
import next from "next";
import { existsSync, readFileSync } from "fs";
import {
IncomingMessage,
ServerResponse,
createServer as createServerHttp,
} from "http";
import Logger from "./lib/client/Logger";
import { loadEnvConfig } from "@next/env";
import getRollbar, {
reportDeploymentToRollbar,
} from "./lib/client/RollbarUtils";
const projectDir = process.cwd();
loadEnvConfig(projectDir);
const logger = new Logger(["STARTUP"]);
logger.log("Starting server...");
const mode = process.env.NODE_ENV;
logger.info("Constants set. Using mode:", mode);
const useHttps =
mode !== "test" &&
existsSync("./certs/key.pem") &&
existsSync("./certs/cert.pem");
const httpsOptions = useHttps
? {
key: readFileSync("./certs/key.pem"),
cert: readFileSync("./certs/cert.pem"),
}
: {};
const port = useHttps ? 443 : mode == "test" ? 3000 : 80;
logger.debug(`Using port ${port}`);
const app = next({
dev: mode == "development",
port,
});
const handle = app.getRequestHandler();
logger.debug("App preparing...");
app.prepare().then(() => {
logger.debug("App prepared. Creating server...");
const ioLogger = new Logger(["NETWORKIO"]);
async function handleRaw(
req: IncomingMessage,
res: ServerResponse<IncomingMessage>,
) {
const start = Date.now();
ioLogger.debug(`IN: ${req.method} ${req.url}`);
if (!req.url) return;
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl).then(() =>
ioLogger.debug(
`OUT: ${req.method} ${req.url} ${res.statusCode} in ${Date.now() - start}ms`,
),
);
}
try {
const server = (
useHttps
? createServerHttps(httpsOptions, handleRaw)
: createServerHttp(handleRaw)
)
.listen(port, () => {
logger.info(
process.env.NODE_ENV +
` Server Running At: ${useHttps ? "https" : "http"}://localhost:` +
port,
);
reportDeploymentToRollbar();
})
.on("error", (err: Error) => {
logger.error(err);
getRollbar().error(err);
throw err;
});
logger.debug("Server created. Listening: " + server.listening);
} catch (err) {
logger.error(err);
throw err;
}
});