-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
92 lines (81 loc) · 2.54 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
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
import path from "path";
import { spawn } from "child_process";
import express from "express";
import next from "next";
import nodemailerSendgrid from "nodemailer-sendgrid";
import payload from "payload";
import { Payload } from "payload/dist/payload";
import { loadEnvConfig } from "@next/env";
const projectDir = process.cwd();
loadEnvConfig(projectDir);
const dev = process.env.NODE_ENV !== "production";
const hostname = process.env.NEXT_HOSTNAME || "localhost";
const port = parseInt(process.env.PORT || "3000", 10);
const sendGridAPIKey = process.env.SENDGRID_API_KEY;
if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
process.on("SIGTERM", () => process.exit(0));
process.on("SIGINT", () => process.exit(0));
}
const app = express();
const start = async (): Promise<void> => {
let localPayload: Payload;
try {
localPayload = await payload.init({
...(sendGridAPIKey
? {
email: {
transportOptions: nodemailerSendgrid({
apiKey: sendGridAPIKey,
}),
fromName: process.env.SENDGRID_FROM_NAME || "Code for Africa CMS",
fromAddress:
process.env.SENDGRID_FROM_EMAIL || "[email protected]",
},
}
: undefined),
secret: process.env.PAYLOAD_SECRET,
express: app,
onInit: (initPayload) => {
initPayload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
},
});
} catch (e: any) {
console.error(e);
process.exit();
}
if (process.env.NEXT_BUILD) {
app.listen(port, async () => {
localPayload.logger.info("NextJS is now building...");
const nextBuild = spawn(
"pnpm",
["next", "build", path.resolve(projectDir)],
{
shell: true,
stdio: "inherit",
},
);
nextBuild.on("close", (code) => {
process.exit(code);
});
nextBuild.on("error", (err) => {
localPayload.logger.error(err);
process.exit(1);
});
});
return;
}
const nextApp = next({ dev, hostname, port });
const nextHandler = nextApp.getRequestHandler();
nextApp.prepare().then(() => {
localPayload.logger.info("NextJS started");
app.get("*", (req: any, res: any) => nextHandler(req, res));
app.post("*", (req: any, res: any) => nextHandler(req, res));
app.put("*", (req: any, res: any) => nextHandler(req, res));
app.listen(port, async () => {
localPayload.logger.info(
`Next.js App URL: ${process.env.NEXT_PUBLIC_APP_URL}`,
);
});
});
};
start();