2023-03-18 00:40:54 +00:00
|
|
|
const Config = require('./config').get;
|
|
|
|
|
|
|
|
// deps
|
|
|
|
const { get, isString } = require('lodash');
|
|
|
|
|
|
|
|
exports.getWebDomain = getWebDomain;
|
2023-03-18 20:30:37 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-03-18 20:30:37 +00:00
|
|
|
|
|
|
|
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}`;
|
|
|
|
}
|