[go: nahoru, domu]

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

Pre-generate SSL Cert for Zero downtime migration #955

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions apps/web/app/api/domains/[domain]/certs/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { generateCert } from "@/lib/api/domains/generate-cert-vercel";
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";

// POST /api/domains/[domain]/certs – generate a certificate for a domain on Vercel
export const POST = withWorkspace(
async ({ domain }) => {
const data = await prisma.domain.findUnique({
where: {
slug: domain,
},
});

if (!data) {
throw new DubApiError({
code: "not_found",
message: "Domain not found",
});
}

const response = await generateCert(domain);

console.log("response", response);

return NextResponse.json(response);
},
{
domainChecks: true,
},
);
31 changes: 31 additions & 0 deletions apps/web/lib/api/domains/generate-cert-vercel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getDomainWithoutWWW } from "@dub/utils";
import { DubApiError } from "../errors";

export const generateCert = async (domain: string) => {
const response = await fetch(
`https://api.vercel.com/v7/certs?teamId=${process.env.TEAM_ID_VERCEL}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
cns: [getDomainWithoutWWW(domain)],
}),
},
);

const data = await response.json();

console.log("data", data);

if (!response.ok) {
throw new DubApiError({
code: "bad_request",
message: data.error.name,
});
}

return await response.json();
};
32 changes: 32 additions & 0 deletions apps/web/ui/domains/domain-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import { Archive, Edit3, FileCog, FolderInput, QrCode } from "lucide-react";
import Link from "next/link";
import { useRef, useState } from "react";
import { toast } from "sonner";
import useSWR from "swr";
import useSWRImmutable from "swr/immutable";
import { useAddEditDomainModal } from "../modals/add-edit-domain-modal";
Expand Down Expand Up @@ -104,6 +105,30 @@ export default function DomainCard({ props }: { props: DomainProps }) {
props,
});

const [generatingCert, setGeneratingCert] = useState(false);

// Generate the cert for the domain
const generateCert = async () => {
setGeneratingCert(true);
const response = await fetch(
`/api/domains/${domain}/certs?workspaceId=${workspaceId}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);

if (!response.ok) {
const { error } = await response.json();
toast.error(error.message);
} else {
toast.success("A new certificate has been generated for this domain.");
}
setGeneratingCert(false);
};

const activeDomainsCount = activeWorkspaceDomains?.length || 0;

return (
Expand Down Expand Up @@ -162,6 +187,13 @@ export default function DomainCard({ props }: { props: DomainProps }) {
mutate();
}}
/>
<Button
text="Generate cert"
variant="secondary"
=> generateCert()}
loading={generatingCert}
className="whitespace-nowrap"
/>
<Popover
content={
<div className="grid w-full gap-px p-2 sm:w-44">
Expand Down
Loading