Break out traefik config

This commit is contained in:
Owen
2025-08-13 16:15:23 -07:00
parent ddd8eb1da0
commit 1f6379a7e6

View File

@@ -13,15 +13,13 @@ export async function traefikConfigProvider(
res: Response res: Response
): Promise<any> { ): Promise<any> {
try { try {
// Get all resources with related data
const allResources = await db.transaction(async (tx) => {
// First query to get resources with site and org info // First query to get resources with site and org info
// Get the current exit node name from config // Get the current exit node name from config
if (!currentExitNodeId) { if (!currentExitNodeId) {
if (config.getRawConfig().gerbil.exit_node_name) { if (config.getRawConfig().gerbil.exit_node_name) {
const exitNodeName = const exitNodeName =
config.getRawConfig().gerbil.exit_node_name!; config.getRawConfig().gerbil.exit_node_name!;
const [exitNode] = await tx const [exitNode] = await db
.select({ .select({
exitNodeId: exitNodes.exitNodeId exitNodeId: exitNodes.exitNodeId
}) })
@@ -31,7 +29,7 @@ export async function traefikConfigProvider(
currentExitNodeId = exitNode.exitNodeId; currentExitNodeId = exitNode.exitNodeId;
} }
} else { } else {
const [exitNode] = await tx const [exitNode] = await db
.select({ .select({
exitNodeId: exitNodes.exitNodeId exitNodeId: exitNodes.exitNodeId
}) })
@@ -44,6 +42,22 @@ export async function traefikConfigProvider(
} }
} }
const traefikConfig = await getTraefikConfig(currentExitNodeId);
return res.status(HttpCode.OK).json(traefikConfig);
} catch (e) {
logger.error(`Failed to build Traefik config: ${e}`);
return res.status(HttpCode.INTERNAL_SERVER_ERROR).json({
error: "Failed to build Traefik config"
});
}
}
export async function getTraefikConfig(
exitNodeId: number
): Promise<any> {
// Get all resources with related data
const allResources = await db.transaction(async (tx) => {
// Get the site(s) on this exit node // Get the site(s) on this exit node
const resourcesWithRelations = await tx const resourcesWithRelations = await tx
.select({ .select({
@@ -73,7 +87,7 @@ export async function traefikConfigProvider(
.innerJoin(sites, eq(sites.siteId, resources.siteId)) .innerJoin(sites, eq(sites.siteId, resources.siteId))
.where( .where(
or( or(
eq(sites.exitNodeId, currentExitNodeId), eq(sites.exitNodeId, exitNodeId),
isNull(sites.exitNodeId) isNull(sites.exitNodeId)
) )
); );
@@ -120,7 +134,7 @@ export async function traefikConfigProvider(
}); });
if (!allResources.length) { if (!allResources.length) {
return res.status(HttpCode.OK).json({}); return {}
} }
const badgerMiddlewareName = "badger"; const badgerMiddlewareName = "badger";
@@ -137,10 +151,7 @@ export async function traefikConfigProvider(
`http://${ `http://${
config.getRawConfig().server config.getRawConfig().server
.internal_hostname .internal_hostname
}:${ }:${config.getRawConfig().server.internal_port}`
config.getRawConfig().server
.internal_port
}`
).href, ).href,
userSessionCookieName: userSessionCookieName:
config.getRawConfig().server config.getRawConfig().server
@@ -247,10 +258,7 @@ export async function traefikConfigProvider(
? config.getRawConfig().traefik.https_entrypoint ? config.getRawConfig().traefik.https_entrypoint
: config.getRawConfig().traefik.http_entrypoint : config.getRawConfig().traefik.http_entrypoint
], ],
middlewares: [ middlewares: [badgerMiddlewareName, ...additionalMiddlewares],
badgerMiddlewareName,
...additionalMiddlewares
],
service: serviceName, service: serviceName,
rule: `Host(\`${fullDomain}\`)`, rule: `Host(\`${fullDomain}\`)`,
priority: 100, priority: 100,
@@ -356,8 +364,7 @@ export async function traefikConfigProvider(
} }
}; };
if (!config_output.http.routers![routerName].middlewares) { if (!config_output.http.routers![routerName].middlewares) {
config_output.http.routers![routerName].middlewares = config_output.http.routers![routerName].middlewares = [];
[];
} }
config_output.http.routers![routerName].middlewares = [ config_output.http.routers![routerName].middlewares = [
...config_output.http.routers![routerName].middlewares, ...config_output.http.routers![routerName].middlewares,
@@ -440,11 +447,5 @@ export async function traefikConfigProvider(
}; };
} }
} }
return res.status(HttpCode.OK).json(config_output); return config_output;
} catch (e) {
logger.error(`Failed to build Traefik config: ${e}`);
return res.status(HttpCode.INTERNAL_SERVER_ERROR).json({
error: "Failed to build Traefik config"
});
}
} }