Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,104 @@
|
||||
export type DomainRoutingEnv = Record<string, string | undefined> & {
|
||||
APP_BASE_DOMAIN?: string;
|
||||
WEB_APP_HOST?: string;
|
||||
ADMIN_APP_HOST?: string;
|
||||
API_HOST?: string;
|
||||
APP_PROTOCOL?: string;
|
||||
NODE_ENV?: string;
|
||||
};
|
||||
|
||||
export type DomainRoutingConfig = {
|
||||
appBaseDomain: string;
|
||||
webAppHost: string;
|
||||
adminAppHost: string;
|
||||
apiHost: 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";
|
||||
|
||||
export function getDomainRoutingConfig(env: DomainRoutingEnv = process.env): DomainRoutingConfig {
|
||||
const appBaseDomain = normalizeHost(env.APP_BASE_DOMAIN) ?? LOCAL_BASE_DOMAIN;
|
||||
const derivedHosts = getDerivedHosts(appBaseDomain);
|
||||
|
||||
return {
|
||||
appBaseDomain,
|
||||
webAppHost: normalizeHost(env.WEB_APP_HOST) ?? derivedHosts.webAppHost,
|
||||
adminAppHost: normalizeHost(env.ADMIN_APP_HOST) ?? derivedHosts.adminAppHost,
|
||||
apiHost: normalizeHost(env.API_HOST) ?? derivedHosts.apiHost,
|
||||
protocol: normalizeProtocol(env.APP_PROTOCOL) ?? (isLocalHost(appBaseDomain) || env.NODE_ENV === "development" ? "http" : "https"),
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeHost(value: string | undefined): string | undefined {
|
||||
const trimmed = value?.trim();
|
||||
if (!trimmed) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const withProtocol = /^[a-z][a-z\d+\-.]*:\/\//i.test(trimmed) ? trimmed : `http://${trimmed}`;
|
||||
const url = new URL(withProtocol);
|
||||
return url.host.toLowerCase();
|
||||
}
|
||||
|
||||
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),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
webAppHost: withSubdomain("app", baseDomain),
|
||||
adminAppHost: withSubdomain("admin", baseDomain),
|
||||
apiHost: withSubdomain("api", 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}`;
|
||||
}
|
||||
Reference in new issue
Block a user