2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const Log = require('../../logger.js').log;
|
|
|
|
const ServerModule = require('../../server_module.js').ServerModule;
|
|
|
|
const Config = require('../../config.js').get;
|
2023-01-06 19:15:29 +00:00
|
|
|
const { Errors } = require('../../enig_error.js');
|
2023-01-03 22:10:39 +00:00
|
|
|
const { loadModulesForCategory, moduleCategories } = require('../../module_util');
|
|
|
|
const WebHandlerModule = require('../../web_handler_module');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
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');
|
2018-12-27 09:46:16 +00:00
|
|
|
const forEachSeries = require('async/forEachSeries');
|
2022-12-29 07:56:10 +00:00
|
|
|
const findSeries = require('async/findSeries');
|
2023-02-02 06:02:33 +00:00
|
|
|
const WebLog = require('../../web_log.js');
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const ModuleInfo = (exports.moduleInfo = {
|
|
|
|
name: 'Web',
|
|
|
|
desc: 'Web Server',
|
|
|
|
author: 'NuSkooler',
|
|
|
|
packageName: 'codes.l33t.enigma.web.server',
|
|
|
|
});
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2022-12-31 22:30:54 +00:00
|
|
|
exports.WellKnownLocations = {
|
2023-01-02 02:19:51 +00:00
|
|
|
Rfc5785: '/.well-known', // https://www.rfc-editor.org/rfc/rfc5785
|
|
|
|
Internal: '/_enig', // location of most enigma provided routes
|
2022-12-31 22:30:54 +00:00
|
|
|
};
|
|
|
|
|
2016-10-25 03:49:45 +00:00
|
|
|
class Route {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(route) {
|
|
|
|
Object.assign(this, route);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.method) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.method = this.method.toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.pathRegExp = new RegExp(this.path);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2023-01-07 16:50:16 +00:00
|
|
|
this.log.error({ route: route }, 'Invalid regular expression for route path');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
return (
|
2022-06-05 20:04:25 +00:00
|
|
|
(this.pathRegExp instanceof RegExp &&
|
2023-01-06 20:49:13 +00:00
|
|
|
-1 !==
|
|
|
|
[
|
|
|
|
'GET',
|
|
|
|
'HEAD',
|
|
|
|
'POST',
|
|
|
|
'PUT',
|
|
|
|
'DELETE',
|
|
|
|
'CONNECT',
|
|
|
|
'OPTIONS',
|
|
|
|
'TRACE',
|
|
|
|
].indexOf(this.method)) ||
|
|
|
|
!_.isFunction(this.handler)
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
matchesRequest(req) {
|
|
|
|
return req.method === this.method && this.pathRegExp.test(req.url);
|
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
getRouteKey() {
|
|
|
|
return `${this.method}:${this.path}`;
|
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.getModule = class WebServerModule extends ServerModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2023-02-02 06:02:33 +00:00
|
|
|
//this.log = Log.child({ server: 'Web' });
|
|
|
|
this.log = WebLog.createWebLog();
|
2023-01-07 16:50:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const config = Config();
|
|
|
|
this.enableHttp = config.contentServers.web.http.enabled || false;
|
|
|
|
this.enableHttps = config.contentServers.web.https.enabled || false;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this.routes = {};
|
|
|
|
}
|
|
|
|
|
2023-01-07 16:50:16 +00:00
|
|
|
logger() {
|
|
|
|
return this.log;
|
|
|
|
}
|
|
|
|
|
2022-12-31 22:30:54 +00:00
|
|
|
getDomain() {
|
|
|
|
const config = Config();
|
2023-01-28 18:55:31 +00:00
|
|
|
const overridePrefix = _.get(config, 'contentServers.web.overrideUrlPrefix');
|
2022-12-31 22:30:54 +00:00
|
|
|
if (_.isString(overridePrefix)) {
|
|
|
|
const url = new URL(overridePrefix);
|
|
|
|
return url.hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.contentServers.web.domain;
|
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
baseUrl() {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
2023-01-28 18:55:31 +00:00
|
|
|
const overridePrefix = _.get(config, 'contentServers.web.overrideUrlPrefix');
|
|
|
|
if (overridePrefix) {
|
|
|
|
return overridePrefix;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let schema;
|
|
|
|
let port;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (config.contentServers.web.https.enabled) {
|
|
|
|
schema = 'https://';
|
|
|
|
port =
|
2023-01-06 20:49:13 +00:00
|
|
|
443 === config.contentServers.web.https.port
|
|
|
|
? ''
|
|
|
|
: `:${config.contentServers.web.https.port}`;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
schema = 'http://';
|
|
|
|
port =
|
2023-01-06 20:49:13 +00:00
|
|
|
80 === config.contentServers.web.http.port
|
|
|
|
? ''
|
|
|
|
: `:${config.contentServers.web.http.port}`;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 18:55:31 +00:00
|
|
|
return `${schema}${config.contentServers.web.domain}${port}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
fullUrl(req) {
|
|
|
|
const base = this.baseUrl();
|
|
|
|
return new URL(`${base}${req.url}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
buildUrl(pathAndQuery) {
|
|
|
|
//
|
|
|
|
// Create a URL such as
|
|
|
|
// https://l33t.codes:44512/ + |pathAndQuery|
|
|
|
|
//
|
|
|
|
// Prefer HTTPS over HTTP. Be explicit about the port
|
|
|
|
// only if non-standard. Allow users to override full prefix in config.
|
|
|
|
//
|
|
|
|
const base = this.baseUrl();
|
|
|
|
return `${base}${pathAndQuery}`;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isEnabled() {
|
|
|
|
return this.enableHttp || this.enableHttps;
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:19:26 +00:00
|
|
|
createServer(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.enableHttp) {
|
|
|
|
this.httpServer = http.createServer((req, resp) =>
|
|
|
|
this.routeRequest(req, resp)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.enableHttps) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const options = {
|
2022-06-05 20:04:25 +00:00
|
|
|
cert: fs.readFileSync(config.contentServers.web.https.certPem),
|
|
|
|
key: fs.readFileSync(config.contentServers.web.https.keyPem),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// additional options
|
2022-06-05 20:04:25 +00:00
|
|
|
Object.assign(options, config.contentServers.web.https.options || {});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.httpsServer = https.createServer(options, (req, resp) =>
|
|
|
|
this.routeRequest(req, resp)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-12-27 09:19:26 +00:00
|
|
|
|
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-31 05:35:18 +00:00
|
|
|
beforeListen(cb) {
|
|
|
|
if (!this.isEnabled()) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadModulesForCategory(
|
2022-12-31 22:30:54 +00:00
|
|
|
moduleCategories.WebHandlers,
|
2022-12-31 05:35:18 +00:00
|
|
|
(module, nextModule) => {
|
|
|
|
const moduleInst = new module.getModule();
|
|
|
|
try {
|
2023-01-03 22:10:39 +00:00
|
|
|
const normalizedName = _.camelCase(module.moduleInfo.name);
|
|
|
|
if (!WebHandlerModule.isEnabled(normalizedName)) {
|
2023-01-07 16:50:16 +00:00
|
|
|
this.log.info(
|
2023-01-06 20:49:13 +00:00
|
|
|
{ moduleName: normalizedName },
|
2023-01-07 16:50:16 +00:00
|
|
|
'Web handler module not enabled'
|
2023-01-06 20:49:13 +00:00
|
|
|
);
|
2023-01-03 22:10:39 +00:00
|
|
|
return nextModule(null);
|
|
|
|
}
|
|
|
|
|
2023-01-06 20:49:13 +00:00
|
|
|
Log.info(
|
|
|
|
{ moduleName: normalizedName },
|
2023-01-07 16:50:16 +00:00
|
|
|
'Initializing web handler module'
|
2023-01-06 20:49:13 +00:00
|
|
|
);
|
2023-01-06 19:15:29 +00:00
|
|
|
|
2023-01-07 16:50:16 +00:00
|
|
|
moduleInst.init(this, err => {
|
2022-12-31 05:35:18 +00:00
|
|
|
return nextModule(err);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2023-01-07 16:50:16 +00:00
|
|
|
this.log.error(
|
|
|
|
{ error: e.message },
|
|
|
|
'Exception caught loading web handler'
|
|
|
|
);
|
2022-12-31 05:35:18 +00:00
|
|
|
return nextModule(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:46:16 +00:00
|
|
|
listen(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
forEachSeries(
|
|
|
|
['http', 'https'],
|
|
|
|
(service, nextService) => {
|
|
|
|
const name = `${service}Server`;
|
|
|
|
if (this[name]) {
|
|
|
|
const port = parseInt(config.contentServers.web[service].port);
|
|
|
|
if (isNaN(port)) {
|
2023-01-07 16:50:16 +00:00
|
|
|
this.log.error(
|
2022-06-05 20:04:25 +00:00
|
|
|
{
|
|
|
|
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);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addRoute(route) {
|
|
|
|
route = new Route(route);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!route.isValid()) {
|
2023-01-07 16:50:16 +00:00
|
|
|
this.log.error(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ route: route },
|
|
|
|
'Cannot add route: missing or invalid required members'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const routeKey = route.getRouteKey();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (routeKey in this.routes) {
|
2023-01-07 16:50:16 +00:00
|
|
|
this.log.warn(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ route: route, routeKey: routeKey },
|
|
|
|
'Cannot add route: duplicate method/path combination exists'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.routes[routeKey] = route;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
routeRequest(req, resp) {
|
2023-02-02 06:02:33 +00:00
|
|
|
this.log.trace({ req }, 'Request');
|
2023-01-07 16:50:16 +00:00
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
let route = _.find(this.routes, r => r.matchesRequest(req));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
if (route) {
|
|
|
|
return route.handler(req, resp);
|
|
|
|
} else {
|
|
|
|
this.tryStaticRoute(req, resp, wasHandled => {
|
|
|
|
if (!wasHandled) {
|
|
|
|
this.tryRouteIndex(req, resp, wasHandled => {
|
|
|
|
if (!wasHandled) {
|
|
|
|
return this.fileNotFound(resp);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
respondWithError(resp, code, bodyText, title) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const customErrorPage = paths.join(
|
|
|
|
Config().contentServers.web.staticRoot,
|
|
|
|
`${code}.html`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
fs.readFile(customErrorPage, 'utf8', (err, data) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
resp.writeHead(code, { 'Content-Type': 'text/html' });
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return resp.end(`<!doctype html>
|
2018-06-23 03:26:46 +00:00
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>${title}</title>
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article>
|
|
|
|
<h2>${bodyText}</h2>
|
|
|
|
</article>
|
|
|
|
</body>
|
2022-06-05 20:04:25 +00:00
|
|
|
</html>`);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp.end(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-29 23:52:01 +00:00
|
|
|
created(resp, body = '', headers = { 'Content-Type:': 'text/html' }) {
|
|
|
|
resp.writeHead(201, 'Created', body ? headers : null);
|
|
|
|
return resp.end(body);
|
|
|
|
}
|
|
|
|
|
2023-01-26 05:22:45 +00:00
|
|
|
accepted(resp, body = '', headers = { 'Content-Type:': 'text/html' }) {
|
|
|
|
resp.writeHead(202, 'Accepted', body ? headers : null);
|
|
|
|
return resp.end(body);
|
|
|
|
}
|
|
|
|
|
2023-01-08 08:22:02 +00:00
|
|
|
badRequest(resp) {
|
|
|
|
return this.respondWithError(resp, 400, 'Bad request.', 'Bad Request');
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
accessDenied(resp) {
|
|
|
|
return this.respondWithError(resp, 401, 'Access denied.', 'Access Denied');
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNotFound(resp) {
|
|
|
|
return this.respondWithError(resp, 404, 'File not found.', 'File Not Found');
|
|
|
|
}
|
|
|
|
|
2023-01-08 08:22:02 +00:00
|
|
|
resourceNotFound(resp) {
|
|
|
|
return this.respondWithError(
|
|
|
|
resp,
|
|
|
|
404,
|
|
|
|
'Resource not found.',
|
|
|
|
'Resource Not Found'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-21 05:15:59 +00:00
|
|
|
internalServerError(resp, err) {
|
|
|
|
if (err) {
|
|
|
|
this.log.error({ error: err.message }, 'Internal server error');
|
|
|
|
}
|
2023-01-08 08:22:02 +00:00
|
|
|
return this.respondWithError(
|
|
|
|
resp,
|
|
|
|
500,
|
|
|
|
'Internal server error.',
|
|
|
|
'Internal Server Error'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-21 08:19:19 +00:00
|
|
|
notImplemented(resp) {
|
|
|
|
return this.respondWithError(resp, 501, 'Not implemented.', 'Not Implemented');
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
tryRouteIndex(req, resp, cb) {
|
|
|
|
const tryFiles = Config().contentServers.web.tryFiles || [
|
|
|
|
'index.html',
|
|
|
|
'index.htm',
|
|
|
|
];
|
|
|
|
|
|
|
|
findSeries(
|
|
|
|
tryFiles,
|
|
|
|
(tryFile, nextTryFile) => {
|
|
|
|
const fileName = paths.join(
|
|
|
|
req.url.substr(req.url.lastIndexOf('/', 1)),
|
|
|
|
tryFile
|
|
|
|
);
|
|
|
|
|
2023-01-05 03:29:18 +00:00
|
|
|
const filePath = this.resolveStaticPath(fileName);
|
2022-12-29 07:56:10 +00:00
|
|
|
fs.stat(filePath, (err, stats) => {
|
|
|
|
if (err || !stats.isFile()) {
|
|
|
|
return nextTryFile(null, false);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
const headers = {
|
|
|
|
'Content-Type':
|
2023-01-06 20:49:13 +00:00
|
|
|
mimeTypes.contentType(paths.basename(filePath)) ||
|
|
|
|
mimeTypes.contentType('.bin'),
|
2022-12-29 07:56:10 +00:00
|
|
|
'Content-Length': stats.size,
|
|
|
|
};
|
|
|
|
|
|
|
|
const readStream = fs.createReadStream(filePath);
|
|
|
|
resp.writeHead(200, headers);
|
|
|
|
readStream.pipe(resp);
|
|
|
|
|
|
|
|
return nextTryFile(null, true);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(_, wasHandled) => {
|
|
|
|
return cb(wasHandled);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
tryStaticRoute(req, resp, cb) {
|
|
|
|
const fileName = req.url.substr(req.url.lastIndexOf('/', 1));
|
|
|
|
const filePath = this.resolveStaticPath(fileName);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-11-27 19:50:57 +00:00
|
|
|
if (!filePath) {
|
2022-12-29 07:56:10 +00:00
|
|
|
return cb(false);
|
2020-11-27 19:50:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
fs.stat(filePath, (err, stats) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err || !stats.isFile()) {
|
2022-12-29 07:56:10 +00:00
|
|
|
return cb(false);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const headers = {
|
2022-06-05 20:04:25 +00:00
|
|
|
'Content-Type':
|
2023-01-06 20:49:13 +00:00
|
|
|
mimeTypes.contentType(paths.basename(filePath)) ||
|
|
|
|
mimeTypes.contentType('.bin'),
|
2022-06-05 20:04:25 +00:00
|
|
|
'Content-Length': stats.size,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const readStream = fs.createReadStream(filePath);
|
|
|
|
resp.writeHead(200, headers);
|
2022-12-29 07:56:10 +00:00
|
|
|
readStream.pipe(resp);
|
|
|
|
|
|
|
|
return cb(true);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:50:57 +00:00
|
|
|
resolveStaticPath(requestPath) {
|
|
|
|
const staticRoot = _.get(Config(), 'contentServers.web.staticRoot');
|
|
|
|
const path = paths.resolve(staticRoot, `.${requestPath}`);
|
|
|
|
if (path.startsWith(staticRoot)) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 02:19:51 +00:00
|
|
|
resolveTemplatePath(path) {
|
|
|
|
if (paths.isAbsolute(path)) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const staticRoot = _.get(Config(), 'contentServers.web.staticRoot');
|
|
|
|
const resolved = paths.resolve(staticRoot, path);
|
|
|
|
if (resolved.startsWith(staticRoot)) {
|
|
|
|
return resolved;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
routeTemplateFilePage(templatePath, preprocessCallback, resp) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
fs.readFile(templatePath, 'utf8', (err, templateData) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return self.fileNotFound(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
preprocessCallback(templateData, (err, finalPage, contentType) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err || !finalPage) {
|
|
|
|
return self.respondWithError(
|
|
|
|
resp,
|
|
|
|
500,
|
|
|
|
'Internal Server Error.',
|
|
|
|
'Internal Server Error'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const headers = {
|
2022-06-05 20:04:25 +00:00
|
|
|
'Content-Type': contentType || mimeTypes.contentType('.html'),
|
|
|
|
'Content-Length': finalPage.length,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
resp.writeHead(200, headers);
|
|
|
|
return resp.end(finalPage);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-02-04 16:20:36 +00:00
|
|
|
};
|