mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
Merge pull request #977 from drudge/ports-ui
feat(traefik/ports): improved UI
This commit is contained in:
@@ -80,8 +80,10 @@ export const EditTraefikEnv = ({ children, serverId }: Props) => {
|
|||||||
<DialogTrigger asChild>{children}</DialogTrigger>
|
<DialogTrigger asChild>{children}</DialogTrigger>
|
||||||
<DialogContent className="max-h-screen overflow-y-auto sm:max-w-4xl">
|
<DialogContent className="max-h-screen overflow-y-auto sm:max-w-4xl">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Update Traefik Env</DialogTitle>
|
<DialogTitle>Update Traefik Environment</DialogTitle>
|
||||||
<DialogDescription>Update the traefik env</DialogDescription>
|
<DialogDescription>
|
||||||
|
Update the traefik environment variables
|
||||||
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
|
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
|
import { AlertBlock } from "@/components/shared/alert-block";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Card, CardContent } from "@/components/ui/card";
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
DialogDescription,
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
DialogHeader,
|
DialogHeader,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Input } from "@/components/ui/input";
|
||||||
import {
|
import {
|
||||||
Select,
|
Select,
|
||||||
SelectContent,
|
SelectContent,
|
||||||
@@ -14,59 +17,56 @@ import {
|
|||||||
SelectTrigger,
|
SelectTrigger,
|
||||||
SelectValue,
|
SelectValue,
|
||||||
} from "@/components/ui/select";
|
} from "@/components/ui/select";
|
||||||
|
import {
|
||||||
|
Form,
|
||||||
|
FormControl,
|
||||||
|
FormField,
|
||||||
|
FormItem,
|
||||||
|
FormLabel,
|
||||||
|
FormMessage,
|
||||||
|
} from "@/components/ui/form";
|
||||||
import { api } from "@/utils/api";
|
import { api } from "@/utils/api";
|
||||||
|
import { ArrowRightLeft, Plus, Trash2 } from "lucide-react";
|
||||||
import { useTranslation } from "next-i18next";
|
import { useTranslation } from "next-i18next";
|
||||||
import type React from "react";
|
import type React from "react";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { useFieldArray, useForm } from "react-hook-form";
|
||||||
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
|
|
||||||
/**
|
|
||||||
* Props for the ManageTraefikPorts component
|
|
||||||
* @interface Props
|
|
||||||
* @property {React.ReactNode} children - The trigger element that opens the ports management modal
|
|
||||||
* @property {string} [serverId] - Optional ID of the server whose ports are being managed
|
|
||||||
*/
|
|
||||||
interface Props {
|
interface Props {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
serverId?: string;
|
serverId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
const PortSchema = z.object({
|
||||||
* Represents a port mapping configuration for Traefik
|
targetPort: z.number().min(1, "Target port is required"),
|
||||||
* @interface AdditionalPort
|
publishedPort: z.number().min(1, "Published port is required"),
|
||||||
* @property {number} targetPort - The internal port that the service is listening on
|
publishMode: z.enum(["ingress", "host"]),
|
||||||
* @property {number} publishedPort - The external port that will be exposed
|
});
|
||||||
* @property {"ingress" | "host"} publishMode - The Docker Swarm publish mode:
|
|
||||||
* - "host": Publishes the port directly on the host
|
const TraefikPortsSchema = z.object({
|
||||||
* - "ingress": Publishes the port through the Swarm routing mesh
|
ports: z.array(PortSchema),
|
||||||
*/
|
});
|
||||||
interface AdditionalPort {
|
|
||||||
targetPort: number;
|
type TraefikPortsForm = z.infer<typeof TraefikPortsSchema>;
|
||||||
publishedPort: number;
|
|
||||||
publishMode: "ingress" | "host";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ManageTraefikPorts is a component that provides a modal interface for managing
|
|
||||||
* additional port mappings for Traefik in a Docker Swarm environment.
|
|
||||||
*
|
|
||||||
* Features:
|
|
||||||
* - Add, remove, and edit port mappings
|
|
||||||
* - Configure target port, published port, and publish mode for each mapping
|
|
||||||
* - Persist port configurations through API calls
|
|
||||||
*
|
|
||||||
* @component
|
|
||||||
* @example
|
|
||||||
* ```tsx
|
|
||||||
* <ManageTraefikPorts serverId="server-123">
|
|
||||||
* <Button>Manage Ports</Button>
|
|
||||||
* </ManageTraefikPorts>
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
||||||
const { t } = useTranslation("settings");
|
const { t } = useTranslation("settings");
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const [additionalPorts, setAdditionalPorts] = useState<AdditionalPort[]>([]);
|
|
||||||
|
const form = useForm<TraefikPortsForm>({
|
||||||
|
resolver: zodResolver(TraefikPortsSchema),
|
||||||
|
defaultValues: {
|
||||||
|
ports: [],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { fields, append, remove } = useFieldArray({
|
||||||
|
control: form.control,
|
||||||
|
name: "ports",
|
||||||
|
});
|
||||||
|
|
||||||
const { data: currentPorts, refetch: refetchPorts } =
|
const { data: currentPorts, refetch: refetchPorts } =
|
||||||
api.settings.getTraefikPorts.useQuery({
|
api.settings.getTraefikPorts.useQuery({
|
||||||
@@ -82,22 +82,19 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (currentPorts) {
|
if (currentPorts) {
|
||||||
setAdditionalPorts(currentPorts);
|
form.reset({ ports: currentPorts });
|
||||||
}
|
}
|
||||||
}, [currentPorts]);
|
}, [currentPorts, form]);
|
||||||
|
|
||||||
const handleAddPort = () => {
|
const handleAddPort = () => {
|
||||||
setAdditionalPorts([
|
append({ targetPort: 0, publishedPort: 0, publishMode: "host" });
|
||||||
...additionalPorts,
|
|
||||||
{ targetPort: 0, publishedPort: 0, publishMode: "host" },
|
|
||||||
]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUpdatePorts = async () => {
|
const onSubmit = async (data: TraefikPortsForm) => {
|
||||||
try {
|
try {
|
||||||
await updatePorts({
|
await updatePorts({
|
||||||
serverId,
|
serverId,
|
||||||
additionalPorts,
|
additionalPorts: data.ports,
|
||||||
});
|
});
|
||||||
toast.success(t("settings.server.webServer.traefik.portsUpdated"));
|
toast.success(t("settings.server.webServer.traefik.portsUpdated"));
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
@@ -110,121 +107,197 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
|
|||||||
<>
|
<>
|
||||||
<div onClick={() => setOpen(true)}>{children}</div>
|
<div onClick={() => setOpen(true)}>{children}</div>
|
||||||
<Dialog open={open} onOpenChange={setOpen}>
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
<DialogContent className="sm:max-w-2xl">
|
<DialogContent className="sm:max-w-3xl">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>
|
<DialogTitle className="flex items-center gap-2 text-xl">
|
||||||
{t("settings.server.webServer.traefik.managePorts")}
|
{t("settings.server.webServer.traefik.managePorts")}
|
||||||
</DialogTitle>
|
</DialogTitle>
|
||||||
<DialogDescription>
|
<DialogDescription className="text-base w-full">
|
||||||
{t("settings.server.webServer.traefik.managePortsDescription")}
|
<div className="flex items-center justify-between">
|
||||||
|
{t("settings.server.webServer.traefik.managePortsDescription")}
|
||||||
|
<Button
|
||||||
|
onClick={handleAddPort}
|
||||||
|
variant="default"
|
||||||
|
className="gap-2"
|
||||||
|
>
|
||||||
|
<Plus className="h-4 w-4" />
|
||||||
|
Add Mapping
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<div className="grid gap-4 py-4">
|
|
||||||
{additionalPorts.map((port, index) => (
|
|
||||||
<div
|
|
||||||
key={index}
|
|
||||||
className="grid grid-cols-[120px_120px_minmax(120px,1fr)_80px] gap-4 items-end"
|
|
||||||
>
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor={`target-port-${index}`}>
|
|
||||||
{t("settings.server.webServer.traefik.targetPort")}
|
|
||||||
</Label>
|
|
||||||
<input
|
|
||||||
id={`target-port-${index}`}
|
|
||||||
type="number"
|
|
||||||
value={port.targetPort}
|
|
||||||
onChange={(e) => {
|
|
||||||
const newPorts = [...additionalPorts];
|
|
||||||
|
|
||||||
if (newPorts[index]) {
|
<Form {...form}>
|
||||||
newPorts[index].targetPort = Number.parseInt(
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
||||||
e.target.value,
|
<div className="grid gap-6 py-4">
|
||||||
);
|
{fields.length === 0 ? (
|
||||||
}
|
<div className="flex w-full flex-col items-center justify-center gap-3 pt-10">
|
||||||
|
<ArrowRightLeft className="size-8 text-muted-foreground" />
|
||||||
|
<span className="text-base text-muted-foreground text-center">
|
||||||
|
No port mappings configured
|
||||||
|
</span>
|
||||||
|
<p className="text-sm text-muted-foreground text-center">
|
||||||
|
Add one to get started
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="grid gap-4">
|
||||||
|
{fields.map((field, index) => (
|
||||||
|
<Card key={field.id}>
|
||||||
|
<CardContent className="grid grid-cols-[1fr_1fr_1.5fr_auto] gap-4 p-4 transparent">
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name={`ports.${index}.targetPort`}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel className="text-sm font-medium text-muted-foreground">
|
||||||
|
{t(
|
||||||
|
"settings.server.webServer.traefik.targetPort",
|
||||||
|
)}
|
||||||
|
</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
{...field}
|
||||||
|
onChange={(e) =>
|
||||||
|
field.onChange(Number(e.target.value))
|
||||||
|
}
|
||||||
|
className="w-full dark:bg-black"
|
||||||
|
placeholder="e.g. 8080"
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
setAdditionalPorts(newPorts);
|
<FormField
|
||||||
}}
|
control={form.control}
|
||||||
className="w-full rounded border p-2"
|
name={`ports.${index}.publishedPort`}
|
||||||
/>
|
render={({ field }) => (
|
||||||
</div>
|
<FormItem>
|
||||||
<div className="space-y-2">
|
<FormLabel className="text-sm font-medium text-muted-foreground">
|
||||||
<Label htmlFor={`published-port-${index}`}>
|
{t(
|
||||||
{t("settings.server.webServer.traefik.publishedPort")}
|
"settings.server.webServer.traefik.publishedPort",
|
||||||
</Label>
|
)}
|
||||||
<input
|
</FormLabel>
|
||||||
id={`published-port-${index}`}
|
<FormControl>
|
||||||
type="number"
|
<Input
|
||||||
value={port.publishedPort}
|
type="number"
|
||||||
onChange={(e) => {
|
{...field}
|
||||||
const newPorts = [...additionalPorts];
|
onChange={(e) =>
|
||||||
if (newPorts[index]) {
|
field.onChange(Number(e.target.value))
|
||||||
newPorts[index].publishedPort = Number.parseInt(
|
}
|
||||||
e.target.value,
|
className="w-full dark:bg-black"
|
||||||
);
|
placeholder="e.g. 80"
|
||||||
}
|
/>
|
||||||
setAdditionalPorts(newPorts);
|
</FormControl>
|
||||||
}}
|
<FormMessage />
|
||||||
className="w-full rounded border p-2"
|
</FormItem>
|
||||||
/>
|
)}
|
||||||
</div>
|
/>
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor={`publish-mode-${index}`}>
|
|
||||||
{t("settings.server.webServer.traefik.publishMode")}
|
|
||||||
</Label>
|
|
||||||
<Select
|
|
||||||
value={port.publishMode}
|
|
||||||
onValueChange={(value: "ingress" | "host") => {
|
|
||||||
const newPorts = [...additionalPorts];
|
|
||||||
|
|
||||||
if (newPorts[index]) {
|
<FormField
|
||||||
newPorts[index].publishMode = value;
|
control={form.control}
|
||||||
}
|
name={`ports.${index}.publishMode`}
|
||||||
setAdditionalPorts(newPorts);
|
render={({ field }) => (
|
||||||
}}
|
<FormItem>
|
||||||
>
|
<FormLabel className="text-sm font-medium text-muted-foreground">
|
||||||
<SelectTrigger
|
{t(
|
||||||
id={`publish-mode-${index}`}
|
"settings.server.webServer.traefik.publishMode",
|
||||||
className="w-full"
|
)}
|
||||||
>
|
</FormLabel>
|
||||||
<SelectValue />
|
<Select
|
||||||
</SelectTrigger>
|
onValueChange={field.onChange}
|
||||||
<SelectContent>
|
value={field.value}
|
||||||
<SelectItem value="host">Host</SelectItem>
|
>
|
||||||
<SelectItem value="ingress">Ingress</SelectItem>
|
<FormControl>
|
||||||
</SelectContent>
|
<SelectTrigger className="dark:bg-black">
|
||||||
</Select>
|
<SelectValue />
|
||||||
</div>
|
</SelectTrigger>
|
||||||
<div>
|
</FormControl>
|
||||||
<Button
|
<SelectContent>
|
||||||
onClick={() => {
|
<SelectItem value="host">
|
||||||
const newPorts = additionalPorts.filter(
|
Host Mode
|
||||||
(_, i) => i !== index,
|
</SelectItem>
|
||||||
);
|
<SelectItem value="ingress">
|
||||||
setAdditionalPorts(newPorts);
|
Ingress Mode
|
||||||
}}
|
</SelectItem>
|
||||||
variant="destructive"
|
</SelectContent>
|
||||||
size="sm"
|
</Select>
|
||||||
>
|
<FormMessage />
|
||||||
Remove
|
</FormItem>
|
||||||
</Button>
|
)}
|
||||||
</div>
|
/>
|
||||||
|
|
||||||
|
<div className="flex items-end">
|
||||||
|
<Button
|
||||||
|
onClick={() => remove(index)}
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="text-muted-foreground hover:text-destructive"
|
||||||
|
>
|
||||||
|
<Trash2 className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{fields.length > 0 && (
|
||||||
|
<AlertBlock type="info">
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<span className="text-sm">
|
||||||
|
<strong>
|
||||||
|
Each port mapping defines how external traffic reaches
|
||||||
|
your containers.
|
||||||
|
</strong>
|
||||||
|
<ul className="pt-2">
|
||||||
|
<li>
|
||||||
|
<strong>Host Mode:</strong> Directly binds the port
|
||||||
|
to the host machine.
|
||||||
|
<ul className="p-2 list-inside list-disc">
|
||||||
|
<li>
|
||||||
|
Best for single-node deployments or when you
|
||||||
|
need guaranteed port availability.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<strong>Ingress Mode:</strong> Routes through Docker
|
||||||
|
Swarm's load balancer.
|
||||||
|
<ul className="p-2 list-inside list-disc">
|
||||||
|
<li>
|
||||||
|
Recommended for multi-node deployments and
|
||||||
|
better scalability.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</AlertBlock>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
|
||||||
<div className="mt-4 flex justify-between">
|
<DialogFooter>
|
||||||
<Button onClick={handleAddPort} variant="outline" size="sm">
|
<Button
|
||||||
{t("settings.server.webServer.traefik.addPort")}
|
type="submit"
|
||||||
</Button>
|
variant="default"
|
||||||
<Button
|
className="text-sm"
|
||||||
onClick={handleUpdatePorts}
|
isLoading={isLoading}
|
||||||
size="sm"
|
>
|
||||||
disabled={isLoading}
|
Save
|
||||||
>
|
</Button>
|
||||||
Save
|
</DialogFooter>
|
||||||
</Button>
|
</form>
|
||||||
</div>
|
</Form>
|
||||||
</div>
|
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default ManageTraefikPorts;
|
||||||
|
|||||||
@@ -17,8 +17,8 @@
|
|||||||
"settings.server.webServer.updateServerIp": "Update Server IP",
|
"settings.server.webServer.updateServerIp": "Update Server IP",
|
||||||
"settings.server.webServer.server.label": "Server",
|
"settings.server.webServer.server.label": "Server",
|
||||||
"settings.server.webServer.traefik.label": "Traefik",
|
"settings.server.webServer.traefik.label": "Traefik",
|
||||||
"settings.server.webServer.traefik.modifyEnv": "Modify Env",
|
"settings.server.webServer.traefik.modifyEnv": "Modify Environment",
|
||||||
"settings.server.webServer.traefik.managePorts": "Additional Ports",
|
"settings.server.webServer.traefik.managePorts": "Additional Port Mappings",
|
||||||
"settings.server.webServer.traefik.managePortsDescription": "Add or remove additional ports for Traefik",
|
"settings.server.webServer.traefik.managePortsDescription": "Add or remove additional ports for Traefik",
|
||||||
"settings.server.webServer.traefik.targetPort": "Target Port",
|
"settings.server.webServer.traefik.targetPort": "Target Port",
|
||||||
"settings.server.webServer.traefik.publishedPort": "Published Port",
|
"settings.server.webServer.traefik.publishedPort": "Published Port",
|
||||||
|
|||||||
Reference in New Issue
Block a user