refactor(domains): add services to each router

This commit is contained in:
Mauricio Siu
2024-08-17 21:33:23 -06:00
parent d08e232f50
commit 38c1d86e2f
2 changed files with 41 additions and 30 deletions

View File

@@ -21,54 +21,69 @@ describe("createDomainLabels", () => {
it("should create basic labels for web entrypoint", async () => { it("should create basic labels for web entrypoint", async () => {
const labels = await createDomainLabels(appName, baseDomain, "web"); const labels = await createDomainLabels(appName, baseDomain, "web");
expect(labels).toEqual([
expect(labels).toContain(
"traefik.http.routers.test-app-1-web.rule=Host(`example.com`)", "traefik.http.routers.test-app-1-web.rule=Host(`example.com`)",
);
expect(labels).toContain(
"traefik.http.routers.test-app-1-web.entrypoints=web", "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.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 () => { it("should create labels for websecure entrypoint", async () => {
const labels = await createDomainLabels( const labels = await createDomainLabels(appName, baseDomain, "websecure");
appName, expect(labels).toEqual([
{ ...baseDomain, https: true },
"websecure",
);
expect(labels).toContain(
"traefik.http.routers.test-app-1-websecure.rule=Host(`example.com`)", "traefik.http.routers.test-app-1-websecure.rule=Host(`example.com`)",
);
expect(labels).toContain(
"traefik.http.routers.test-app-1-websecure.entrypoints=websecure", "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.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 () => { it("should add redirect middleware for https on web entrypoint", async () => {
const labels = await createDomainLabels( const httpsBaseDomain = { ...baseDomain, https: true };
appName, const labels = await createDomainLabels(appName, httpsBaseDomain, "web");
{ ...baseDomain, https: true },
"web",
);
expect(labels).toContain( expect(labels).toContain(
"traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file", "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 () => { 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( const labels = await createDomainLabels(
appName, appName,
{ ...baseDomain, https: true, certificateType: "letsencrypt" }, letsencryptDomain,
"websecure", "websecure",
); );
expect(labels).toContain( expect(labels).toContain(
"traefik.http.routers.test-app-1-websecure.tls.certresolver=letsencrypt", "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",
);
});
}); });

View File

@@ -157,14 +157,10 @@ export const createDomainLabels = async (
const labels = [ const labels = [
`traefik.http.routers.${routerName}.rule=Host(\`${host}\`)`, `traefik.http.routers.${routerName}.rule=Host(\`${host}\`)`,
`traefik.http.routers.${routerName}.entrypoints=${entrypoint}`, `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) { if (entrypoint === "web" && https) {
labels.push( labels.push(
`traefik.http.routers.${routerName}.middlewares=redirect-to-https@file`, `traefik.http.routers.${routerName}.middlewares=redirect-to-https@file`,