mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-01 00:06:38 +00:00
Add to oss traefik config and fix create/update
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { db, exitNodes, targetHealthCheck } from "@server/db";
|
import { db, exitNodes, targetHealthCheck } from "@server/db";
|
||||||
import { and, eq, inArray, or, isNull, ne, isNotNull } from "drizzle-orm";
|
import { and, eq, inArray, or, isNull, ne, isNotNull, desc } from "drizzle-orm";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
import { orgs, resources, sites, Target, targets } from "@server/db";
|
import { orgs, resources, sites, Target, targets } from "@server/db";
|
||||||
@@ -124,6 +124,8 @@ export async function getTraefikConfig(
|
|||||||
pathMatchType: targets.pathMatchType,
|
pathMatchType: targets.pathMatchType,
|
||||||
rewritePath: targets.rewritePath,
|
rewritePath: targets.rewritePath,
|
||||||
rewritePathType: targets.rewritePathType,
|
rewritePathType: targets.rewritePathType,
|
||||||
|
priority: targets.priority,
|
||||||
|
|
||||||
// Site fields
|
// Site fields
|
||||||
siteId: sites.siteId,
|
siteId: sites.siteId,
|
||||||
siteType: sites.type,
|
siteType: sites.type,
|
||||||
@@ -152,7 +154,8 @@ export async function getTraefikConfig(
|
|||||||
? isNotNull(resources.http) // ignore the http check if allow_raw_resources is true
|
? isNotNull(resources.http) // ignore the http check if allow_raw_resources is true
|
||||||
: eq(resources.http, true)
|
: eq(resources.http, true)
|
||||||
)
|
)
|
||||||
);
|
)
|
||||||
|
.orderBy(desc(targets.priority), targets.targetId); // stable ordering
|
||||||
|
|
||||||
// Group by resource and include targets with their unique site data
|
// Group by resource and include targets with their unique site data
|
||||||
const resourcesMap = new Map();
|
const resourcesMap = new Map();
|
||||||
@@ -163,6 +166,7 @@ export async function getTraefikConfig(
|
|||||||
const pathMatchType = row.pathMatchType || "";
|
const pathMatchType = row.pathMatchType || "";
|
||||||
const rewritePath = row.rewritePath || "";
|
const rewritePath = row.rewritePath || "";
|
||||||
const rewritePathType = row.rewritePathType || "";
|
const rewritePathType = row.rewritePathType || "";
|
||||||
|
const priority = row.priority ?? 100;
|
||||||
|
|
||||||
// Create a unique key combining resourceId, path config, and rewrite config
|
// Create a unique key combining resourceId, path config, and rewrite config
|
||||||
const pathKey = [targetPath, pathMatchType, rewritePath, rewritePathType]
|
const pathKey = [targetPath, pathMatchType, rewritePath, rewritePathType]
|
||||||
@@ -202,7 +206,8 @@ export async function getTraefikConfig(
|
|||||||
path: row.path, // the targets will all have the same path
|
path: row.path, // the targets will all have the same path
|
||||||
pathMatchType: row.pathMatchType, // the targets will all have the same pathMatchType
|
pathMatchType: row.pathMatchType, // the targets will all have the same pathMatchType
|
||||||
rewritePath: row.rewritePath,
|
rewritePath: row.rewritePath,
|
||||||
rewritePathType: row.rewritePathType
|
rewritePathType: row.rewritePathType,
|
||||||
|
priority: priority // may be null, we fallback later
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,6 +222,7 @@ export async function getTraefikConfig(
|
|||||||
enabled: row.targetEnabled,
|
enabled: row.targetEnabled,
|
||||||
rewritePath: row.rewritePath,
|
rewritePath: row.rewritePath,
|
||||||
rewritePathType: row.rewritePathType,
|
rewritePathType: row.rewritePathType,
|
||||||
|
priority: row.priority,
|
||||||
site: {
|
site: {
|
||||||
siteId: row.siteId,
|
siteId: row.siteId,
|
||||||
type: row.siteType,
|
type: row.siteType,
|
||||||
@@ -402,10 +408,30 @@ export async function getTraefikConfig(
|
|||||||
|
|
||||||
// Build routing rules
|
// Build routing rules
|
||||||
let rule = `Host(\`${fullDomain}\`)`;
|
let rule = `Host(\`${fullDomain}\`)`;
|
||||||
let priority = 100;
|
|
||||||
|
// priority logic
|
||||||
|
let priority: number;
|
||||||
|
if (resource.priority && resource.priority != 100) {
|
||||||
|
priority = resource.priority;
|
||||||
|
} else {
|
||||||
|
priority = 100;
|
||||||
|
if (resource.path && resource.pathMatchType) {
|
||||||
|
priority += 10;
|
||||||
|
if (resource.pathMatchType === "exact") {
|
||||||
|
priority += 5;
|
||||||
|
} else if (resource.pathMatchType === "prefix") {
|
||||||
|
priority += 3;
|
||||||
|
} else if (resource.pathMatchType === "regex") {
|
||||||
|
priority += 2;
|
||||||
|
}
|
||||||
|
if (resource.path === "/") {
|
||||||
|
priority = 1; // lowest for catch-all
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (resource.path && resource.pathMatchType) {
|
if (resource.path && resource.pathMatchType) {
|
||||||
priority += 1;
|
// priority += 1;
|
||||||
// add path to rule based on match type
|
// add path to rule based on match type
|
||||||
let path = resource.path;
|
let path = resource.path;
|
||||||
// if the path doesn't start with a /, add it
|
// if the path doesn't start with a /, add it
|
||||||
|
|||||||
@@ -211,7 +211,10 @@ export async function createTarget(
|
|||||||
internalPort,
|
internalPort,
|
||||||
enabled: targetData.enabled,
|
enabled: targetData.enabled,
|
||||||
path: targetData.path,
|
path: targetData.path,
|
||||||
pathMatchType: targetData.pathMatchType
|
pathMatchType: targetData.pathMatchType,
|
||||||
|
rewritePath: targetData.rewritePath,
|
||||||
|
rewritePathType: targetData.rewritePathType,
|
||||||
|
priority: targetData.priority
|
||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
|
|||||||
@@ -199,7 +199,10 @@ export async function updateTarget(
|
|||||||
internalPort,
|
internalPort,
|
||||||
enabled: parsedBody.data.enabled,
|
enabled: parsedBody.data.enabled,
|
||||||
path: parsedBody.data.path,
|
path: parsedBody.data.path,
|
||||||
pathMatchType: parsedBody.data.pathMatchType
|
pathMatchType: parsedBody.data.pathMatchType,
|
||||||
|
priority: parsedBody.data.priority,
|
||||||
|
rewritePath: parsedBody.data.rewritePath,
|
||||||
|
rewritePathType: parsedBody.data.rewritePathType
|
||||||
})
|
})
|
||||||
.where(eq(targets.targetId, targetId))
|
.where(eq(targets.targetId, targetId))
|
||||||
.returning();
|
.returning();
|
||||||
|
|||||||
Reference in New Issue
Block a user