2014-04-07 00:33:49 +00:00
|
|
|
var http = require("http");
|
2016-10-30 14:01:21 +00:00
|
|
|
var compression = require("compression");
|
2014-04-09 23:58:14 +00:00
|
|
|
var domain = require("domain");
|
2016-10-11 20:28:36 +00:00
|
|
|
var Promise = require("promise");
|
2016-10-18 11:27:20 +00:00
|
|
|
var express = require("express");
|
|
|
|
var path = require("path");
|
2017-02-09 19:39:42 +00:00
|
|
|
var webpack = require("webpack");
|
|
|
|
var webpackConfig = require("../frontend/webpack.config");
|
|
|
|
var webpackMiddleware = require("webpack-dev-middleware");
|
2016-10-30 14:01:21 +00:00
|
|
|
|
|
|
|
var config = require("../config");
|
|
|
|
var Database = require("./database/database");
|
|
|
|
var utils = require("./utils");
|
|
|
|
var Socket = require("./socket");
|
2016-10-18 11:27:20 +00:00
|
|
|
|
|
|
|
var frontendPath = path.resolve(__dirname + "/../frontend");
|
2014-04-07 00:33:49 +00:00
|
|
|
|
2014-12-14 03:43:05 +00:00
|
|
|
Object.defineProperty(Error.prototype, "toJSON", {
|
|
|
|
value: function() {
|
2014-12-27 02:47:40 +00:00
|
|
|
var str = this.message;
|
|
|
|
if(this.errors) {
|
|
|
|
for(var i=0; i<this.errors.length; i++)
|
|
|
|
str += "\n"+this.errors[i].message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2014-12-14 03:43:05 +00:00
|
|
|
},
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
|
2016-10-30 14:01:21 +00:00
|
|
|
process.on('unhandledRejection', (reason, promise) => {
|
|
|
|
console.trace("Unhandled rejection", reason);
|
2016-10-18 11:27:20 +00:00
|
|
|
});
|
|
|
|
|
2016-10-30 14:01:21 +00:00
|
|
|
utils.promiseAuto({
|
|
|
|
database: () => new Database(),
|
2016-10-18 11:27:20 +00:00
|
|
|
|
2016-10-30 14:01:21 +00:00
|
|
|
databaseConnect: database => database.connect(),
|
2014-04-18 18:11:50 +00:00
|
|
|
|
2016-10-30 14:01:21 +00:00
|
|
|
server: () => {
|
|
|
|
var app = express();
|
|
|
|
app.use(compression());
|
2017-02-09 19:13:45 +00:00
|
|
|
|
|
|
|
app.get("/bundle-:hash.js", function(req, res, next) {
|
|
|
|
res.setHeader('Cache-Control', 'public, max-age=31557600'); // one year
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2017-02-09 19:39:42 +00:00
|
|
|
var staticMiddleware = process.env.FM_DEV
|
|
|
|
? webpackMiddleware(webpack(webpackConfig), {
|
|
|
|
publicPath: "/"
|
|
|
|
})
|
|
|
|
: express.static(frontendPath + "/build/");
|
|
|
|
|
|
|
|
app.use(staticMiddleware);
|
|
|
|
|
|
|
|
app.get("/:padId", function(req, res, next) {
|
|
|
|
req.url = req.url.replace(/[^\/]*$/, "");
|
|
|
|
req.originalUrl = req.originalUrl.replace(/[^\/]*$/, "");
|
|
|
|
|
|
|
|
staticMiddleware(req, res, next);
|
2016-03-04 19:40:02 +00:00
|
|
|
});
|
|
|
|
|
2016-10-30 14:01:21 +00:00
|
|
|
var server = http.createServer(app);
|
|
|
|
return Promise.denodeify(server.listen.bind(server))(config.port, config.host).then(() => server);
|
|
|
|
},
|
2014-04-07 00:33:49 +00:00
|
|
|
|
2016-10-30 14:01:21 +00:00
|
|
|
socket: (server, database) => {
|
|
|
|
return new Socket(server, database);
|
|
|
|
}
|
|
|
|
}).then(res => {
|
2016-10-11 20:28:36 +00:00
|
|
|
console.log("Server started on " + (config.host || "*" ) + ":" + config.port);
|
2016-10-30 14:01:21 +00:00
|
|
|
}).catch(err => {
|
2016-10-11 20:28:36 +00:00
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
2016-10-30 14:01:21 +00:00
|
|
|
});
|