-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcore.server.ts
261 lines (225 loc) · 7.63 KB
/
core.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import * as path from "node:path";
import { createRequestHandler, type RequestHandler } from "@remix-run/express";
import { broadcastDevReady, installGlobals } from "@remix-run/node";
import compression from "compression";
import express from "express";
import morgan from "morgan";
import nodemailer from "nodemailer";
import payload from "payload";
import sourceMapSupport from "source-map-support";
import invariant from "tiny-invariant";
import { settings } from "./app/config";
// patch in Remix runtime globals
installGlobals({});
require("dotenv").config();
sourceMapSupport.install();
// Make sure devDependencies don't ship to production
const chokidar =
process.env.NODE_ENV === "development" ? require("chokidar") : null;
const rdt =
process.env.NODE_ENV === "development"
? require("remix-development-tools/server")
: null;
/**
* @typedef {import('@remix-run/node').ServerBuild} ServerBuild
*/
const BUILD_PATH = path.resolve("./build/index.js");
const WATCH_PATH = path.resolve("./build/version.txt");
/**
* Initial build
* @type {ServerBuild}
*/
let build = require(BUILD_PATH);
const transport = nodemailer.createTransport({
host: process.env.NODEMAILER_HOST ?? "smtp.resend.com",
port: parseInt(process.env.NODEMAILER_PORT ?? "465"),
secure: true,
auth: {
user: process.env.NODEMAILER_USER ?? "resend",
pass: process.env.NODEMAILER_PASSWORD,
},
});
//Start core site (remix + payload instance)
async function startCore() {
const app = express();
invariant(process.env.PAYLOADCMS_SECRET, "PAYLOADCMS_SECRET is required");
// Initialize Payload
await payload.init({
secret: process.env.PAYLOADCMS_SECRET,
express: app,
onInit: () => {
payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
},
...(process.env.NODEMAILER_PASSWORD
? {
email: {
transport,
fromName: settings?.fromName ?? "No Reply - Mana Wiki",
fromAddress: settings?.fromEmail ?? "[email protected]",
},
}
: {
email: {
fromName: "Admin",
fromAddress: "[email protected]",
logMockCredentials: true, // Optional
},
}),
});
app.use(compression());
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
app.disable("x-powered-by");
const getHost = (req: { get: (key: string) => string | undefined }) =>
req.get("X-Forwarded-Host") ?? req.get("host") ?? "";
// fly is our proxy
app.set("trust proxy", true);
app.use((req, res, next) => {
//enforce https connection to make sure the site uses http2 protocol
const proto = req.get("X-Forwarded-Proto");
const host = getHost(req);
// console.log("proto", proto, "host", host);
if (proto === "http") {
res.set("X-Forwarded-Proto", "https");
res.redirect(`https://${host}${req.originalUrl}`);
return;
}
// if they connect once with HTTPS, then they'll connect with HTTPS for the next hundred years
res.set(
"Strict-Transport-Security",
"max-age=63072000; includeSubDomains; preload",
);
// no ending slashes for SEO reasons
if (req.path.endsWith("/") && req.path.length > 1) {
const query = req.url.slice(req.path.length);
const safepath = req.path.slice(0, -1).replace(/\/+/g, "/");
res.redirect(301, safepath + query);
return;
}
next();
});
// Remix fingerprints its assets so we can cache forever.
app.use(
"/build",
express.static("public/build", { immutable: true, maxAge: "1y" }),
);
// Aggressively cache fonts for a year
app.use(
"/fonts",
express.static("public/fonts", { immutable: true, maxAge: "1y" }),
);
app.use(
"/icons",
express.static("public/icons", { immutable: true, maxAge: "1y" }),
);
// Everything else (like favicon.ico) is cached for an hour. You may want to be
// more aggressive with this caching.
app.use(express.static("public", { maxAge: "1h" }));
app.use(morgan("tiny"));
app.use(payload.authenticate);
// This makes sure the build is wrapped on reload by RDT
if (rdt) build = rdt.withServerDevTools(build);
// Check if the server is running in development mode and reflect realtime changes in the codebase.
// We'll also inject payload in the remix handler so we can use it in our routes.
app.all(
"*",
process.env.NODE_ENV === "development"
? createDevRequestHandler()
: createProductionRequestHandler(),
);
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Express server listening on port http://localhost:${port}`);
if (process.env.NODE_ENV === "development") {
broadcastDevReady(build);
}
});
}
startCore();
// Create a request handler for production
function createProductionRequestHandler(): RequestHandler {
return createRequestHandler({
build,
mode: process.env.NODE_ENV,
getLoadContext(req, res) {
const userData = req.user
? {
id: req?.user?.id,
roles: req?.user?.roles,
username: req?.user?.username,
avatar: {
id: req?.user?.avatar?.id,
url: req?.user?.avatar?.url,
},
}
: undefined;
return {
payload: req.payload,
user: userData,
res,
};
},
});
}
// Create a request handler that watches for changes to the server build during development.
function createDevRequestHandler(): RequestHandler {
async function handleServerUpdate() {
// This makes sure the build is wrapped on reload by RDT
build = rdt.withServerDevTools(await reimportServer());
// Add debugger to assist in v2 dev debugging
if (build?.assets === undefined) {
console.log(build.assets);
debugger;
}
// 2. tell dev server that this app server is now up-to-date and ready
broadcastDevReady(build);
}
chokidar
.watch(WATCH_PATH, {
ignoreInitial: true,
})
.on("add", handleServerUpdate)
.on("change", handleServerUpdate);
// wrap request handler to make sure its recreated with the latest build for every request
return async (req, res, next) => {
try {
return createRequestHandler({
build,
mode: process.env.NODE_ENV,
getLoadContext(req, res) {
const userData = req.user
? {
id: req?.user?.id,
roles: req?.user?.roles,
username: req?.user?.username,
avatar: {
id: req?.user?.avatar?.id,
url: req?.user?.avatar?.url,
},
}
: undefined;
return {
payload: req.payload,
user: userData,
res,
};
},
})(req, res, next);
} catch (error) {
next(error);
}
};
}
// CJS require cache busting
/**
* @type {() => Promise<ServerBuild>}
*/
async function reimportServer() {
// 1. manually remove the server build from the require cache
Object.keys(require.cache).forEach((key) => {
if (key.startsWith(BUILD_PATH)) {
delete require.cache[key];
}
});
// 2. re-import the server build
return require(BUILD_PATH);
}