133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
export type DomainRoutingEnv = Record<string, string | undefined> & {
|
|
APP_BASE_DOMAIN?: string;
|
|
WEB_APP_HOST?: string;
|
|
ADMIN_APP_HOST?: string;
|
|
API_HOST?: string;
|
|
PORTAL_APP_HOST?: string;
|
|
APP_PROTOCOL?: string;
|
|
NODE_ENV?: string;
|
|
};
|
|
|
|
export type DomainRoutingConfig = {
|
|
appBaseDomain: string;
|
|
webAppHost: string;
|
|
adminAppHost: string;
|
|
apiHost: string;
|
|
portalAppHost: string;
|
|
protocol: "http" | "https";
|
|
};
|
|
|
|
const LOCAL_BASE_DOMAIN = "localhost";
|
|
const LOCAL_WEB_APP_HOST = "app.localhost";
|
|
const LOCAL_ADMIN_APP_HOST = "admin.localhost";
|
|
const LOCAL_API_HOST = "api.localhost";
|
|
const LOCAL_PORTAL_APP_HOST = "portal.localhost";
|
|
|
|
export function getDomainRoutingConfig(env: DomainRoutingEnv = process.env): DomainRoutingConfig {
|
|
const rawBaseDomain = normalizeHost(env.APP_BASE_DOMAIN) ?? LOCAL_BASE_DOMAIN;
|
|
const protocol = normalizeProtocol(env.APP_PROTOCOL) ?? (isLocalHost(rawBaseDomain) || env.NODE_ENV === "development" ? "http" : "https");
|
|
const appBaseDomain = normalizeHostForProtocol(rawBaseDomain, protocol) ?? rawBaseDomain;
|
|
const derivedHosts = getDerivedHosts(appBaseDomain);
|
|
|
|
return {
|
|
appBaseDomain,
|
|
webAppHost: normalizeHostForProtocol(env.WEB_APP_HOST, protocol) ?? derivedHosts.webAppHost,
|
|
adminAppHost: normalizeHostForProtocol(env.ADMIN_APP_HOST, protocol) ?? derivedHosts.adminAppHost,
|
|
apiHost: normalizeHostForProtocol(env.API_HOST, protocol) ?? derivedHosts.apiHost,
|
|
portalAppHost: normalizeHostForProtocol(env.PORTAL_APP_HOST, protocol) ?? derivedHosts.portalAppHost,
|
|
protocol,
|
|
};
|
|
}
|
|
|
|
export function normalizeHost(value: string | undefined): string | undefined {
|
|
const trimmed = value?.trim();
|
|
if (!trimmed) {
|
|
return undefined;
|
|
}
|
|
|
|
try {
|
|
const withProtocol = /^[a-z][a-z\d+\-.]*:\/\//i.test(trimmed) ? trimmed : `http://${trimmed}`;
|
|
const url = new URL(withProtocol);
|
|
return url.host.toLowerCase();
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function normalizeHostForProtocol(
|
|
value: string | undefined,
|
|
protocol: DomainRoutingConfig["protocol"]
|
|
): string | undefined {
|
|
const host = normalizeHost(value);
|
|
if (!host) {
|
|
return undefined;
|
|
}
|
|
|
|
try {
|
|
return new URL(`${protocol}://${host}`).host.toLowerCase();
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function hostMatches(actualHost: string | undefined, expectedHost: string | undefined): boolean {
|
|
const actual = normalizeHost(actualHost);
|
|
const expected = normalizeHost(expectedHost);
|
|
if (!actual || !expected) {
|
|
return false;
|
|
}
|
|
if (actual === expected) {
|
|
return true;
|
|
}
|
|
|
|
const actualUrl = new URL(`http://${actual}`);
|
|
const expectedUrl = new URL(`http://${expected}`);
|
|
const bothLocal = isLocalHost(actualUrl.hostname) && isLocalHost(expectedUrl.hostname);
|
|
if (!bothLocal || actualUrl.hostname !== expectedUrl.hostname) {
|
|
return false;
|
|
}
|
|
|
|
return !actualUrl.port || !expectedUrl.port;
|
|
}
|
|
|
|
function normalizeProtocol(value: string | undefined): DomainRoutingConfig["protocol"] | undefined {
|
|
if (value === "http" || value === "https") {
|
|
return value;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function isLocalHost(host: string) {
|
|
const hostname = host.split(":")[0];
|
|
return hostname === "localhost" || hostname.endsWith(".localhost") || hostname === "127.0.0.1" || hostname === "::1";
|
|
}
|
|
|
|
function getDerivedHosts(baseDomain: string) {
|
|
if (isLocalHost(baseDomain)) {
|
|
return {
|
|
webAppHost: withLocalPort(LOCAL_WEB_APP_HOST, baseDomain),
|
|
adminAppHost: withLocalPort(LOCAL_ADMIN_APP_HOST, baseDomain),
|
|
apiHost: withLocalPort(LOCAL_API_HOST, baseDomain),
|
|
portalAppHost: withLocalPort(LOCAL_PORTAL_APP_HOST, baseDomain),
|
|
};
|
|
}
|
|
|
|
return {
|
|
webAppHost: withSubdomain("app", baseDomain),
|
|
adminAppHost: withSubdomain("admin", baseDomain),
|
|
apiHost: withSubdomain("api", baseDomain),
|
|
portalAppHost: withSubdomain("portal", baseDomain),
|
|
};
|
|
}
|
|
|
|
function withLocalPort(defaultHost: string, baseDomain: string) {
|
|
const port = normalizeHost(baseDomain)?.split(":")[1];
|
|
return port ? `${defaultHost}:${port}` : defaultHost;
|
|
}
|
|
|
|
function withSubdomain(subdomain: string, baseDomain: string) {
|
|
const normalizedBaseDomain = normalizeHost(baseDomain) ?? baseDomain;
|
|
const [hostname, port] = normalizedBaseDomain.split(":");
|
|
return port ? `${subdomain}.${hostname}:${port}` : `${subdomain}.${hostname}`;
|
|
}
|