2024-09-28 22:50:10 -04:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import db from "@server/db";
|
|
|
|
import * as schema from "@server/db/schema";
|
|
|
|
import { DynamicTraefikConfig } from "./configSchema";
|
2024-10-26 19:57:47 -04:00
|
|
|
import { and, eq, isNotNull } from "drizzle-orm";
|
2024-09-28 22:50:10 -04:00
|
|
|
import logger from "@server/logger";
|
2024-10-01 20:48:03 -04:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
2024-10-12 18:21:31 -04:00
|
|
|
import config from "@server/config";
|
2024-09-28 22:50:10 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
export async function traefikConfigProvider(
|
|
|
|
_: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<any> {
|
2024-09-28 22:50:10 -04:00
|
|
|
try {
|
2024-10-26 19:57:47 -04:00
|
|
|
const all = await db
|
|
|
|
.select()
|
|
|
|
.from(schema.targets)
|
|
|
|
.innerJoin(
|
|
|
|
schema.resources,
|
|
|
|
eq(schema.targets.resourceId, schema.resources.resourceId)
|
|
|
|
)
|
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(schema.targets.enabled, true),
|
|
|
|
isNotNull(schema.resources.fullDomain)
|
|
|
|
)
|
|
|
|
);
|
2024-09-28 22:50:10 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
if (!all.length) {
|
|
|
|
return { http: {} } as DynamicTraefikConfig;
|
|
|
|
}
|
2024-10-22 00:09:27 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
const middlewareName = "badger";
|
2024-09-28 22:50:10 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
const baseDomain = new URL(config.app.base_url).hostname;
|
2024-10-22 00:09:27 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
const tls = {
|
|
|
|
certResolver: config.traefik.cert_resolver,
|
|
|
|
...(config.traefik.prefer_wildcard_cert
|
2024-10-26 20:03:25 -04:00
|
|
|
? {
|
|
|
|
domains: {
|
|
|
|
main: baseDomain,
|
|
|
|
sans: [`*.${baseDomain}`],
|
|
|
|
},
|
|
|
|
}
|
2024-10-26 19:57:47 -04:00
|
|
|
: {}),
|
|
|
|
};
|
2024-10-22 00:09:27 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
const http: any = {
|
|
|
|
routers: {},
|
|
|
|
services: {},
|
|
|
|
middlewares: {
|
|
|
|
[middlewareName]: {
|
|
|
|
plugin: {
|
|
|
|
[middlewareName]: {
|
|
|
|
apiBaseUrl: new URL(
|
|
|
|
"/api/v1",
|
|
|
|
`http://${config.server.internal_hostname}:${config.server.internal_port}`
|
|
|
|
).href,
|
|
|
|
appBaseUrl: config.app.base_url,
|
|
|
|
},
|
2024-09-29 14:37:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-09-28 22:50:10 -04:00
|
|
|
};
|
2024-10-26 19:57:47 -04:00
|
|
|
for (const item of all) {
|
|
|
|
const target = item.targets;
|
|
|
|
const resource = item.resources;
|
|
|
|
|
|
|
|
const routerName = `${target.targetId}-router`;
|
|
|
|
const serviceName = `${target.targetId}-service`;
|
2024-09-28 22:50:10 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
http.routers![routerName] = {
|
|
|
|
entryPoints: [
|
|
|
|
target.ssl
|
|
|
|
? config.traefik.https_entrypoint
|
|
|
|
: config.traefik.http_entrypoint,
|
2024-09-28 22:50:10 -04:00
|
|
|
],
|
2024-10-26 19:57:47 -04:00
|
|
|
middlewares: [middlewareName],
|
|
|
|
service: serviceName,
|
|
|
|
rule: `Host(\`${resource.fullDomain}\`)`,
|
|
|
|
...(target.ssl ? { tls } : {}),
|
|
|
|
};
|
2024-09-28 22:50:10 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
http.services![serviceName] = {
|
|
|
|
loadBalancer: {
|
|
|
|
servers: [
|
|
|
|
{
|
|
|
|
url: `${target.method}://${target.ip}:${target.port}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2024-09-28 22:50:10 -04:00
|
|
|
|
2024-10-26 19:57:47 -04:00
|
|
|
return res.status(HttpCode.OK).json({ http });
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(`Failed to build traefik config: ${e}`);
|
|
|
|
return res.status(HttpCode.INTERNAL_SERVER_ERROR).json({
|
|
|
|
error: "Failed to build traefik config",
|
|
|
|
});
|
|
|
|
}
|
2024-09-28 22:50:10 -04:00
|
|
|
}
|