Skip to content
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

Enable FastBoot (again) #1832

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: node --optimize_for_size --max_old_space_size=200 fastboot.js & bin/start-nginx target/release/server & wait -n
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
4 changes: 4 additions & 0 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ 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
63 changes: 63 additions & 0 deletions fastboot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable no-console */
/* 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');

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 server = new FastBootAppServer({
distPath: 'dist',
port: 9000,
ui: logger,
workerCount: workerCount,
});

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
},
};
Loading