make fields in provider endpoint optional

This commit is contained in:
Milo Schwartz 2024-09-28 23:19:39 -04:00
parent 30a3ceb2f3
commit 5750c89b92
No known key found for this signature in database
4 changed files with 32 additions and 21 deletions

View file

@ -44,6 +44,7 @@ export const targets = sqliteTable("targets", {
method: text("method").notNull(),
port: integer("port").notNull(),
protocol: text("protocol"),
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
});
// Exit Nodes table

View file

@ -1,11 +1,11 @@
export type DynamicTraefikConfig = {
http: Http;
http?: Http;
};
export type Http = {
routers: Routers;
services: Services;
middlewares: Middlewares;
routers?: Routers;
services?: Services;
middlewares?: Middlewares;
};
export type Routers = {

View file

@ -2,13 +2,14 @@ import { Request, Response } from "express";
import db from "@server/db";
import * as schema from "@server/db/schema";
import { DynamicTraefikConfig } from "./configSchema";
import { like } from "drizzle-orm";
import { and, like, eq } from "drizzle-orm";
import logger from "@server/logger";
export async function traefikConfigProvider(_: Request, res: Response) {
try {
const targets = await getAllTargets();
const traefikConfig = buildTraefikConfig(targets);
logger.debug("Built traefik config");
res.status(200).send(traefikConfig);
} catch (e) {
logger.error(`Failed to build traefik config: ${e}`);
@ -21,35 +22,39 @@ export function buildTraefikConfig(
): DynamicTraefikConfig {
const middlewareName = "gerbil";
if (!targets.length) {
return {};
}
const http: DynamicTraefikConfig["http"] = {
routers: {},
services: {},
middlewares: {
[middlewareName]: {
plugin: {
[middlewareName]: {
// These are temporary values
APIEndpoint:
"http://host.docker.internal:3001/api/v1/gerbil",
ValidToken: "abc123",
},
},
},
},
// middlewares: {
// [middlewareName]: {
// plugin: {
// [middlewareName]: {
// // These are temporary values
// APIEndpoint:
// "http://host.docker.internal:3001/api/v1/gerbil",
// ValidToken: "abc123",
// },
// },
// },
// },
};
for (const target of targets) {
const routerName = `router-${target.targetId}`;
const serviceName = `service-${target.targetId}`;
http.routers[routerName] = {
http.routers![routerName] = {
entryPoints: [target.method],
middlewares: [middlewareName],
middlewares: [],
service: serviceName,
rule: `Host(\`${target.resourceId}\`)`, // assuming resourceId is a valid full hostname
};
http.services[serviceName] = {
http.services![serviceName] = {
loadBalancer: {
servers: [
{ url: `${target.method}://${target.ip}:${target.port}` },
@ -65,6 +70,11 @@ export async function getAllTargets(): Promise<schema.Target[]> {
const all = await db
.select()
.from(schema.targets)
.where(like(schema.targets.resourceId, "%.%")); // any resourceId with a dot is a valid hostname; otherwise it's a UUID placeholder
.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
return all;
}