Add an index_map which can be used to redirect different hostnames to different app paths so that I can host multiple domains of the same device.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4653 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-12-03 17:03:17 +00:00
parent 1292775a75
commit 70db31bb8f

View File

@ -47,6 +47,11 @@ const k_global_settings = {
default_value: '/~core/apps/',
description: 'Default path.',
},
index_map: {
type: 'textarea',
default_value: undefined,
description: 'Mappings from hostname to redirect path, one per line, as in: "www.tildefriends.net=/~core/index/"',
},
room: {
type: 'boolean',
default_value: true,
@ -951,6 +956,17 @@ loadSettings().then(function() {
httpd.all("", function(request, response) {
let match;
if (request.uri === "/" || request.uri === "") {
try {
for (let line of (gGlobalSettings.index_map || '').split('\n')) {
let parts = line.split('=');
if (parts.length == 2 && request.headers.host.match(new RegExp(parts[0], 'i'))) {
response.writeHead(303, {"Location": (request.client.tls ? 'https://' : 'http://') + request.headers.host + parts[1], "Content-Length": "0"});
return response.end();
}
}
} catch (e) {
print(e);
}
response.writeHead(303, {"Location": (request.client.tls ? 'https://' : 'http://') + request.headers.host + gGlobalSettings.index, "Content-Length": "0"});
return response.end();
} else if (match = /^(\/~[^\/]+\/[^\/]+)(\/?.*)$/.exec(request.uri)) {