mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-04 10:05:53 +02:00
organized routes and routes and added rate limiter
This commit is contained in:
parent
f1e77dfe42
commit
1a91dbb89c
45 changed files with 241 additions and 181 deletions
52
server/routers/traefik/configSchema.ts
Normal file
52
server/routers/traefik/configSchema.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
export type DynamicTraefikConfig = {
|
||||
http?: Http;
|
||||
};
|
||||
|
||||
export type Http = {
|
||||
routers?: Routers;
|
||||
services?: Services;
|
||||
middlewares?: Middlewares;
|
||||
};
|
||||
|
||||
export type Routers = {
|
||||
[key: string]: Router;
|
||||
};
|
||||
|
||||
export type Router = {
|
||||
entryPoints: string[];
|
||||
middlewares: string[];
|
||||
service: string;
|
||||
rule: string;
|
||||
};
|
||||
|
||||
export type Services = {
|
||||
[key: string]: Service;
|
||||
};
|
||||
|
||||
export type Service = {
|
||||
loadBalancer: LoadBalancer;
|
||||
};
|
||||
|
||||
export type LoadBalancer = {
|
||||
servers: Server[];
|
||||
};
|
||||
|
||||
export type Server = {
|
||||
url: string;
|
||||
};
|
||||
|
||||
export type Middlewares = {
|
||||
[key: string]: MiddlewarePlugin;
|
||||
};
|
||||
|
||||
export type MiddlewarePlugin = {
|
||||
plugin: Plugin;
|
||||
};
|
||||
|
||||
export type Plugin = {
|
||||
[key: string]: MiddlewarePluginConfig;
|
||||
};
|
||||
|
||||
export type MiddlewarePluginConfig = {
|
||||
[key: string]: any;
|
||||
};
|
83
server/routers/traefik/getTraefikConfig.ts
Normal file
83
server/routers/traefik/getTraefikConfig.ts
Normal file
|
@ -0,0 +1,83 @@
|
|||
import { Request, Response } from "express";
|
||||
import db from "@server/db";
|
||||
import * as schema from "@server/db/schema";
|
||||
import { DynamicTraefikConfig } from "./configSchema";
|
||||
import { and, like, eq } from "drizzle-orm";
|
||||
import logger from "@server/logger";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
|
||||
export async function traefikConfigProvider(_: Request, res: Response) {
|
||||
try {
|
||||
const targets = await getAllTargets();
|
||||
const traefikConfig = buildTraefikConfig(targets);
|
||||
// logger.debug("Built traefik config");
|
||||
res.status(HttpCode.OK).json(traefikConfig);
|
||||
} catch (e) {
|
||||
logger.error(`Failed to build traefik config: ${e}`);
|
||||
res.status(HttpCode.INTERNAL_SERVER_ERROR).json({
|
||||
error: "Failed to build traefik config",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function buildTraefikConfig(
|
||||
targets: schema.Target[],
|
||||
): DynamicTraefikConfig {
|
||||
const middlewareName = "badger";
|
||||
|
||||
if (!targets.length) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const http: DynamicTraefikConfig["http"] = {
|
||||
routers: {},
|
||||
services: {},
|
||||
middlewares: {
|
||||
[middlewareName]: {
|
||||
plugin: {
|
||||
[middlewareName]: {
|
||||
// These are temporary values
|
||||
apiAddress:
|
||||
"http://host.docker.internal:3001/api/v1/badger",
|
||||
validToken: "abc123",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
for (const target of targets) {
|
||||
const routerName = `router-${target.targetId}`;
|
||||
const serviceName = `service-${target.targetId}`;
|
||||
|
||||
http.routers![routerName] = {
|
||||
entryPoints: [target.method],
|
||||
middlewares: [middlewareName],
|
||||
service: serviceName,
|
||||
rule: `Host(\`${target.resourceId}\`)`, // assuming resourceId is a valid full hostname
|
||||
};
|
||||
|
||||
http.services![serviceName] = {
|
||||
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)
|
||||
.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;
|
||||
}
|
1
server/routers/traefik/index.ts
Normal file
1
server/routers/traefik/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from "./getTraefikConfig";
|
Loading…
Add table
Add a link
Reference in a new issue