feat: add preview image builder

This commit is contained in:
BennyKok
2024-01-04 22:29:22 +08:00
parent 314eb9fd16
commit 9937252777
18 changed files with 1085 additions and 56 deletions
+13 -1
View File
@@ -1,4 +1,5 @@
import { insertMachineSchema } from "@/db/schema";
import { insertMachineSchema, machinesTable } from "@/db/schema";
import { createInsertSchema } from "drizzle-zod";
export const addMachineSchema = insertMachineSchema.pick({
name: true,
@@ -6,3 +7,14 @@ export const addMachineSchema = insertMachineSchema.pick({
type: true,
auth_token: true,
});
export const insertCustomMachineSchema = createInsertSchema(machinesTable, {
name: (schema) => schema.name.default("My Machine"),
type: (schema) => schema.type.default("modal-serverless"),
});
export const addCustomMachineSchema = insertCustomMachineSchema.pick({
name: true,
type: true,
snapshot: true,
});
+93 -2
View File
@@ -1,12 +1,17 @@
"use server";
import type { addMachineSchema } from "./addMachineSchema";
import type {
addCustomMachineSchema,
addMachineSchema,
} from "./addMachineSchema";
import { withServerPromise } from "./withServerPromise";
import { db } from "@/db/db";
import { machinesTable } from "@/db/schema";
import { auth } from "@clerk/nextjs";
import { and, eq } from "drizzle-orm";
import { and, eq, isNull } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import "server-only";
import type { z } from "zod";
@@ -27,6 +32,26 @@ export async function getMachines() {
return machines;
}
export async function getMachineById(id: string) {
const { userId, orgId } = auth();
if (!userId) throw new Error("No user id");
const machines = await db
.select()
.from(machinesTable)
.where(
and(
orgId
? eq(machinesTable.org_id, orgId)
: and(
eq(machinesTable.user_id, userId),
isNull(machinesTable.org_id)
),
eq(machinesTable.id, id)
)
);
return machines[0];
}
export const addMachine = withServerPromise(
async (data: z.infer<typeof addMachineSchema>) => {
const { userId, orgId } = auth();
@@ -42,6 +67,72 @@ export const addMachine = withServerPromise(
}
);
export const addCustomMachine = withServerPromise(
async (data: z.infer<typeof addCustomMachineSchema>) => {
const { userId, orgId } = auth();
const headersList = headers();
if (!userId) return { error: "No user id" };
// Insert to our db
const a = await db
.insert(machinesTable)
.values({
...data,
org_id: orgId,
user_id: userId,
status: "building",
endpoint: "not-ready",
})
.returning();
const b = a[0];
// const origin = new URL(request.url).origin;
const domain = headersList.get("x-forwarded-host") || "";
const protocol = headersList.get("x-forwarded-proto") || "";
// console.log("domain", domain);
// console.log("domain", `${protocol}://${domain}/api/machine-built`);
// return { message: "Machine Building" };
if (domain === "") {
throw new Error("No domain");
}
// Call remote builder
const result = await fetch(`${process.env.MODAL_BUILDER_URL!}/create`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
machine_id: b.id,
name: b.id,
snapshot: JSON.parse(data.snapshot as string),
callback_url: `${protocol}://${domain}/api/machine-built`,
}),
});
if (!result.ok) {
const error_log = await result.text();
await db
.update(machinesTable)
.set({
...data,
status: "error",
build_log: error_log,
})
.where(eq(machinesTable.id, b.id));
throw new Error(`Error: ${result.statusText} ${error_log}`);
}
redirect(`/machines/${b.id}`);
// revalidatePath("/machines");
return { message: "Machine Building" };
}
);
export const updateMachine = withServerPromise(
async ({
id,
+3
View File
@@ -1,5 +1,8 @@
import { isRedirectError } from "next/dist/client/components/redirect";
export async function wrapServerPromise<T>(result: Promise<T>) {
return result.catch((error) => {
if (isRedirectError(error)) throw error;
return {
error: error.message,
};