First pass formatting with Prettier
* Added .prettierrc.json * Added .prettierignore * Formatted
This commit is contained in:
@@ -2,46 +2,56 @@
|
||||
'use strict';
|
||||
|
||||
// ENiGMA½
|
||||
const Log = require('../../logger.js').log;
|
||||
const ServerModule = require('../../server_module.js').ServerModule;
|
||||
const Config = require('../../config.js').get;
|
||||
const { Errors } = require('../../enig_error.js');
|
||||
const Log = require('../../logger.js').log;
|
||||
const ServerModule = require('../../server_module.js').ServerModule;
|
||||
const Config = require('../../config.js').get;
|
||||
const { Errors } = require('../../enig_error.js');
|
||||
|
||||
// deps
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const _ = require('lodash');
|
||||
const fs = require('graceful-fs');
|
||||
const paths = require('path');
|
||||
const mimeTypes = require('mime-types');
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const _ = require('lodash');
|
||||
const fs = require('graceful-fs');
|
||||
const paths = require('path');
|
||||
const mimeTypes = require('mime-types');
|
||||
const forEachSeries = require('async/forEachSeries');
|
||||
|
||||
const ModuleInfo = exports.moduleInfo = {
|
||||
name : 'Web',
|
||||
desc : 'Web Server',
|
||||
author : 'NuSkooler',
|
||||
packageName : 'codes.l33t.enigma.web.server',
|
||||
};
|
||||
const ModuleInfo = (exports.moduleInfo = {
|
||||
name: 'Web',
|
||||
desc: 'Web Server',
|
||||
author: 'NuSkooler',
|
||||
packageName: 'codes.l33t.enigma.web.server',
|
||||
});
|
||||
|
||||
class Route {
|
||||
constructor(route) {
|
||||
Object.assign(this, route);
|
||||
|
||||
if(this.method) {
|
||||
if (this.method) {
|
||||
this.method = this.method.toUpperCase();
|
||||
}
|
||||
|
||||
try {
|
||||
this.pathRegExp = new RegExp(this.path);
|
||||
} catch(e) {
|
||||
Log.debug( { route : route }, 'Invalid regular expression for route path' );
|
||||
} catch (e) {
|
||||
Log.debug({ route: route }, 'Invalid regular expression for route path');
|
||||
}
|
||||
}
|
||||
|
||||
isValid() {
|
||||
return (
|
||||
this.pathRegExp instanceof RegExp &&
|
||||
( -1 !== [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', ].indexOf(this.method) ) ||
|
||||
(this.pathRegExp instanceof RegExp &&
|
||||
-1 !==
|
||||
[
|
||||
'GET',
|
||||
'HEAD',
|
||||
'POST',
|
||||
'PUT',
|
||||
'DELETE',
|
||||
'CONNECT',
|
||||
'OPTIONS',
|
||||
'TRACE',
|
||||
].indexOf(this.method)) ||
|
||||
!_.isFunction(this.handler)
|
||||
);
|
||||
}
|
||||
@@ -50,24 +60,26 @@ class Route {
|
||||
return req.method === this.method && this.pathRegExp.test(req.url);
|
||||
}
|
||||
|
||||
getRouteKey() { return `${this.method}:${this.path}`; }
|
||||
getRouteKey() {
|
||||
return `${this.method}:${this.path}`;
|
||||
}
|
||||
}
|
||||
|
||||
exports.getModule = class WebServerModule extends ServerModule {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
const config = Config();
|
||||
this.enableHttp = config.contentServers.web.http.enabled || false;
|
||||
this.enableHttps = config.contentServers.web.https.enabled || false;
|
||||
const config = Config();
|
||||
this.enableHttp = config.contentServers.web.http.enabled || false;
|
||||
this.enableHttps = config.contentServers.web.https.enabled || false;
|
||||
|
||||
this.routes = {};
|
||||
|
||||
if(this.isEnabled() && config.contentServers.web.staticRoot) {
|
||||
if (this.isEnabled() && config.contentServers.web.staticRoot) {
|
||||
this.addRoute({
|
||||
method : 'GET',
|
||||
path : '/static/.*$',
|
||||
handler : this.routeStaticFile.bind(this),
|
||||
method: 'GET',
|
||||
path: '/static/.*$',
|
||||
handler: this.routeStaticFile.bind(this),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -81,22 +93,24 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||
// only if non-standard. Allow users to override full prefix in config.
|
||||
//
|
||||
const config = Config();
|
||||
if(_.isString(config.contentServers.web.overrideUrlPrefix)) {
|
||||
if (_.isString(config.contentServers.web.overrideUrlPrefix)) {
|
||||
return `${config.contentServers.web.overrideUrlPrefix}${pathAndQuery}`;
|
||||
}
|
||||
|
||||
let schema;
|
||||
let port;
|
||||
if(config.contentServers.web.https.enabled) {
|
||||
schema = 'https://';
|
||||
port = (443 === config.contentServers.web.https.port) ?
|
||||
'' :
|
||||
`:${config.contentServers.web.https.port}`;
|
||||
if (config.contentServers.web.https.enabled) {
|
||||
schema = 'https://';
|
||||
port =
|
||||
443 === config.contentServers.web.https.port
|
||||
? ''
|
||||
: `:${config.contentServers.web.https.port}`;
|
||||
} else {
|
||||
schema = 'http://';
|
||||
port = (80 === config.contentServers.web.http.port) ?
|
||||
'' :
|
||||
`:${config.contentServers.web.http.port}`;
|
||||
schema = 'http://';
|
||||
port =
|
||||
80 === config.contentServers.web.http.port
|
||||
? ''
|
||||
: `:${config.contentServers.web.http.port}`;
|
||||
}
|
||||
|
||||
return `${schema}${config.contentServers.web.domain}${port}${pathAndQuery}`;
|
||||
@@ -107,21 +121,25 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||
}
|
||||
|
||||
createServer(cb) {
|
||||
if(this.enableHttp) {
|
||||
this.httpServer = http.createServer( (req, resp) => this.routeRequest(req, resp) );
|
||||
if (this.enableHttp) {
|
||||
this.httpServer = http.createServer((req, resp) =>
|
||||
this.routeRequest(req, resp)
|
||||
);
|
||||
}
|
||||
|
||||
const config = Config();
|
||||
if(this.enableHttps) {
|
||||
if (this.enableHttps) {
|
||||
const options = {
|
||||
cert : fs.readFileSync(config.contentServers.web.https.certPem),
|
||||
key : fs.readFileSync(config.contentServers.web.https.keyPem),
|
||||
cert: fs.readFileSync(config.contentServers.web.https.certPem),
|
||||
key: fs.readFileSync(config.contentServers.web.https.keyPem),
|
||||
};
|
||||
|
||||
// additional options
|
||||
Object.assign(options, config.contentServers.web.https.options || {} );
|
||||
Object.assign(options, config.contentServers.web.https.options || {});
|
||||
|
||||
this.httpsServer = https.createServer(options, (req, resp) => this.routeRequest(req, resp) );
|
||||
this.httpsServer = https.createServer(options, (req, resp) =>
|
||||
this.routeRequest(req, resp)
|
||||
);
|
||||
}
|
||||
|
||||
return cb(null);
|
||||
@@ -129,38 +147,61 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||
|
||||
listen(cb) {
|
||||
const config = Config();
|
||||
forEachSeries([ 'http', 'https' ], (service, nextService) => {
|
||||
const name = `${service}Server`;
|
||||
if(this[name]) {
|
||||
const port = parseInt(config.contentServers.web[service].port);
|
||||
if(isNaN(port)) {
|
||||
Log.warn( { port : config.contentServers.web[service].port, server : ModuleInfo.name }, `Invalid port (${service})` );
|
||||
return nextService(Errors.Invalid(`Invalid port: ${config.contentServers.web[service].port}`));
|
||||
}
|
||||
forEachSeries(
|
||||
['http', 'https'],
|
||||
(service, nextService) => {
|
||||
const name = `${service}Server`;
|
||||
if (this[name]) {
|
||||
const port = parseInt(config.contentServers.web[service].port);
|
||||
if (isNaN(port)) {
|
||||
Log.warn(
|
||||
{
|
||||
port: config.contentServers.web[service].port,
|
||||
server: ModuleInfo.name,
|
||||
},
|
||||
`Invalid port (${service})`
|
||||
);
|
||||
return nextService(
|
||||
Errors.Invalid(
|
||||
`Invalid port: ${config.contentServers.web[service].port}`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
this[name].listen(port, config.contentServers.web[service].address, err => {
|
||||
return nextService(err);
|
||||
});
|
||||
} else {
|
||||
return nextService(null);
|
||||
this[name].listen(
|
||||
port,
|
||||
config.contentServers.web[service].address,
|
||||
err => {
|
||||
return nextService(err);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
return nextService(null);
|
||||
}
|
||||
},
|
||||
err => {
|
||||
return cb(err);
|
||||
}
|
||||
},
|
||||
err => {
|
||||
return cb(err);
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
addRoute(route) {
|
||||
route = new Route(route);
|
||||
|
||||
if(!route.isValid()) {
|
||||
Log.warn( { route : route }, 'Cannot add route: missing or invalid required members' );
|
||||
if (!route.isValid()) {
|
||||
Log.warn(
|
||||
{ route: route },
|
||||
'Cannot add route: missing or invalid required members'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
const routeKey = route.getRouteKey();
|
||||
if(routeKey in this.routes) {
|
||||
Log.warn( { route : route, routeKey : routeKey }, 'Cannot add route: duplicate method/path combination exists' );
|
||||
if (routeKey in this.routes) {
|
||||
Log.warn(
|
||||
{ route: route, routeKey: routeKey },
|
||||
'Cannot add route: duplicate method/path combination exists'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -169,9 +210,9 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||
}
|
||||
|
||||
routeRequest(req, resp) {
|
||||
const route = _.find(this.routes, r => r.matchesRequest(req) );
|
||||
const route = _.find(this.routes, r => r.matchesRequest(req));
|
||||
|
||||
if(!route && '/' === req.url) {
|
||||
if (!route && '/' === req.url) {
|
||||
return this.routeIndex(req, resp);
|
||||
}
|
||||
|
||||
@@ -179,12 +220,15 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||
}
|
||||
|
||||
respondWithError(resp, code, bodyText, title) {
|
||||
const customErrorPage = paths.join(Config().contentServers.web.staticRoot, `${code}.html`);
|
||||
const customErrorPage = paths.join(
|
||||
Config().contentServers.web.staticRoot,
|
||||
`${code}.html`
|
||||
);
|
||||
|
||||
fs.readFile(customErrorPage, 'utf8', (err, data) => {
|
||||
resp.writeHead(code, { 'Content-Type' : 'text/html' } );
|
||||
resp.writeHead(code, { 'Content-Type': 'text/html' });
|
||||
|
||||
if(err) {
|
||||
if (err) {
|
||||
return resp.end(`<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
@@ -197,8 +241,7 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||
<h2>${bodyText}</h2>
|
||||
</article>
|
||||
</body>
|
||||
</html>`
|
||||
);
|
||||
</html>`);
|
||||
}
|
||||
|
||||
return resp.end(data);
|
||||
@@ -232,13 +275,15 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||
}
|
||||
|
||||
fs.stat(filePath, (err, stats) => {
|
||||
if(err || !stats.isFile()) {
|
||||
if (err || !stats.isFile()) {
|
||||
return self.fileNotFound(resp);
|
||||
}
|
||||
|
||||
const headers = {
|
||||
'Content-Type' : mimeTypes.contentType(paths.basename(filePath)) || mimeTypes.contentType('.bin'),
|
||||
'Content-Length' : stats.size,
|
||||
'Content-Type':
|
||||
mimeTypes.contentType(paths.basename(filePath)) ||
|
||||
mimeTypes.contentType('.bin'),
|
||||
'Content-Length': stats.size,
|
||||
};
|
||||
|
||||
const readStream = fs.createReadStream(filePath);
|
||||
@@ -259,18 +304,23 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||
const self = this;
|
||||
|
||||
fs.readFile(templatePath, 'utf8', (err, templateData) => {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return self.fileNotFound(resp);
|
||||
}
|
||||
|
||||
preprocessCallback(templateData, (err, finalPage, contentType) => {
|
||||
if(err || !finalPage) {
|
||||
return self.respondWithError(resp, 500, 'Internal Server Error.', 'Internal Server Error');
|
||||
if (err || !finalPage) {
|
||||
return self.respondWithError(
|
||||
resp,
|
||||
500,
|
||||
'Internal Server Error.',
|
||||
'Internal Server Error'
|
||||
);
|
||||
}
|
||||
|
||||
const headers = {
|
||||
'Content-Type' : contentType || mimeTypes.contentType('.html'),
|
||||
'Content-Length' : finalPage.length,
|
||||
'Content-Type': contentType || mimeTypes.contentType('.html'),
|
||||
'Content-Length': finalPage.length,
|
||||
};
|
||||
|
||||
resp.writeHead(200, headers);
|
||||
|
||||
Reference in New Issue
Block a user