Skip to content

Commit 02a8806

Browse files
committed
Add Fastboot server
1 parent 7ec618c commit 02a8806

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

fastboot.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-disable no-console */
2+
/* eslint-env node */
3+
4+
'use strict';
5+
6+
const fs = require('fs');
7+
const os = require('os');
8+
const FastBootAppServer = require('fastboot-app-server');
9+
10+
// because fastboot-app-server uses cluster, but it might change in future
11+
const cluster = require('cluster');
12+
13+
class LoggerWithoutTimestamp {
14+
constructor() {
15+
this.prefix = cluster.isMaster ? 'master' : 'worker';
16+
}
17+
writeLine() {
18+
this._write('info', Array.prototype.slice.apply(arguments));
19+
}
20+
21+
writeError() {
22+
this._write('error', Array.prototype.slice.apply(arguments));
23+
}
24+
25+
_write(level, args) {
26+
args[0] = `[${level}][${this.prefix}] ${args[0]}`;
27+
console.log.apply(console, args);
28+
}
29+
}
30+
31+
function writeAppInitializedWhenReady(logger) {
32+
let timeout;
33+
34+
timeout = setInterval(function() {
35+
logger.writeLine('waiting backend');
36+
if (fs.existsSync('/tmp/backend-initialized')) {
37+
logger.writeLine('backend is up. let heroku know the app is ready');
38+
fs.writeFileSync('/tmp/app-initialized', 'hello');
39+
clearInterval(timeout);
40+
} else {
41+
logger.writeLine('backend is still not up');
42+
}
43+
}, 1000);
44+
}
45+
46+
var logger = new LoggerWithoutTimestamp();
47+
48+
logger.writeLine(`${os.cpus().length} cores available`);
49+
50+
let workerCount = process.env.WEB_CONCURRENCY || 1;
51+
52+
let server = new FastBootAppServer({
53+
distPath: 'dist',
54+
port: 9000,
55+
ui: logger,
56+
workerCount: workerCount,
57+
});
58+
59+
if (!cluster.isWorker) {
60+
writeAppInitializedWhenReady(logger);
61+
}
62+
63+
server.start();

0 commit comments

Comments
 (0)