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(), method: text("method").notNull(),
port: integer("port").notNull(), port: integer("port").notNull(),
protocol: text("protocol"), protocol: text("protocol"),
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
}); });
// Exit Nodes table // Exit Nodes table

View file

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

View file

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