From 38c1d86e2f0f9ba9955a916f0ba7e0a9f912235a Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:33:23 -0600 Subject: [PATCH] refactor(domains): add services to each router --- .../__test__/compose/domain/labels.test.ts | 63 ++++++++++++------- apps/dokploy/server/utils/docker/domain.ts | 8 +-- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/apps/dokploy/__test__/compose/domain/labels.test.ts b/apps/dokploy/__test__/compose/domain/labels.test.ts index e2cc7bca4..9d5c0b52a 100644 --- a/apps/dokploy/__test__/compose/domain/labels.test.ts +++ b/apps/dokploy/__test__/compose/domain/labels.test.ts @@ -21,54 +21,69 @@ describe("createDomainLabels", () => { it("should create basic labels for web entrypoint", async () => { const labels = await createDomainLabels(appName, baseDomain, "web"); - - expect(labels).toContain( + expect(labels).toEqual([ "traefik.http.routers.test-app-1-web.rule=Host(`example.com`)", - ); - expect(labels).toContain( "traefik.http.routers.test-app-1-web.entrypoints=web", - ); - expect(labels).toContain( "traefik.http.services.test-app-1-web.loadbalancer.server.port=8080", - ); + "traefik.http.routers.test-app-1-web.service=test-app-1-web", + ]); }); it("should create labels for websecure entrypoint", async () => { - const labels = await createDomainLabels( - appName, - { ...baseDomain, https: true }, - "websecure", - ); - expect(labels).toContain( + const labels = await createDomainLabels(appName, baseDomain, "websecure"); + expect(labels).toEqual([ "traefik.http.routers.test-app-1-websecure.rule=Host(`example.com`)", - ); - expect(labels).toContain( "traefik.http.routers.test-app-1-websecure.entrypoints=websecure", - ); - expect(labels).not.toContain( "traefik.http.services.test-app-1-websecure.loadbalancer.server.port=8080", - ); + "traefik.http.routers.test-app-1-websecure.service=test-app-1-websecure", + ]); }); it("should add redirect middleware for https on web entrypoint", async () => { - const labels = await createDomainLabels( - appName, - { ...baseDomain, https: true }, - "web", - ); + const httpsBaseDomain = { ...baseDomain, https: true }; + const labels = await createDomainLabels(appName, httpsBaseDomain, "web"); expect(labels).toContain( "traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file", ); }); it("should add Let's Encrypt configuration for websecure with letsencrypt certificate", async () => { + const letsencryptDomain = { + ...baseDomain, + https: true, + certificateType: "letsencrypt" as const, + }; const labels = await createDomainLabels( appName, - { ...baseDomain, https: true, certificateType: "letsencrypt" }, + letsencryptDomain, "websecure", ); expect(labels).toContain( "traefik.http.routers.test-app-1-websecure.tls.certresolver=letsencrypt", ); }); + + it("should not add Let's Encrypt configuration for non-letsencrypt certificate", async () => { + const nonLetsencryptDomain = { + ...baseDomain, + https: true, + certificateType: "none" as const, + }; + const labels = await createDomainLabels( + appName, + nonLetsencryptDomain, + "websecure", + ); + expect(labels).not.toContain( + "traefik.http.routers.test-app-1-websecure.tls.certresolver=letsencrypt", + ); + }); + + it("should handle different ports correctly", async () => { + const customPortDomain = { ...baseDomain, port: 3000 }; + const labels = await createDomainLabels(appName, customPortDomain, "web"); + expect(labels).toContain( + "traefik.http.services.test-app-1-web.loadbalancer.server.port=3000", + ); + }); }); diff --git a/apps/dokploy/server/utils/docker/domain.ts b/apps/dokploy/server/utils/docker/domain.ts index 924a9e8e2..ab4a904bd 100644 --- a/apps/dokploy/server/utils/docker/domain.ts +++ b/apps/dokploy/server/utils/docker/domain.ts @@ -157,14 +157,10 @@ export const createDomainLabels = async ( const labels = [ `traefik.http.routers.${routerName}.rule=Host(\`${host}\`)`, `traefik.http.routers.${routerName}.entrypoints=${entrypoint}`, + `traefik.http.services.${routerName}.loadbalancer.server.port=${port}`, + `traefik.http.routers.${routerName}.service=${routerName}`, ]; - if (entrypoint === "web") { - labels.push( - `traefik.http.services.${routerName}.loadbalancer.server.port=${port}`, - ); - } - if (entrypoint === "web" && https) { labels.push( `traefik.http.routers.${routerName}.middlewares=redirect-to-https@file`,