|
| 1 | +import { serve } from "@hono/node-server"; |
| 2 | +import { Hono } from "hono"; |
| 3 | +import { exec } from "node:child_process"; |
| 4 | +import { promisify } from "node:util"; |
| 5 | +import * as fs from "node:fs/promises"; |
| 6 | +import * as path from "node:path"; |
| 7 | +import temp from "temp"; |
| 8 | +import { RivetClient } from "@rivet-gg/api-full"; |
| 9 | + |
| 10 | +// Auto-track and cleanup temp directories/files |
| 11 | +temp.track(); |
| 12 | +const execAsync = promisify(exec); |
| 13 | + |
| 14 | +// Config (should be moved to env vars in production) |
| 15 | +const PORT = process.env.PORT || 3000; |
| 16 | +const RIVET_CLOUD_TOKEN = process.env.RIVET_CLOUD_TOKEN; |
| 17 | +const RIVET_PROJECT = process.env.RIVET_PROJECT; |
| 18 | +const RIVET_ENVIRONMENT = process.env.RIVET_ENVIRONMENT; |
| 19 | + |
| 20 | +if (!RIVET_CLOUD_TOKEN || !RIVET_PROJECT || !RIVET_ENVIRONMENT) { |
| 21 | + console.error( |
| 22 | + "Missing required environment variables: RIVET_CLOUD_TOKEN, RIVET_PROJECT, RIVET_ENVIRONMENT", |
| 23 | + ); |
| 24 | + process.exit(1); |
| 25 | +} |
| 26 | + |
| 27 | +const rivet = new RivetClient({ token: RIVET_CLOUD_TOKEN }); |
| 28 | + |
| 29 | +const app = new Hono(); |
| 30 | + |
| 31 | +app.onError((err, c) => { |
| 32 | + console.error("Error during operation:", err); |
| 33 | + return c.json( |
| 34 | + { |
| 35 | + error: "Operation failed", |
| 36 | + message: err instanceof Error ? err.message : String(err), |
| 37 | + }, |
| 38 | + 500, |
| 39 | + ); |
| 40 | +}); |
| 41 | + |
| 42 | +app.get("/", (c) => { |
| 43 | + return c.text("Multitenant Deploy Service"); |
| 44 | +}); |
| 45 | + |
| 46 | +app.post("/deploy", async (c) => { |
| 47 | + // Get the form data |
| 48 | + const formData = await c.req.formData(); |
| 49 | + const appId = formData.get("appId"); |
| 50 | + |
| 51 | + if (!appId || typeof appId !== "string") { |
| 52 | + return c.json({ error: "Missing or invalid appId" }, 400); |
| 53 | + } |
| 54 | + |
| 55 | + // Validate app ID (alphanumeric and hyphens only, 3-30 chars) |
| 56 | + if (!/^[a-z0-9-]{3,30}$/.test(appId)) { |
| 57 | + return c.json( |
| 58 | + { |
| 59 | + error: "Invalid appId format. Must be 3-30 characters, lowercase alphanumeric with hyphens.", |
| 60 | + }, |
| 61 | + 400, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + // Create a temp directory for the files |
| 66 | + const tempDir = await temp.mkdir("rivet-deploy-"); |
| 67 | + |
| 68 | + // Process and save each file |
| 69 | + let hasDockerfile = false; |
| 70 | + for (const [fieldName, value] of formData.entries()) { |
| 71 | + // Skip non-file fields |
| 72 | + if (!(value instanceof File)) continue; |
| 73 | + |
| 74 | + const filePath = path.join(tempDir, fieldName); |
| 75 | + |
| 76 | + // Ensure parent directory exists |
| 77 | + await fs.mkdir(path.dirname(filePath), { recursive: true }); |
| 78 | + |
| 79 | + // Write the file |
| 80 | + await fs.writeFile(filePath, Buffer.from(await value.arrayBuffer())); |
| 81 | + |
| 82 | + // Check if this is a Dockerfile |
| 83 | + if (fieldName === "Dockerfile" || fieldName.endsWith("/Dockerfile")) { |
| 84 | + hasDockerfile = true; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (!hasDockerfile) { |
| 89 | + return c.json({ error: "Dockerfile is required" }, 400); |
| 90 | + } |
| 91 | + |
| 92 | + // Run rivet publish command |
| 93 | + console.log(`Publishing app ${appId} from ${tempDir}...`); |
| 94 | + const tags = { |
| 95 | + // Differentiates from other actor types |
| 96 | + kind: "app", |
| 97 | + // Specify which app this is |
| 98 | + app: appId, |
| 99 | + }; |
| 100 | + const publishResult = await execAsync( |
| 101 | + `RIVET_CLOUD_TOKEN=${RIVET_CLOUD_TOKEN} rivet publish ${appId} ${tempDir} -e ${RIVET_ENVIRONMENT}`, |
| 102 | + { maxBuffer: 10 * 1024 * 1024 }, // 10MB buffer for output |
| 103 | + ); |
| 104 | + |
| 105 | + console.log("Publish output:", publishResult.stdout); |
| 106 | + |
| 107 | + // TODO: Create containers |
| 108 | + // TODO: Attach env vars |
| 109 | + |
| 110 | + // Update the route |
| 111 | + const hostname = `${appId}.rivet.run`; |
| 112 | + rivet.routes.update(`app-${appId}`, { |
| 113 | + project: RIVET_PROJECT, |
| 114 | + environment: RIVET_ENVIRONMENT, |
| 115 | + body: { |
| 116 | + hostname, |
| 117 | + path: "/", |
| 118 | + stripPrefix: false, |
| 119 | + routeSubpaths: true, |
| 120 | + target: { actors: { selectorTags: tags } }, |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + return c.json({ |
| 125 | + success: true, |
| 126 | + appId, |
| 127 | + hostname, |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +console.log(`Server starting on port ${PORT}...`); |
| 132 | +serve({ |
| 133 | + fetch: app.fetch, |
| 134 | + port: Number(PORT), |
| 135 | +}); |
0 commit comments