forked from rust-lang/crates.io
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastboot.js
70 lines (57 loc) · 1.71 KB
/
fastboot.js
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
'use strict';
// because fastboot-app-server uses cluster, but it might change in future
const FastBootAppServer = require('fastboot-app-server');
const morgan = require('morgan');
const cluster = require('node:cluster');
const fs = require('node:fs');
const os = require('node:os');
class LoggerWithoutTimestamp {
constructor() {
this.prefix = cluster.isMaster ? 'master' : 'worker';
}
writeLine(...args) {
this._write('info', args);
}
writeError(...args) {
this._write('error', args);
}
_write(level, args) {
args[0] = `[${level}][${this.prefix}] ${args[0]}`;
console.log(...args);
}
}
function writeAppInitializedWhenReady(logger) {
let timeout;
timeout = setInterval(function () {
logger.writeLine('waiting backend');
if (fs.existsSync('/tmp/backend-initialized')) {
logger.writeLine('backend is up. let heroku know the app is ready');
fs.writeFileSync('/tmp/app-initialized', 'hello');
clearInterval(timeout);
} else {
logger.writeLine('backend is still not up');
}
}, 1000);
}
var logger = new LoggerWithoutTimestamp();
logger.writeLine(`${os.cpus().length} cores available`);
let workerCount = process.env.WEB_CONCURRENCY || 1;
let logRequests = morgan(
'at=info method=:method path=":url" ' +
'request_id=:req[x-request-id] ' +
'fwd=":req[x-real-ip]" ' +
'user_agent=":req[user-agent]"',
);
let server = new FastBootAppServer({
distPath: 'dist',
port: 9000,
ui: logger,
workerCount: workerCount,
// afterMiddleware won't be called since Fastboot's middleware
// doesn't call next().
beforeMiddleware: app => app.use(logRequests),
});
if (!cluster.isWorker) {
writeAppInitializedWhenReady(logger);
}
server.start();