Add dedicated local admin domain routing

This commit is contained in:
MOH
2026-03-13 17:48:45 +01:00
parent bfd7adccac
commit fef44ded80
47 changed files with 260 additions and 107 deletions
+56 -1
View File
@@ -1,6 +1,7 @@
const DEFAULT_ADMIN_HOST = "root.mohfarawati.de";
const DEFAULT_ADMIN_URL = `https://${DEFAULT_ADMIN_HOST}`;
const DEFAULT_SITE_URL = "https://mohfarawati.de";
const DEV_ADMIN_PREFIX = "/root";
export const INTERNAL_ADMIN_PREFIX = "/admin-internal";
@@ -16,10 +17,32 @@ function normalizeBaseUrl(url: string): string {
return url.replace(/\/+$/, "");
}
function parseHostname(value: string | undefined): string | undefined {
if (!value) {
return undefined;
}
const trimmed = value.trim().toLowerCase();
if (!trimmed) {
return undefined;
}
try {
return new URL(trimmed).hostname.toLowerCase();
} catch {
return trimmed.replace(/^https?:\/\//, "").split("/")[0]?.replace(/:\d+$/, "") || undefined;
}
}
export function getAdminHost(): string {
return (process.env.ADMIN_HOST ?? DEFAULT_ADMIN_HOST).trim().toLowerCase();
}
export function getSiteHost(): string {
return parseHostname(process.env.NEXT_PUBLIC_SITE_URL ?? DEFAULT_SITE_URL) ?? "localhost";
}
export function getRequestHostname(hostHeader?: string | null): string {
return (hostHeader ?? "")
.split(",")[0]
@@ -32,10 +55,18 @@ export function isAdminHost(hostname: string): boolean {
return hostname === getAdminHost();
}
export function hasDedicatedAdminHost(): boolean {
return getAdminHost() !== getSiteHost();
}
export function isLegacyAdminPath(pathname: string): boolean {
return pathname === "/root" || pathname.startsWith("/root/");
}
export function isDevelopmentAdminPath(pathname: string): boolean {
return pathname === DEV_ADMIN_PREFIX || pathname.startsWith(`${DEV_ADMIN_PREFIX}/`);
}
export function isInternalAdminPath(pathname: string): boolean {
return pathname === INTERNAL_ADMIN_PREFIX || pathname.startsWith(`${INTERNAL_ADMIN_PREFIX}/`);
}
@@ -50,12 +81,36 @@ export function toInternalAdminPath(pathname: string): string {
return `${INTERNAL_ADMIN_PREFIX}${normalizedPathname}`;
}
export function fromDevelopmentAdminPath(pathname: string): string {
if (pathname === DEV_ADMIN_PREFIX) {
return "/";
}
if (pathname.startsWith(`${DEV_ADMIN_PREFIX}/`)) {
return pathname.slice(DEV_ADMIN_PREFIX.length);
}
return pathname;
}
export function getAdminBaseUrl(): string {
return normalizeBaseUrl(process.env.NEXT_PUBLIC_ADMIN_URL ?? DEFAULT_ADMIN_URL);
}
export function getAdminAppPath(pathname = "/"): string {
const normalizedPathname = normalizePathname(pathname);
if (process.env.NODE_ENV !== "production" && !hasDedicatedAdminHost()) {
return normalizedPathname === "/"
? DEV_ADMIN_PREFIX
: `${DEV_ADMIN_PREFIX}${normalizedPathname}`;
}
return normalizedPathname;
}
export function buildAdminUrl(pathname = "/"): string {
return `${getAdminBaseUrl()}${normalizePathname(pathname)}`;
return `${getAdminBaseUrl()}${getAdminAppPath(pathname)}`;
}
export function getSiteBaseUrl(): string {