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-09-28 23:19:39 -04:00
|
|
|
import { and, like, eq } from "drizzle-orm";
|
2024-09-28 22:50:10 -04:00
|
|
|
import logger from "@server/logger";
|
|
|
|
|
|
|
|
export async function traefikConfigProvider(_: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const targets = await getAllTargets();
|
|
|
|
const traefikConfig = buildTraefikConfig(targets);
|
2024-09-29 14:37:26 -04:00
|
|
|
// logger.debug("Built traefik config");
|
2024-09-28 22:50:10 -04:00
|
|
|
res.status(200).send(traefikConfig);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(`Failed to build traefik config: ${e}`);
|
|
|
|
res.status(500).send({ message: "Failed to build traefik config" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildTraefikConfig(
|
|
|
|
targets: schema.Target[],
|
|
|
|
): DynamicTraefikConfig {
|
2024-09-29 14:37:26 -04:00
|
|
|
const middlewareName = "badger";
|
2024-09-28 22:50:10 -04:00
|
|
|
|
2024-09-28 23:19:39 -04:00
|
|
|
if (!targets.length) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-09-28 22:50:10 -04:00
|
|
|
const http: DynamicTraefikConfig["http"] = {
|
|
|
|
routers: {},
|
|
|
|
services: {},
|
2024-09-29 14:37:26 -04:00
|
|
|
middlewares: {
|
|
|
|
[middlewareName]: {
|
|
|
|
plugin: {
|
|
|
|
[middlewareName]: {
|
|
|
|
// These are temporary values
|
|
|
|
apiAddress:
|
|
|
|
"http://host.docker.internal:3001/api/v1/badger",
|
|
|
|
validToken: "abc123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-09-28 22:50:10 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
const routerName = `router-${target.targetId}`;
|
|
|
|
const serviceName = `service-${target.targetId}`;
|
|
|
|
|
2024-09-28 23:19:39 -04:00
|
|
|
http.routers![routerName] = {
|
2024-09-28 22:50:10 -04:00
|
|
|
entryPoints: [target.method],
|
2024-09-29 14:37:26 -04:00
|
|
|
middlewares: [middlewareName],
|
2024-09-28 22:50:10 -04:00
|
|
|
service: serviceName,
|
|
|
|
rule: `Host(\`${target.resourceId}\`)`, // assuming resourceId is a valid full hostname
|
|
|
|
};
|
|
|
|
|
2024-09-28 23:19:39 -04:00
|
|
|
http.services![serviceName] = {
|
2024-09-28 22:50:10 -04:00
|
|
|
loadBalancer: {
|
|
|
|
servers: [
|
|
|
|
{ url: `${target.method}://${target.ip}:${target.port}` },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return { http } as DynamicTraefikConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAllTargets(): Promise<schema.Target[]> {
|
|
|
|
const all = await db
|
|
|
|
.select()
|
|
|
|
.from(schema.targets)
|
2024-09-28 23:19:39 -04:00
|
|
|
.where(
|
|
|
|
and(
|
|
|
|
eq(schema.targets.enabled, true),
|
|
|
|
like(schema.targets.resourceId, "%.%"),
|
|
|
|
),
|
|
|
|
); // any resourceId with a dot is a valid hostname; otherwise it's a UUID placeholder
|
2024-09-28 22:50:10 -04:00
|
|
|
return all;
|
|
|
|
}
|