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";
|
2024-10-01 20:48:03 -04:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
2024-10-06 18:08:26 -04:00
|
|
|
import env from "@server/environment";
|
2024-10-06 22:09:30 -04:00
|
|
|
import environment from "@server/environment";
|
2024-09-28 22:50:10 -04:00
|
|
|
|
|
|
|
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-10-01 20:48:03 -04:00
|
|
|
res.status(HttpCode.OK).json(traefikConfig);
|
2024-09-28 22:50:10 -04:00
|
|
|
} catch (e) {
|
|
|
|
logger.error(`Failed to build traefik config: ${e}`);
|
2024-10-01 20:48:03 -04:00
|
|
|
res.status(HttpCode.INTERNAL_SERVER_ERROR).json({
|
|
|
|
error: "Failed to build traefik config",
|
|
|
|
});
|
2024-09-28 22:50:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"] = {
|
2024-10-06 22:09:30 -04:00
|
|
|
routers: {
|
|
|
|
"themainwebpage": {
|
|
|
|
"entryPoints": [
|
|
|
|
"http"
|
|
|
|
],
|
|
|
|
"middlewares": [
|
|
|
|
],
|
|
|
|
"service": "service-themainwebpage",
|
|
|
|
"rule": "Host(`testing123.io`)"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
services: {
|
|
|
|
"service-themainwebpage": {
|
|
|
|
"loadBalancer": {
|
|
|
|
"servers": [
|
|
|
|
{
|
|
|
|
"url": `http://${environment.APP_NAME.toLowerCase()}:3000`
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2024-09-29 14:37:26 -04:00
|
|
|
middlewares: {
|
|
|
|
[middlewareName]: {
|
|
|
|
plugin: {
|
|
|
|
[middlewareName]: {
|
2024-10-06 22:09:30 -04:00
|
|
|
apiBaseUrl: `http://${environment.APP_NAME.toLowerCase()}:3001/api/v1`,
|
|
|
|
// appBaseUrl: env.BASE_URL,
|
|
|
|
appBaseUrl: "http://testing123.io:8081",
|
2024-09-29 14:37:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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;
|
|
|
|
}
|