-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
292 lines (256 loc) · 7.43 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* eslint-disable remix/node-server-imports */
import { createRequestHandler } from '@remix-run/express';
import compression from 'compression';
import express from 'express';
import morgan from 'morgan';
import path from 'path';
import { ChildProcess, spawnSync } from 'child_process';
import { execa } from 'execa';
import { symmetric } from 'secure-webhooks';
/*
1. start quirrel
2. get token
3. load ci jobs
4. start remix
note: find missing processes
lsof -i :9181
kill -9 <<PID>>
*/
const port = process.env.WEB_PORT || 3000;
if (!process.env.QUIRREL_PORT) {
process.env.QUIRREL_PORT = '9891';
}
if (!process.env.MEILI_PORT) {
process.env.MEILI_PORT = '7700';
}
process.env.SESSION_SECRET = `atlas-${port}`;
const MODE = process.env.NODE_ENV;
const BUILD_DIR = path.join(process.cwd(), 'build');
let child: ChildProcess | undefined;
let search: ChildProcess | undefined;
const sleep = (ms: number) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
type FetchWithRetries = (
url: string,
options: {
[x: string]: any;
method?: string;
headers?: { Authorization?: string; 'x-quirrel-signature'?: string };
maxRetries: any;
},
retryCount?: number,
) => Promise<Response>;
const fetchWithRetries: FetchWithRetries = async (
url,
options,
retryCount = 0,
) => {
// split out the maxRetries option from the remaining
// options (with a default of 3 retries)
if (retryCount > 0) console.log(`retry ${retryCount}...`);
const { maxRetries = 3, ...remainingOptions } = options;
let response;
try {
response = await fetch(url, remainingOptions);
if (![201, 200].includes(response.status)) {
const x = await response.text();
console.log(response);
console.log(x);
throw Error(response.status.toString());
}
return response;
} catch (error) {
// if the retryCount has not been exceeded, call again
if (retryCount > 0) console.log(error);
if (retryCount < maxRetries) {
await sleep(5000);
return fetchWithRetries(url, options, retryCount + 1);
}
console.log('Failed to connect to quirrel: max retries exceeded');
// max retries exceeded
child?.kill('SIGINT');
search?.kill('SIGINT');
throw error;
}
};
(async () => {
if (MODE === 'production') {
process.env.MEILISEARCH_URL = `http://127.0.0.1:${process.env.MEILI_PORT}`;
process.env.MEILI_MASTER_KEY = `atlas-meili-${process.env.MEILI_PORT}`;
process.env.QUIRREL_API_URL = `http://127.0.0.1:${process.env.QUIRREL_PORT}`;
process.env.QUIRREL_BASE_URL = `http://127.0.0.1:${port}`;
child = startQuirrel();
search = startMeili();
await getQuirrelToken();
}
const app = express();
app.listen(port, () => {
// require the built app so we're ready when the first request comes in
require(BUILD_DIR);
console.log(`✅ app ready: http://127.0.0.1:${port}`);
});
app.use((req, res, next) => {
// helpful headers:
res.set('x-fly-region', process.env.FLY_REGION ?? 'unknown');
res.set('Strict-Transport-Security', `max-age=${60 * 60 * 24 * 365 * 100}`);
// /clean-urls/ -> /clean-urls
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();
});
// if we're not in the primary region, then we need to make sure all
// non-GET/HEAD/OPTIONS requests hit the primary region rather than read-only
// Postgres DBs.
// learn more: https://fly.io/docs/getting-started/multi-region-databases/#replay-the-request
app.all('*', function getReplayResponse(req, res, next) {
const { method, path: pathname } = req;
const { PRIMARY_REGION, FLY_REGION } = process.env;
const isMethodReplayable = !['GET', 'OPTIONS', 'HEAD'].includes(method);
const isReadOnlyRegion =
FLY_REGION && PRIMARY_REGION && FLY_REGION !== PRIMARY_REGION;
const shouldReplay = isMethodReplayable && isReadOnlyRegion;
if (!shouldReplay) return next();
const logInfo = {
pathname,
method,
PRIMARY_REGION,
FLY_REGION,
};
console.info(`Replaying:`, logInfo);
res.set('fly-replay', `region=${PRIMARY_REGION}`);
return res.sendStatus(409);
});
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');
// Remix fingerprints its assets so we can cache forever.
app.use(
'/build',
express.static('public/build', { 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.all(
'*',
MODE === 'production'
? createRequestHandler({ build: require(BUILD_DIR) })
: (...args) => {
purgeRequireCache();
const requestHandler = createRequestHandler({
build: require(BUILD_DIR),
mode: MODE,
});
return requestHandler(...args);
},
);
// call search loader for an initial load
const response = await fetchWithRetries(
`http://127.0.0.1:${port}/queues/searchService`,
{
body: '',
method: 'POST',
headers: {
'x-quirrel-signature': symmetric.sign(
'',
process.env.QUIRREL_TOKEN || '',
),
},
maxRetries: 5,
},
);
console.log('🔍 Triggered search load');
})();
function startQuirrel() {
const child: ChildProcess = execa(
'node',
[`${process.cwd()}/node_modules/quirrel/dist/cjs/src/api/main.js`],
{
env: {
...process.env,
PORT: process.env.QUIRREL_PORT,
PASSPHRASES: `atlas`,
DISABLE_TELEMETRY: '1',
},
stdio: 'inherit',
},
);
child.on('exit', function (code, signal) {
throw Error(
'child process exited with ' + `code ${code} and signal ${signal}`,
);
});
return child;
}
function startMeili() {
const child: ChildProcess = execa('./etc/meilisearch', {
env: {
...process.env,
MEILI_HTTP_ADDR: `127.0.0.1:${process.env.MEILI_PORT}`,
MEILI_NO_ANALYTICS: 'true',
},
stdio: 'inherit',
});
child.on('exit', function (code, signal) {
throw Error(
'child process exited with ' + `code ${code} and signal ${signal}`,
);
});
return child;
}
function purgeRequireCache() {
// purge require cache on requests for "server side HMR" this won't let
// you have in-memory objects between requests in development,
// alternatively you can set up nodemon/pm2-dev to restart the server on
// file changes, we prefer the DX of this though, so we've included it
// for you by default
for (const key in require.cache) {
if (key.startsWith(BUILD_DIR)) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete require.cache[key];
}
}
}
async function getQuirrelToken() {
//curl --user ignored:atlas-quirrel-9891 -X PUT http://127.0.0.1:9891/tokens/prod
console.log('Quirrel: getting token...');
const response = await fetchWithRetries(
`http://127.0.0.1:${process.env.QUIRREL_PORT}/tokens/prod`,
{
method: 'PUT',
headers: {
Authorization: 'Basic ' + btoa('ignored:atlas'),
},
maxRetries: 5,
},
);
process.env.QUIRREL_TOKEN = await response.text();
// load cron jobs
console.log('Quirrel: loading cron jobs');
try {
const ci = spawnSync(
'node',
[`${process.cwd()}/node_modules/quirrel/dist/cjs/src/cli/index.js`, 'ci'],
{
env: {
...process.env,
PORT: process.env.QUIRREL_PORT,
},
stdio: 'inherit',
encoding: 'utf-8',
},
);
} catch (error) {
child?.kill('SIGINT');
search?.kill('SIGINT');
throw error;
}
}