Path rewriting working?

This commit is contained in:
Owen
2025-10-13 16:41:14 -07:00
parent 8b2f8ad3ef
commit 902b413881
4 changed files with 326 additions and 250 deletions

View File

@@ -1860,5 +1860,38 @@
}, },
"priority": "Priority", "priority": "Priority",
"priorityDescription": "Higher priority routes are evaluated first. Priority = 100 means automatic ordering (system decides). Use another number to enforce manual priority.", "priorityDescription": "Higher priority routes are evaluated first. Priority = 100 means automatic ordering (system decides). Use another number to enforce manual priority.",
"instanceName": "Instance Name" "instanceName": "Instance Name",
"pathMatchModalTitle": "Configure Path Matching",
"pathMatchModalDescription": "Set up how incoming requests should be matched based on their path.",
"pathMatchType": "Match Type",
"pathMatchPrefix": "Prefix",
"pathMatchExact": "Exact",
"pathMatchRegex": "Regex",
"pathMatchValue": "Path Value",
"clear": "Clear",
"saveChanges": "Save Changes",
"pathMatchRegexPlaceholder": "^/api/.*",
"pathMatchDefaultPlaceholder": "/path",
"pathMatchPrefixHelp": "Example: /api matches /api, /api/users, etc.",
"pathMatchExactHelp": "Example: /api matches only /api",
"pathMatchRegexHelp": "Example: ^/api/.* matches /api/anything",
"pathRewriteModalTitle": "Configure Path Rewriting",
"pathRewriteModalDescription": "Transform the matched path before forwarding to the target.",
"pathRewriteType": "Rewrite Type",
"pathRewritePrefixOption": "Prefix - Replace prefix",
"pathRewriteExactOption": "Exact - Replace entire path",
"pathRewriteRegexOption": "Regex - Pattern replacement",
"pathRewriteStripPrefixOption": "Strip Prefix - Remove prefix",
"pathRewriteValue": "Rewrite Value",
"pathRewriteRegexPlaceholder": "/new/$1",
"pathRewriteDefaultPlaceholder": "/new-path",
"pathRewritePrefixHelp": "Replace the matched prefix with this value",
"pathRewriteExactHelp": "Replace the entire path with this value when the path matches exactly",
"pathRewriteRegexHelp": "Use capture groups like $1, $2 for replacement",
"pathRewriteStripPrefixHelp": "Leave empty to strip prefix or provide new prefix",
"pathRewritePrefix": "Prefix",
"pathRewriteExact": "Exact",
"pathRewriteRegex": "Regex",
"pathRewriteStrip": "Strip",
"pathRewriteStripLabel": "strip"
} }

View File

@@ -164,9 +164,6 @@ export async function getTraefikConfig(
port: row.port, port: row.port,
internalPort: row.internalPort, internalPort: row.internalPort,
enabled: row.targetEnabled, enabled: row.targetEnabled,
rewritePath: row.rewritePath,
rewritePathType: row.rewritePathType,
priority: row.priority,
site: { site: {
siteId: row.siteId, siteId: row.siteId,
type: row.siteType, type: row.siteType,
@@ -268,8 +265,8 @@ export async function getTraefikConfig(
// Handle path rewriting middleware // Handle path rewriting middleware
if ( if (
resource.rewritePath && resource.rewritePath !== null &&
resource.path && resource.path !== null &&
resource.pathMatchType && resource.pathMatchType &&
resource.rewritePathType resource.rewritePathType
) { ) {

View File

@@ -203,9 +203,6 @@ export async function getTraefikConfig(
port: row.port, port: row.port,
internalPort: row.internalPort, internalPort: row.internalPort,
enabled: row.targetEnabled, enabled: row.targetEnabled,
rewritePath: row.rewritePath,
rewritePathType: row.rewritePathType,
priority: row.priority,
site: { site: {
siteId: row.siteId, siteId: row.siteId,
type: row.siteType, type: row.siteType,
@@ -330,8 +327,8 @@ export async function getTraefikConfig(
// Handle path rewriting middleware // Handle path rewriting middleware
if ( if (
resource.rewritePath && resource.rewritePath !== null &&
resource.path && resource.path !== null &&
resource.pathMatchType && resource.pathMatchType &&
resource.rewritePathType resource.rewritePathType
) { ) {

View File

@@ -4,7 +4,7 @@ import {
SelectContent, SelectContent,
SelectItem, SelectItem,
SelectTrigger, SelectTrigger,
SelectValue, SelectValue
} from "@/components/ui/select"; } from "@/components/ui/select";
import { Badge } from "@app/components/ui/badge"; import { Badge } from "@app/components/ui/badge";
@@ -12,20 +12,35 @@ import { Label } from "@app/components/ui/label";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Input } from "./ui/input"; import { Input } from "./ui/input";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { Credenza, CredenzaContent, CredenzaDescription, CredenzaFooter, CredenzaHeader, CredenzaTitle, CredenzaTrigger } from "./Credenza"; import {
Credenza,
CredenzaContent,
CredenzaDescription,
CredenzaFooter,
CredenzaHeader,
CredenzaTitle,
CredenzaTrigger
} from "./Credenza";
import { useTranslations } from "next-intl";
export function PathMatchModal({ export function PathMatchModal({
value, value,
onChange, onChange,
trigger, trigger
}: { }: {
value: { path: string | null; pathMatchType: string | null }; value: { path: string | null; pathMatchType: string | null };
onChange: (config: { path: string | null; pathMatchType: string | null }) => void; onChange: (config: {
path: string | null;
pathMatchType: string | null;
}) => void;
trigger: React.ReactNode; trigger: React.ReactNode;
}) { }) {
const t = useTranslations();
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [matchType, setMatchType] = useState(value?.pathMatchType || "prefix"); const [matchType, setMatchType] = useState(
value?.pathMatchType || "prefix"
);
const [path, setPath] = useState(value?.path || ""); const [path, setPath] = useState(value?.path || "");
useEffect(() => { useEffect(() => {
@@ -45,16 +60,16 @@ export function PathMatchModal({
setOpen(false); setOpen(false);
}; };
const getPlaceholder = () => (matchType === "regex" ? "^/api/.*" : "/path"); const getPlaceholder = () => (matchType === "regex" ? t("pathMatchRegexPlaceholder") : t("pathMatchDefaultPlaceholder"));
const getHelpText = () => { const getHelpText = () => {
switch (matchType) { switch (matchType) {
case "prefix": case "prefix":
return "Example: /api matches /api, /api/users, etc."; return t("pathMatchPrefixHelp");
case "exact": case "exact":
return "Example: /api matches only /api"; return t("pathMatchExactHelp");
case "regex": case "regex":
return "Example: ^/api/.* matches /api/anything"; return t("pathMatchRegexHelp");
default: default:
return ""; return "";
} }
@@ -65,44 +80,46 @@ export function PathMatchModal({
<CredenzaTrigger asChild>{trigger}</CredenzaTrigger> <CredenzaTrigger asChild>{trigger}</CredenzaTrigger>
<CredenzaContent className="sm:max-w-[500px]"> <CredenzaContent className="sm:max-w-[500px]">
<CredenzaHeader> <CredenzaHeader>
<CredenzaTitle>Configure Path Matching</CredenzaTitle> <CredenzaTitle>{t("pathMatchModalTitle")}</CredenzaTitle>
<CredenzaDescription> <CredenzaDescription>
Set up how incoming requests should be matched based on their path. {t("pathMatchModalDescription")}
</CredenzaDescription> </CredenzaDescription>
</CredenzaHeader> </CredenzaHeader>
<div className="grid gap-4"> <div className="grid gap-4">
<div className="grid gap-2"> <div className="grid gap-2">
<Label htmlFor="match-type">Match Type</Label> <Label htmlFor="match-type">{t("pathMatchType")}</Label>
<Select value={matchType} onValueChange={setMatchType}> <Select value={matchType} onValueChange={setMatchType}>
<SelectTrigger id="match-type"> <SelectTrigger id="match-type">
<SelectValue /> <SelectValue />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
<SelectItem value="prefix">Prefix</SelectItem> <SelectItem value="prefix">{t("pathMatchPrefix")}</SelectItem>
<SelectItem value="exact">Exact</SelectItem> <SelectItem value="exact">{t("pathMatchExact")}</SelectItem>
<SelectItem value="regex">Regex</SelectItem> <SelectItem value="regex">{t("pathMatchRegex")}</SelectItem>
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>
<div className="grid gap-2"> <div className="grid gap-2">
<Label htmlFor="path-value">Path Value</Label> <Label htmlFor="path-value">{t("pathMatchValue")}</Label>
<Input <Input
id="path-value" id="path-value"
placeholder={getPlaceholder()} placeholder={getPlaceholder()}
value={path} value={path}
onChange={(e) => setPath(e.target.value)} onChange={(e) => setPath(e.target.value)}
/> />
<p className="text-sm text-muted-foreground">{getHelpText()}</p> <p className="text-sm text-muted-foreground">
{getHelpText()}
</p>
</div> </div>
</div> </div>
<CredenzaFooter className="gap-2"> <CredenzaFooter className="gap-2">
{value?.path && ( {value?.path && (
<Button variant="outline" onClick={handleClear}> <Button variant="outline" onClick={handleClear}>
Clear {t("clear")}
</Button> </Button>
)} )}
<Button onClick={handleSave} disabled={!path.trim()}> <Button onClick={handleSave} disabled={!path.trim()}>
Save Changes {t("saveChanges")}
</Button> </Button>
</CredenzaFooter> </CredenzaFooter>
</CredenzaContent> </CredenzaContent>
@@ -110,20 +127,25 @@ export function PathMatchModal({
); );
} }
export function PathRewriteModal({ export function PathRewriteModal({
value, value,
onChange, onChange,
trigger, trigger,
disabled, disabled
}: { }: {
value: { rewritePath: string | null; rewritePathType: string | null }; value: { rewritePath: string | null; rewritePathType: string | null };
onChange: (config: { rewritePath: string | null; rewritePathType: string | null }) => void; onChange: (config: {
rewritePath: string | null;
rewritePathType: string | null;
}) => void;
trigger: React.ReactNode; trigger: React.ReactNode;
disabled?: boolean; disabled?: boolean;
}) { }) {
const t = useTranslations();
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [rewriteType, setRewriteType] = useState(value?.rewritePathType || "prefix"); const [rewriteType, setRewriteType] = useState(
value?.rewritePathType || "prefix"
);
const [rewritePath, setRewritePath] = useState(value?.rewritePath || ""); const [rewritePath, setRewritePath] = useState(value?.rewritePath || "");
useEffect(() => { useEffect(() => {
@@ -134,7 +156,10 @@ export function PathRewriteModal({
}, [open, value]); }, [open, value]);
const handleSave = () => { const handleSave = () => {
onChange({ rewritePathType: rewriteType as any, rewritePath: rewritePath.trim() }); onChange({
rewritePathType: rewriteType as any,
rewritePath: rewritePath.trim()
});
setOpen(false); setOpen(false);
}; };
@@ -146,24 +171,24 @@ export function PathRewriteModal({
const getPlaceholder = () => { const getPlaceholder = () => {
switch (rewriteType) { switch (rewriteType) {
case "regex": case "regex":
return "/new/$1"; return t("pathRewriteRegexPlaceholder");
case "stripPrefix": case "stripPrefix":
return ""; return "";
default: default:
return "/new-path"; return t("pathRewriteDefaultPlaceholder");
} }
}; };
const getHelpText = () => { const getHelpText = () => {
switch (rewriteType) { switch (rewriteType) {
case "prefix": case "prefix":
return "Replace the matched prefix with this value"; return t("pathRewritePrefixHelp");
case "exact": case "exact":
return "Replace the entire path with this value"; return t("pathRewriteExactHelp");
case "regex": case "regex":
return "Use capture groups like $1, $2 for replacement"; return t("pathRewriteRegexHelp");
case "stripPrefix": case "stripPrefix":
return "Leave empty to strip prefix or provide new prefix"; return t("pathRewriteStripPrefixHelp");
default: default:
return ""; return "";
} }
@@ -171,53 +196,66 @@ export function PathRewriteModal({
return ( return (
<Credenza open={open} onOpenChange={(v) => !disabled && setOpen(v)}> <Credenza open={open} onOpenChange={(v) => !disabled && setOpen(v)}>
<CredenzaTrigger asChild> <CredenzaTrigger asChild>{trigger}</CredenzaTrigger>
{trigger}
</CredenzaTrigger>
<CredenzaContent className="sm:max-w-[500px]"> <CredenzaContent className="sm:max-w-[500px]">
<CredenzaHeader> <CredenzaHeader>
<CredenzaTitle>Configure Path Rewriting</CredenzaTitle> <CredenzaTitle>{t("pathRewriteModalTitle")}</CredenzaTitle>
<CredenzaDescription> <CredenzaDescription>
Transform the matched path before forwarding to the target. {t("pathRewriteModalDescription")}
</CredenzaDescription> </CredenzaDescription>
</CredenzaHeader> </CredenzaHeader>
<div className="grid gap-4"> <div className="grid gap-4">
<div className="grid gap-2"> <div className="grid gap-2">
<Label htmlFor="rewrite-type">Rewrite Type</Label> <Label htmlFor="rewrite-type">{t("pathRewriteType")}</Label>
<Select value={rewriteType} onValueChange={setRewriteType}> <Select
value={rewriteType}
onValueChange={setRewriteType}
>
<SelectTrigger id="rewrite-type"> <SelectTrigger id="rewrite-type">
<SelectValue /> <SelectValue />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
<SelectItem value="prefix">Prefix - Replace prefix</SelectItem> <SelectItem value="prefix">
<SelectItem value="exact">Exact - Replace entire path</SelectItem> {t("pathRewritePrefixOption")}
<SelectItem value="regex">Regex - Pattern replacement</SelectItem> </SelectItem>
<SelectItem value="stripPrefix">Strip Prefix - Remove prefix</SelectItem> <SelectItem value="exact">
{t("pathRewriteExactOption")}
</SelectItem>
<SelectItem value="regex">
{t("pathRewriteRegexOption")}
</SelectItem>
<SelectItem value="stripPrefix">
{t("pathRewriteStripPrefixOption")}
</SelectItem>
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>
<div className="grid gap-2"> <div className="grid gap-2">
<Label htmlFor="rewrite-value">Rewrite Value</Label> <Label htmlFor="rewrite-value">{t("pathRewriteValue")}</Label>
<Input <Input
id="rewrite-value" id="rewrite-value"
placeholder={getPlaceholder()} placeholder={getPlaceholder()}
value={rewritePath} value={rewritePath}
onChange={(e) => setRewritePath(e.target.value)} onChange={(e) => setRewritePath(e.target.value)}
/> />
<p className="text-sm text-muted-foreground">{getHelpText()}</p> <p className="text-sm text-muted-foreground">
{getHelpText()}
</p>
</div> </div>
</div> </div>
<CredenzaFooter className="gap-2"> <CredenzaFooter className="gap-2">
{value?.rewritePath && ( {value?.rewritePath && (
<Button variant="outline" onClick={handleClear}> <Button variant="outline" onClick={handleClear}>
Clear {t("clear")}
</Button> </Button>
)} )}
<Button <Button
onClick={handleSave} onClick={handleSave}
disabled={rewriteType !== "stripPrefix" && !rewritePath.trim()} disabled={
rewriteType !== "stripPrefix" && !rewritePath.trim()
}
> >
Save Changes {t("saveChanges")}
</Button> </Button>
</CredenzaFooter> </CredenzaFooter>
</CredenzaContent> </CredenzaContent>
@@ -226,17 +264,19 @@ export function PathRewriteModal({
} }
export function PathMatchDisplay({ export function PathMatchDisplay({
value, value
}: { }: {
value: { path: string | null; pathMatchType: string | null }; value: { path: string | null; pathMatchType: string | null };
}) { }) {
const t = useTranslations();
if (!value?.path) return null; if (!value?.path) return null;
const getTypeLabel = (type: string | null) => { const getTypeLabel = (type: string | null) => {
const labels: Record<string, string> = { const labels: Record<string, string> = {
prefix: "Prefix", prefix: t("pathMatchPrefix"),
exact: "Exact", exact: t("pathMatchExact"),
regex: "Regex", regex: t("pathMatchRegex")
}; };
return labels[type || ""] || type; return labels[type || ""] || type;
}; };
@@ -254,20 +294,22 @@ export function PathMatchDisplay({
); );
} }
export function PathRewriteDisplay({ export function PathRewriteDisplay({
value, value
}: { }: {
value: { rewritePath: string | null; rewritePathType: string | null }; value: { rewritePath: string | null; rewritePathType: string | null };
}) { }) {
if (!value?.rewritePath && value?.rewritePathType !== "stripPrefix") return null; const t = useTranslations();
if (!value?.rewritePath && value?.rewritePathType !== "stripPrefix")
return null;
const getTypeLabel = (type: string | null) => { const getTypeLabel = (type: string | null) => {
const labels: Record<string, string> = { const labels: Record<string, string> = {
prefix: "Prefix", prefix: t("pathRewritePrefix"),
exact: "Exact", exact: t("pathRewriteExact"),
regex: "Regex", regex: t("pathRewriteRegex"),
stripPrefix: "Strip", stripPrefix: t("pathRewriteStrip")
}; };
return labels[type || ""] || type; return labels[type || ""] || type;
}; };
@@ -277,8 +319,15 @@ export function PathRewriteDisplay({
<Badge variant="secondary" className="text-xs shrink-0"> <Badge variant="secondary" className="text-xs shrink-0">
{getTypeLabel(value.rewritePathType)} {getTypeLabel(value.rewritePathType)}
</Badge> </Badge>
<code className="text-sm flex-1 truncate" title={value.rewritePath || ""}> <code
{value.rewritePath || <span className="text-muted-foreground italic">(strip)</span>} className="text-sm flex-1 truncate"
title={value.rewritePath || ""}
>
{value.rewritePath || (
<span className="text-muted-foreground italic">
({t("pathRewriteStripLabel")})
</span>
)}
</code> </code>
<Settings className="h-4 w-4" /> <Settings className="h-4 w-4" />
</div> </div>