Move webserver code to separate file

pull/108/head
Candid Dauth 2017-02-09 23:55:56 +01:00
rodzic 892ddec73f
commit 7287e8b48f
2 zmienionych plików z 50 dodań i 43 usunięć

Wyświetl plik

@ -1,19 +1,8 @@
var http = require("http");
var compression = require("compression");
var domain = require("domain");
var Promise = require("promise");
var express = require("express");
var path = require("path");
var webpack = require("webpack");
var webpackConfig = require("../frontend/webpack.config");
var webpackMiddleware = require("webpack-dev-middleware");
var config = require("../config"); var config = require("../config");
var Database = require("./database/database"); var Database = require("./database/database");
var utils = require("./utils"); var utils = require("./utils");
var Socket = require("./socket"); var Socket = require("./socket");
var webserver = require("./webserver");
var frontendPath = path.resolve(__dirname + "/../frontend");
Object.defineProperty(Error.prototype, "toJSON", { Object.defineProperty(Error.prototype, "toJSON", {
value: function() { value: function() {
@ -32,41 +21,16 @@ process.on('unhandledRejection', (reason, promise) => {
console.trace("Unhandled rejection", reason); console.trace("Unhandled rejection", reason);
}); });
const database = new Database();
utils.promiseAuto({ utils.promiseAuto({
database: () => new Database(), databaseConnect: database.connect(),
databaseConnect: database => database.connect(), server: (databaseConnect) => {
return webserver.init();
server: () => {
var app = express();
app.use(compression());
app.get("/bundle-:hash.js", function(req, res, next) {
res.setHeader('Cache-Control', 'public, max-age=31557600'); // one year
next();
});
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);
});
var server = http.createServer(app);
return Promise.denodeify(server.listen.bind(server))(config.port, config.host).then(() => server);
}, },
socket: (server, database) => { socket: (server) => {
return new Socket(server, database); return new Socket(server, database);
} }
}).then(res => { }).then(res => {

Wyświetl plik

@ -0,0 +1,43 @@
const compression = require("compression");
const express = require("express");
const http = require("http");
const path = require("path");
const Promise = require("promise");
const webpack = require("webpack");
const webpackMiddleware = require("webpack-dev-middleware");
const config = require("../config");
const webpackConfig = require("../frontend/webpack.config");
const frontendPath = path.resolve(__dirname + "/../frontend");
const staticMiddleware = process.env.FM_DEV ?
webpackMiddleware(webpack(webpackConfig), {
publicPath: "/"
}) :
express.static(frontendPath + "/build/");
const webserver = module.exports = {
init() {
let app = express();
app.use(compression());
app.get("/bundle-:hash.js", function(req, res, next) {
res.setHeader('Cache-Control', 'public, max-age=31557600'); // one year
next();
});
app.use(staticMiddleware);
app.get("/:padId", function(req, res, next) {
req.url = req.url.replace(/[^\/]*$/, "");
req.originalUrl = req.originalUrl.replace(/[^\/]*$/, "");
staticMiddleware(req, res, next);
});
let server = http.createServer(app);
return Promise.denodeify(server.listen.bind(server))(config.port, config.host).then(() => server);
}
};