enigma-bbs/core/web_util.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-03-18 00:40:54 +00:00
const Config = require('./config').get;
// deps
const { get, isString } = require('lodash');
exports.getWebDomain = getWebDomain;
exports.getBaseUrl = getBaseUrl;
exports.getFullUrl = getFullUrl;
exports.buildUrl = buildUrl;
2023-03-18 00:40:54 +00:00
function getWebDomain() {
const config = Config();
const overridePrefix = get(config, 'contentServers.web.overrideUrlPrefix');
if (isString(overridePrefix)) {
const url = new URL(overridePrefix);
return url.hostname;
}
return config.contentServers.web.domain;
}
function getBaseUrl() {
const config = Config();
const overridePrefix = get(config, 'contentServers.web.overrideUrlPrefix');
if (overridePrefix) {
return overridePrefix;
}
let schema;
let 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}`;
}
return `${schema}${config.contentServers.web.domain}${port}`;
}
function getFullUrl(req) {
const base = getBaseUrl();
return new URL(`${base}${req.url}`);
}
function 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 = getBaseUrl();
return `${base}${pathAndQuery}`;
}