Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wildcard domain support #871

Open
wants to merge 2 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const baseAdmin: Admin = {
serverIp: null,
certificateType: "none",
host: null,
wildcardDomain: null,
letsEncryptEmail: null,
sshPrivateKey: null,
enableDockerCleanup: false,
Expand Down
28 changes: 28 additions & 0 deletions apps/dokploy/components/dashboard/settings/web-domain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const addServerDomain = z
domain: z.string().min(1, { message: "URL is required" }),
letsEncryptEmail: z.string(),
certificateType: z.enum(["letsencrypt", "none"]),
wildcardDomain: z.string().optional(),
})
.superRefine((data, ctx) => {
if (data.certificateType === "letsencrypt" && !data.letsEncryptEmail) {
Expand All @@ -60,6 +61,7 @@ export const WebDomain = () => {
domain: "",
certificateType: "none",
letsEncryptEmail: "",
wildcardDomain: "",
},
resolver: zodResolver(addServerDomain),
});
Expand All @@ -69,6 +71,7 @@ export const WebDomain = () => {
domain: user?.host || "",
certificateType: user?.certificateType,
letsEncryptEmail: user?.letsEncryptEmail || "",
wildcardDomain: user?.wildcardDomain || "",
});
}
}, [form, form.reset, user]);
Expand All @@ -78,6 +81,7 @@ export const WebDomain = () => {
host: data.domain,
letsEncryptEmail: data.letsEncryptEmail,
certificateType: data.certificateType,
wildcardDomain: data.wildcardDomain,
Comment on lines 81 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not updating the value in the database, check in the backend mutation how the data is received.

})
.then(async () => {
await refetch();
Expand Down Expand Up @@ -126,6 +130,30 @@ export const WebDomain = () => {
}}
/>

<FormField
control={form.control}
name="wildcardDomain"
render={({ field }) => {
return (
<FormItem>
<FormLabel>
{t("settings.server.domain.form.wildcardDomain")}
</FormLabel>
<FormControl>
<Input
className="w-full"
placeholder={t(
"settings.server.domain.form.wildcardDomain.placeholder",
)}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
);
}}
/>

<FormField
control={form.control}
name="letsEncryptEmail"
Expand Down
1 change: 1 addition & 0 deletions apps/dokploy/drizzle/0045_add_custom_domain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "admin" ADD COLUMN "wildcardDomain" text;
2 changes: 2 additions & 0 deletions apps/dokploy/public/locales/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"settings.server.domain.form.certificate.placeholder": "Select a certificate",
"settings.server.domain.form.certificateOptions.none": "None",
"settings.server.domain.form.certificateOptions.letsencrypt": "Let's Encrypt (Default)",
"settings.server.domain.form.wildcardDomain": "Wildcard Domain",
"settings.server.domain.form.wildcardDomain.placeholder": "traefik.me",

"settings.server.webServer.title": "Web Server",
"settings.server.webServer.description": "Reload or clean the web server.",
Expand Down
16 changes: 8 additions & 8 deletions apps/dokploy/templates/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,8 @@ export const templates: TemplateData[] = [
},
tags: ["forum", "community", "discussion"],
load: () => import("./discourse/index").then((m) => m.generate),
},
{
},
{
id: "immich",
name: "Immich",
version: "v1.121.0",
Expand All @@ -896,8 +896,8 @@ export const templates: TemplateData[] = [
},
tags: ["photos", "videos", "backup", "media"],
load: () => import("./immich/index").then((m) => m.generate),
},
{
},
{
id: "twenty",
name: "Twenty CRM",
version: "latest",
Expand All @@ -911,8 +911,8 @@ export const templates: TemplateData[] = [
},
tags: ["crm", "sales", "business"],
load: () => import("./twenty/index").then((m) => m.generate),
},
{
},
{
id: "yourls",
name: "YOURLS",
version: "1.9.2",
Expand All @@ -926,8 +926,8 @@ export const templates: TemplateData[] = [
},
tags: ["url-shortener", "php"],
load: () => import("./yourls/index").then((m) => m.generate),
},
{
},
{
id: "ryot",
name: "Ryot",
version: "v7.10",
Expand Down
6 changes: 6 additions & 0 deletions apps/dokploy/templates/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { TemplatesKeys } from "../types/templates-data.type";
export interface Schema {
serverIp: string;
projectName: string;
wildcardDomain?: string | null;
}

export type DomainSchema = Pick<Domain, "host" | "port" | "serviceName">;
Expand All @@ -26,10 +27,15 @@ export interface Template {
export const generateRandomDomain = ({
serverIp,
projectName,
wildcardDomain,
}: Schema): string => {
const hash = randomBytes(3).toString("hex");
const slugIp = serverIp.replaceAll(".", "-");

if (wildcardDomain) {
return `${projectName}.${wildcardDomain}`;
}

return `${projectName}-${hash}${slugIp === "" ? "" : `-${slugIp}`}.traefik.me`;
};

Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/db/schema/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const admins = pgTable("admin", {
serverIp: text("serverIp"),
certificateType: certificateType("certificateType").notNull().default("none"),
host: text("host"),
wildcardDomain: text("wildcardDomain"),
letsEncryptEmail: text("letsEncryptEmail"),
sshPrivateKey: text("sshPrivateKey"),
enableDockerCleanup: boolean("enableDockerCleanup").notNull().default(false),
Expand Down Expand Up @@ -51,6 +52,7 @@ const createSchema = createInsertSchema(admins, {
certificateType: z.enum(["letsencrypt", "none"]).default("none"),
serverIp: z.string().optional(),
letsEncryptEmail: z.string().optional(),
wildcardDomain: z.string().optional(),
});

export const apiUpdateAdmin = createSchema.partial();
Expand All @@ -66,10 +68,12 @@ export const apiAssignDomain = createSchema
host: true,
certificateType: true,
letsEncryptEmail: true,
wildcardDomain: true,
})
.required()
.partial({
letsEncryptEmail: true,
wildcardDomain: true,
});

export const apiUpdateDockerCleanup = createSchema
Expand Down
5 changes: 5 additions & 0 deletions packages/server/src/services/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,27 @@ export const generateTraefikMeDomain = async (
) => {
if (serverId) {
const server = await findServerById(serverId);
const admin = await findAdminById(adminId);
return generateRandomDomain({
serverIp: server.ipAddress,
projectName: appName,
wildcardDomain: admin?.wildcardDomain,
});
}

if (process.env.NODE_ENV === "development") {
const admin = await findAdminById(adminId);
return generateRandomDomain({
serverIp: "",
projectName: appName,
wildcardDomain: admin?.wildcardDomain,
});
}
const admin = await findAdminById(adminId);
return generateRandomDomain({
serverIp: admin?.serverIp || "",
projectName: appName,
wildcardDomain: admin?.wildcardDomain,
});
};

Expand Down
6 changes: 6 additions & 0 deletions packages/server/src/templates/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Domain } from "@dokploy/server/services/domain";
export interface Schema {
serverIp: string;
projectName: string;
wildcardDomain?: string | null;
}

export type DomainSchema = Pick<Domain, "host" | "port" | "serviceName">;
Expand All @@ -22,10 +23,15 @@ export interface Template {
export const generateRandomDomain = ({
serverIp,
projectName,
wildcardDomain,
}: Schema): string => {
const hash = randomBytes(3).toString("hex");
const slugIp = serverIp.replaceAll(".", "-");

if (wildcardDomain) {
return `${projectName}.${wildcardDomain}`;
}

return `${projectName}-${hash}${slugIp === "" ? "" : `-${slugIp}`}.traefik.me`;
};

Expand Down
Loading