forked from rust-lang/crates.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastboot.js
73 lines (58 loc) · 1.78 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
71
72
73
/* eslint-env node */
'use strict';
const fs = require('fs');
const os = require('os');
const FastBootAppServer = require('fastboot-app-server');
// because fastboot-app-server uses cluster, but it might change in future
const cluster = require('cluster');
const morgan = require('morgan');
class LoggerWithoutTimestamp {
constructor() {
this.prefix = cluster.isMaster ? 'master' : 'worker';
}
writeLine() {
this._write('info', Array.prototype.slice.apply(arguments));
}
writeError() {
this._write('error', Array.prototype.slice.apply(arguments));
}
_write(level, args) {
args[0] = `[${level}][${this.prefix}] ${args[0]}`;
console.log.apply(console, 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();