dynamic worker routes based on env

This commit is contained in:
Ishaan Dey
2024-05-26 18:37:36 -07:00
parent 6285a68102
commit 6376493ae7
17 changed files with 427 additions and 260 deletions

View File

@ -4,6 +4,10 @@ NEXT_PUBLIC_LIVEBLOCKS_PUBLIC_KEY=
LIVEBLOCKS_SECRET_KEY=
NEXT_PUBLIC_SERVER_PORT=4000
NEXT_PUBLIC_APP_URL=http://localhost:3000
NEXT_PUBLIC_DATABASE_WORKER_URL=
NEXT_PUBLIC_STORAGE_WORKER_URL=
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up

View File

@ -11,7 +11,12 @@ export const revalidate = 0
const getUserData = async (id: string) => {
const userRes = await fetch(
`https://database.ishaan1013.workers.dev/api/user?id=${id}`
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
const userData: User = await userRes.json()
return userData
@ -19,7 +24,12 @@ const getUserData = async (id: string) => {
const getSandboxData = async (id: string) => {
const sandboxRes = await fetch(
`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox?id=${id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
const sandboxData: Sandbox = await sandboxRes.json()
return sandboxData
@ -33,7 +43,12 @@ const getSharedUsers = async (usersToSandboxes: UsersToSandboxes[]) => {
const shared = await Promise.all(
usersToSandboxes.map(async (user) => {
const userRes = await fetch(
`https://database.ishaan1013.workers.dev/api/user?id=${user.userId}`
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${user.userId}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
const userData: User = await userRes.json()
return { id: userData.id, name: userData.name }

View File

@ -12,12 +12,22 @@ export default async function DashboardPage() {
}
const userRes = await fetch(
`https://database.ishaan1013.workers.dev/api/user?id=${user.id}`
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${user.id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
const userData = (await userRes.json()) as User
const sharedRes = await fetch(
`https://database.ishaan1013.workers.dev/api/sandbox/share?id=${user.id}`
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox/share?id=${user.id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
const shared = (await sharedRes.json()) as {
id: string

View File

@ -1,30 +1,36 @@
import { User } from "@/lib/types";
import { currentUser } from "@clerk/nextjs";
import { redirect } from "next/navigation";
import { User } from "@/lib/types"
import { currentUser } from "@clerk/nextjs"
import { redirect } from "next/navigation"
export default async function AppAuthLayout({
children,
}: {
children: React.ReactNode;
children: React.ReactNode
}) {
const user = await currentUser();
const user = await currentUser()
if (!user) {
redirect("/");
redirect("/")
}
const dbUser = await fetch(
`https://database.ishaan1013.workers.dev/api/user?id=${user.id}`
);
const dbUserJSON = (await dbUser.json()) as User;
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${user.id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
const dbUserJSON = (await dbUser.json()) as User
if (!dbUserJSON.id) {
const res = await fetch(
"https://database.ishaan1013.workers.dev/api/user",
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
body: JSON.stringify({
id: user.id,
@ -32,8 +38,8 @@ export default async function AppAuthLayout({
email: user.emailAddresses[0].emailAddress,
}),
}
);
)
}
return <>{children}</>;
return <>{children}</>
}

View File

@ -18,7 +18,12 @@ export async function POST(request: NextRequest) {
}
const res = await fetch(
`https://database.ishaan1013.workers.dev/api/user?id=${clerkUser.id}`
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${clerkUser.id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
const user = (await res.json()) as User

View File

@ -60,9 +60,7 @@ export default function GenerateInput({
regenerate?: boolean
}) => {
if (user.generations >= 10) {
toast.error(
"You reached the maximum # of generations. Contact @ishaandey_ on X/Twitter to reset :)"
)
toast.error("You reached the maximum # of generations.")
return
}

View File

@ -51,8 +51,7 @@ export default function Loading({
</DialogTitle>
{didFail ? (
<DialogDescription className="pt-2">
Try again in a minute, or contact @ishaandey_ on Twitter/X if it
still doesn't work.
Try again soon.
</DialogDescription>
) : description ? (
<DialogDescription className="pt-2">

View File

@ -75,13 +75,17 @@ export default function ShareSandboxModal({
{data.visibility === "private" ? (
<DialogDescription className="text-sm text-muted-foreground">
This sandbox is private. Making it public will allow shared
users to view and collaborate. You can still share & manage access below.
users to view and collaborate. You can still share & manage
access below.
</DialogDescription>
) : null}
</DialogHeader>
<div className="flex space-x-4 w-full">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="flex w-full">
<form
onSubmit={form.handleSubmit(onSubmit)}
className="flex w-full"
>
<FormField
control={form.control}
name="email"
@ -101,7 +105,8 @@ export default function ShareSandboxModal({
<Button disabled={loading} type="submit" className="">
{loading ? (
<>
<Loader2 className="animate-spin mr-2 h-4 w-4" /> Loading...
<Loader2 className="animate-spin mr-2 h-4 w-4" />{" "}
Loading...
</>
) : (
<>
@ -111,11 +116,19 @@ export default function ShareSandboxModal({
</Button>
</form>
</Form>
<Button onClick={() => {
navigator.clipboard.writeText(`https://s.ishaand.com/code/${data.id}`)
toast.success("Link copied to clipboard.")
}} size="icon" disabled={loading} variant="outline" className="shrink-0">
<Link className="h-4 w-4" />
<Button
onClick={() => {
navigator.clipboard.writeText(
`${process.env.NEXT_PUBLIC_APP_URL}/code/${data.id}`
)
toast.success("Link copied to clipboard.")
}}
size="icon"
disabled={loading}
variant="outline"
className="shrink-0"
>
<Link className="h-4 w-4" />
</Button>
</div>
</div>

View File

@ -9,11 +9,12 @@ export async function createSandbox(body: {
visibility: string
}) {
const res = await fetch(
"https://database.ishaan1013.workers.dev/api/sandbox",
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
body: JSON.stringify(body),
}
@ -27,10 +28,11 @@ export async function updateSandbox(body: {
name?: string
visibility?: "public" | "private"
}) {
await fetch("https://database.ishaan1013.workers.dev/api/sandbox", {
await fetch(`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
body: JSON.stringify(body),
})
@ -39,20 +41,27 @@ export async function updateSandbox(body: {
}
export async function deleteSandbox(id: string) {
await fetch(`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`, {
method: "DELETE",
})
await fetch(
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox?id=${id}`,
{
method: "DELETE",
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
)
revalidatePath("/dashboard")
}
export async function shareSandbox(sandboxId: string, email: string) {
const res = await fetch(
"https://database.ishaan1013.workers.dev/api/sandbox/share",
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox/share`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
body: JSON.stringify({ sandboxId, email }),
}
@ -68,13 +77,17 @@ export async function shareSandbox(sandboxId: string, email: string) {
}
export async function unshareSandbox(sandboxId: string, userId: string) {
await fetch("https://database.ishaan1013.workers.dev/api/sandbox/share", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ sandboxId, userId }),
})
await fetch(
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox/share`,
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
body: JSON.stringify({ sandboxId, userId }),
}
)
revalidatePath(`/code/${sandboxId}`)
}