Skip to content

Enable FastBoot #1715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 5, 2019
Merged
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
'.eslintrc.js',
'.template-lintrc.js',
'ember-cli-build.js',
'fastboot.js',
'testem.js',
'blueprints/*/index.js',
'config/**/*.js',
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
release: bin/diesel migration run
web: bin/start-nginx ./target/release/server
web: bin/start-nginx npm run nf -- --procfile foreman-procfile start --raw
background_worker: ./target/release/background-worker
4 changes: 3 additions & 1 deletion app/styles/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ body {
@include align-items(center);
}

.ember-application > div {
/* .ember-application is added by Ember after initial rendering */
.ember-application > div,
body > div {
width: 960px;
@media only screen and (max-width: 960px) {
width: 100%;
Expand Down
3 changes: 3 additions & 0 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module.exports = function(environment) {
// Here you can pass flags/options to your application instance
// when it is created
},
fastboot: {
hostWhitelist: ['crates.io', /^localhost:\d+$/, /\.herokuapp\.com$/],
},
};

if (environment === 'development') {
Expand Down
5 changes: 5 additions & 0 deletions config/nginx.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ http {
proxy_pass http://app_server;
}

# Just in case, only forward "/policies" to Ember for a moment
location = /policies {
proxy_pass http://localhost:9000;
}

location ~ ^/api/v./crates/new$ {
proxy_pass http://app_server;

Expand Down
56 changes: 56 additions & 0 deletions fastboot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable no-console */

'use strict';

const fs = require('fs');
const FastBootAppServer = require('fastboot-app-server');

// because fastboot-app-server uses cluster, but it might change in future
const cluster = require('cluster');

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();

let server = new FastBootAppServer({
distPath: 'dist',
port: 9000,
ui: logger,
});

if (!cluster.isWorker) {
writeAppInitializedWhenReady(logger);
}

server.start();
8 changes: 8 additions & 0 deletions fastboot/initializers/ajax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
name: 'ajax-service',
initialize() {
// This is to override Fastboot's initializer which prevents ember-fetch from working
// https://github.com/ember-fastboot/ember-cli-fastboot/blob/master/fastboot/initializers/ajax.js
// https://github.com/ember-cli/ember-fetch#ajax-service
},
};
2 changes: 2 additions & 0 deletions foreman-procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ember: node fastboot.js
api: ./target/release/server
Loading