Skip to content

Commit 45ed646

Browse files
committed
Enable Fastboot and run its Node server from Procfile
1 parent 0fbde13 commit 45ed646

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
release: bin/diesel migration run
2-
web: bin/start-nginx ./target/release/server
2+
web: node fastboot.js & bin/start-nginx target/release/server & wait -n
33
background_worker: ./target/release/background-worker

config/environment.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ module.exports = function(environment) {
2222
// Here you can pass flags/options to your application instance
2323
// when it is created
2424
},
25+
26+
fastboot: {
27+
hostWhitelist: ['crates.io', /^localhost:\d+$/, /\.herokuapp\.com$/],
28+
},
2529
};
2630

2731
if (environment === 'development') {

config/nginx.conf.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ http {
6464
proxy_pass http://app_server;
6565
}
6666

67+
# Just in case, only forward "/policies" to Ember for a moment
68+
location = /policies {
69+
proxy_pass http://localhost:9000;
70+
}
71+
6772
location ~ ^/api/v./crates/new$ {
6873
proxy_pass http://app_server;
6974

fastboot.js

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

src/bin/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ fn main() {
102102
// Creating this file tells heroku to tell nginx that the application is ready
103103
// to receive traffic.
104104
if heroku {
105-
println!("Writing to /tmp/app-initialized");
106-
File::create("/tmp/app-initialized").unwrap();
105+
println!("Writing to /tmp/backend-initialized");
106+
File::create("/tmp/backend-initialized").unwrap();
107107
}
108108

109109
// Block the main thread until the server has shutdown

0 commit comments

Comments
 (0)