refactor(domain): enhance middleware handling for Traefik routers and improve path validation

This commit is contained in:
Mauricio Siu
2025-08-02 19:24:11 -06:00
parent 5ed49a5ca1
commit 0b9eaac390
+24 -18
View File
@@ -313,40 +313,46 @@ export const createDomainLabels = (
`traefik.http.routers.${routerName}.service=${routerName}`, `traefik.http.routers.${routerName}.service=${routerName}`,
]; ];
// Validate stripPath - it should only be used when path is defined and not "/" // Collect middlewares for this router
if (stripPath) { const middlewares: string[] = [];
if (!path || path === "/") {
console.warn( // Add HTTPS redirect for web entrypoint (must be first)
`stripPath is enabled but path is not defined or is "/" for domain ${host}`, if (entrypoint === "web" && https) {
); middlewares.push("redirect-to-https@file");
} else { }
const middlewareName = `stripprefix-${appName}-${uniqueConfigKey}`;
// Add stripPath middleware if needed
if (stripPath && path && path !== "/") {
const middlewareName = `stripprefix-${appName}-${uniqueConfigKey}`;
// Only define middleware once (on web entrypoint)
if (entrypoint === "web") {
labels.push( labels.push(
`traefik.http.middlewares.${middlewareName}.stripprefix.prefixes=${path}`, `traefik.http.middlewares.${middlewareName}.stripprefix.prefixes=${path}`,
); );
} }
middlewares.push(middlewareName);
} }
// Validate internalPath - ensure it's a valid path format // Add internalPath middleware if needed
if (internalPath && internalPath !== "/") { if (internalPath && internalPath !== "/" && internalPath.startsWith("/")) {
if (!internalPath.startsWith("/")) { const middlewareName = `addprefix-${appName}-${uniqueConfigKey}`;
console.warn( // Only define middleware once (on web entrypoint)
`internalPath "${internalPath}" should start with "/" and not be empty for domain ${host}`, if (entrypoint === "web") {
);
} else {
const middlewareName = `addprefix-${appName}-${uniqueConfigKey}`;
labels.push( labels.push(
`traefik.http.middlewares.${middlewareName}.addprefix.prefix=${internalPath}`, `traefik.http.middlewares.${middlewareName}.addprefix.prefix=${internalPath}`,
); );
} }
middlewares.push(middlewareName);
} }
if (entrypoint === "web" && https) { // Apply middlewares to router if any exist
if (middlewares.length > 0) {
labels.push( labels.push(
`traefik.http.routers.${routerName}.middlewares=redirect-to-https@file`, `traefik.http.routers.${routerName}.middlewares=${middlewares.join(",")}`,
); );
} }
// Add TLS configuration for websecure
if (entrypoint === "websecure") { if (entrypoint === "websecure") {
if (certificateType === "letsencrypt") { if (certificateType === "letsencrypt") {
labels.push( labels.push(