| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1 | import { PrismaClient } from "@prisma/client"; |
| 2 | import express from "express"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 3 | import fs from "node:fs"; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 4 | import { env } from "node:process"; |
| 5 | import axios from "axios"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 6 | import { GithubClient } from "./github.js"; |
| 7 | import { AppManager } from "./app_manager.js"; |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 8 | import { z } from "zod"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 9 | import { ProjectMonitor, WorkerSchema } from "./project_monitor.js"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 10 | import tmp from "tmp"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 11 | import { NodeJSAnalyzer } from "./lib/nodejs.js"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 12 | import shell from "shelljs"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 13 | import { RealFileSystem } from "./lib/fs.js"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 14 | import path from "node:path"; |
| gio | 9b7421a | 2025-06-18 12:31:13 +0000 | [diff] [blame] | 15 | import { |
| 16 | Env, |
| 17 | generateDodoConfig, |
| 18 | ConfigSchema, |
| 19 | AppNode, |
| 20 | ConfigWithInput, |
| 21 | configToGraph, |
| 22 | Network, |
| 23 | GithubRepository, |
| 24 | } from "config"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 25 | |
| 26 | async function generateKey(root: string): Promise<[string, string]> { |
| 27 | const privKeyPath = path.join(root, "key"); |
| 28 | const pubKeyPath = path.join(root, "key.pub"); |
| 29 | if (shell.exec(`ssh-keygen -t ed25519 -f ${privKeyPath} -N ""`).code !== 0) { |
| 30 | throw new Error("Failed to generate SSH key pair"); |
| 31 | } |
| 32 | const publicKey = await fs.promises.readFile(pubKeyPath, "utf8"); |
| 33 | const privateKey = await fs.promises.readFile(privKeyPath, "utf8"); |
| 34 | return [publicKey, privateKey]; |
| 35 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 36 | |
| 37 | const db = new PrismaClient(); |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 38 | const appManager = new AppManager(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 39 | |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 40 | const projectMonitors = new Map<number, ProjectMonitor>(); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 41 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 42 | const handleProjectCreate: express.Handler = async (req, resp) => { |
| 43 | try { |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 44 | const tmpDir = tmp.dirSync().name; |
| 45 | const [publicKey, privateKey] = await generateKey(tmpDir); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 46 | const { id } = await db.project.create({ |
| 47 | data: { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 48 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 49 | name: req.body.name, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 50 | deployKey: privateKey, |
| 51 | deployKeyPublic: publicKey, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 52 | }, |
| 53 | }); |
| 54 | resp.status(200); |
| 55 | resp.header("Content-Type", "application/json"); |
| 56 | resp.write( |
| 57 | JSON.stringify({ |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 58 | id: id.toString(), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 59 | }), |
| 60 | ); |
| 61 | } catch (e) { |
| 62 | console.log(e); |
| 63 | resp.status(500); |
| 64 | } finally { |
| 65 | resp.end(); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | const handleProjectAll: express.Handler = async (req, resp) => { |
| 70 | try { |
| 71 | const r = await db.project.findMany({ |
| 72 | where: { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 73 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 74 | }, |
| 75 | }); |
| 76 | resp.status(200); |
| 77 | resp.header("Content-Type", "application/json"); |
| 78 | resp.write( |
| 79 | JSON.stringify( |
| 80 | r.map((p) => ({ |
| 81 | id: p.id.toString(), |
| 82 | name: p.name, |
| 83 | })), |
| 84 | ), |
| 85 | ); |
| 86 | } catch (e) { |
| 87 | console.log(e); |
| 88 | resp.status(500); |
| 89 | } finally { |
| 90 | resp.end(); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | const handleSave: express.Handler = async (req, resp) => { |
| 95 | try { |
| 96 | await db.project.update({ |
| 97 | where: { |
| 98 | id: Number(req.params["projectId"]), |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 99 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 100 | }, |
| 101 | data: { |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 102 | draft: JSON.stringify(req.body), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 103 | }, |
| 104 | }); |
| 105 | resp.status(200); |
| 106 | } catch (e) { |
| 107 | console.log(e); |
| 108 | resp.status(500); |
| 109 | } finally { |
| 110 | resp.end(); |
| 111 | } |
| 112 | }; |
| 113 | |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 114 | function handleSavedGet(state: "deploy" | "draft"): express.Handler { |
| 115 | return async (req, resp) => { |
| 116 | try { |
| 117 | const r = await db.project.findUnique({ |
| 118 | where: { |
| 119 | id: Number(req.params["projectId"]), |
| 120 | userId: resp.locals.userId, |
| 121 | }, |
| 122 | select: { |
| 123 | state: true, |
| 124 | draft: true, |
| 125 | }, |
| 126 | }); |
| 127 | if (r == null) { |
| 128 | resp.status(404); |
| 129 | return; |
| 130 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 131 | resp.status(200); |
| 132 | resp.header("content-type", "application/json"); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 133 | let currentState: Record<string, unknown> | null = null; |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 134 | if (state === "deploy") { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 135 | if (r.state == null) { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 136 | currentState = { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 137 | nodes: [], |
| 138 | edges: [], |
| 139 | viewport: { x: 0, y: 0, zoom: 1 }, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 140 | }; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 141 | } else { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 142 | currentState = JSON.parse(Buffer.from(r.state).toString("utf8")); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 143 | } |
| 144 | } else { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 145 | if (r.draft == null) { |
| 146 | if (r.state == null) { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 147 | currentState = { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 148 | nodes: [], |
| 149 | edges: [], |
| 150 | viewport: { x: 0, y: 0, zoom: 1 }, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 151 | }; |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 152 | } else { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 153 | currentState = JSON.parse(Buffer.from(r.state).toString("utf8")); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 154 | } |
| 155 | } else { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 156 | currentState = JSON.parse(Buffer.from(r.draft).toString("utf8")); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 157 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 158 | } |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 159 | const env = await getEnv(Number(req.params["projectId"]), resp.locals.userId, resp.locals.username); |
| 160 | if (currentState) { |
| 161 | const config = generateDodoConfig( |
| 162 | req.params["projectId"].toString(), |
| 163 | currentState.nodes as AppNode[], |
| 164 | env, |
| 165 | ); |
| 166 | resp.send({ |
| 167 | state: currentState, |
| 168 | config, |
| 169 | }); |
| 170 | } |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 171 | } catch (e) { |
| 172 | console.log(e); |
| 173 | resp.status(500); |
| 174 | } finally { |
| 175 | resp.end(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 176 | } |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 177 | }; |
| 178 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 179 | |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 180 | const projectDeleteReqSchema = z.object({ |
| 181 | state: z.optional(z.nullable(z.string())), |
| 182 | }); |
| 183 | |
| 184 | const handleProjectDelete: express.Handler = async (req, resp) => { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 185 | try { |
| 186 | const projectId = Number(req.params["projectId"]); |
| 187 | const p = await db.project.findUnique({ |
| 188 | where: { |
| 189 | id: projectId, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 190 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 191 | }, |
| 192 | select: { |
| 193 | instanceId: true, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 194 | githubToken: true, |
| 195 | deployKeyPublic: true, |
| 196 | state: true, |
| 197 | draft: true, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 198 | }, |
| 199 | }); |
| 200 | if (p === null) { |
| 201 | resp.status(404); |
| 202 | return; |
| 203 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 204 | const parseResult = projectDeleteReqSchema.safeParse(req.body); |
| 205 | if (!parseResult.success) { |
| 206 | resp.status(400); |
| 207 | resp.write(JSON.stringify({ error: "Invalid request body", issues: parseResult.error.format() })); |
| 208 | return; |
| gio | e440db8 | 2025-05-13 12:21:44 +0000 | [diff] [blame] | 209 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 210 | if (p.githubToken && p.deployKeyPublic) { |
| 211 | const allRepos = [ |
| 212 | ...new Set([ |
| 213 | ...extractGithubRepos(p.state), |
| 214 | ...extractGithubRepos(p.draft), |
| 215 | ...extractGithubRepos(parseResult.data.state), |
| 216 | ]), |
| 217 | ]; |
| 218 | if (allRepos.length > 0) { |
| 219 | const diff: RepoDiff = { toDelete: allRepos, toAdd: [] }; |
| 220 | const github = new GithubClient(p.githubToken); |
| 221 | await manageGithubRepos(github, diff, p.deployKeyPublic, env.PUBLIC_ADDR); |
| 222 | console.log( |
| 223 | `Attempted to remove deploy keys for project ${projectId} from associated GitHub repositories.`, |
| 224 | ); |
| 225 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 226 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 227 | if (p.instanceId !== null) { |
| 228 | if (!(await appManager.removeInstance(p.instanceId))) { |
| 229 | resp.status(500); |
| 230 | resp.write(JSON.stringify({ error: "Failed to remove deployment from cluster" })); |
| 231 | return; |
| 232 | } |
| 233 | } |
| 234 | await db.project.delete({ |
| 235 | where: { |
| 236 | id: projectId, |
| 237 | }, |
| 238 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 239 | resp.status(200); |
| 240 | } catch (e) { |
| 241 | console.log(e); |
| 242 | resp.status(500); |
| 243 | } finally { |
| 244 | resp.end(); |
| 245 | } |
| 246 | }; |
| 247 | |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 248 | function extractGithubRepos(serializedState: string | null | undefined): string[] { |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 249 | if (!serializedState) { |
| 250 | return []; |
| 251 | } |
| 252 | try { |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 253 | const stateObj = JSON.parse(serializedState); |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 254 | const githubNodes = stateObj.nodes.filter( |
| 255 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 256 | (n: any) => n.type === "github" && n.data?.repository?.id, |
| 257 | ); |
| 258 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 259 | return githubNodes.map((n: any) => n.data.repository.sshURL); |
| 260 | } catch (error) { |
| 261 | console.error("Failed to parse state or extract GitHub repos:", error); |
| 262 | return []; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | type RepoDiff = { |
| 267 | toAdd?: string[]; |
| 268 | toDelete?: string[]; |
| 269 | }; |
| 270 | |
| 271 | function calculateRepoDiff(oldRepos: string[], newRepos: string[]): RepoDiff { |
| 272 | const toAdd = newRepos.filter((repo) => !oldRepos.includes(repo)); |
| 273 | const toDelete = oldRepos.filter((repo) => !newRepos.includes(repo)); |
| 274 | return { toAdd, toDelete }; |
| 275 | } |
| 276 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 277 | async function manageGithubRepos( |
| 278 | github: GithubClient, |
| 279 | diff: RepoDiff, |
| 280 | deployKey: string, |
| 281 | publicAddr?: string, |
| 282 | ): Promise<void> { |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 283 | for (const repoUrl of diff.toDelete ?? []) { |
| 284 | try { |
| 285 | await github.removeDeployKey(repoUrl, deployKey); |
| 286 | console.log(`Removed deploy key from repository ${repoUrl}`); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 287 | if (publicAddr) { |
| 288 | const webhookCallbackUrl = `${publicAddr}/api/webhook/github/push`; |
| 289 | await github.removePushWebhook(repoUrl, webhookCallbackUrl); |
| 290 | console.log(`Removed push webhook from repository ${repoUrl}`); |
| 291 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 292 | } catch (error) { |
| 293 | console.error(`Failed to remove deploy key from repository ${repoUrl}:`, error); |
| 294 | } |
| 295 | } |
| 296 | for (const repoUrl of diff.toAdd ?? []) { |
| 297 | try { |
| 298 | await github.addDeployKey(repoUrl, deployKey); |
| 299 | console.log(`Added deploy key to repository ${repoUrl}`); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 300 | if (publicAddr) { |
| 301 | const webhookCallbackUrl = `${publicAddr}/api/webhook/github/push`; |
| 302 | await github.addPushWebhook(repoUrl, webhookCallbackUrl); |
| 303 | console.log(`Added push webhook to repository ${repoUrl}`); |
| 304 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 305 | } catch (error) { |
| 306 | console.error(`Failed to add deploy key from repository ${repoUrl}:`, error); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 311 | const handleDeploy: express.Handler = async (req, resp) => { |
| 312 | try { |
| 313 | const projectId = Number(req.params["projectId"]); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 314 | const p = await db.project.findUnique({ |
| 315 | where: { |
| 316 | id: projectId, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 317 | // userId: resp.locals.userId, TODO(gio): validate |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 318 | }, |
| 319 | select: { |
| 320 | instanceId: true, |
| 321 | githubToken: true, |
| 322 | deployKey: true, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 323 | deployKeyPublic: true, |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 324 | state: true, |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 325 | geminiApiKey: true, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 326 | }, |
| 327 | }); |
| 328 | if (p === null) { |
| 329 | resp.status(404); |
| 330 | return; |
| 331 | } |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 332 | const config = ConfigSchema.safeParse(req.body.config); |
| 333 | if (!config.success) { |
| 334 | resp.status(400); |
| 335 | resp.write(JSON.stringify({ error: "Invalid configuration", issues: config.error.format() })); |
| 336 | return; |
| 337 | } |
| gio | 9b7421a | 2025-06-18 12:31:13 +0000 | [diff] [blame] | 338 | let repos: GithubRepository[] = []; |
| 339 | if (p.githubToken) { |
| 340 | const github = new GithubClient(p.githubToken); |
| 341 | repos = await github.getRepositories(); |
| 342 | } |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 343 | const state = req.body.state |
| 344 | ? JSON.stringify(req.body.state) |
| 345 | : JSON.stringify( |
| 346 | configToGraph( |
| 347 | config.data, |
| 348 | getNetworks(resp.locals.username), |
| gio | 9b7421a | 2025-06-18 12:31:13 +0000 | [diff] [blame] | 349 | repos, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 350 | p.state ? JSON.parse(Buffer.from(p.state).toString("utf8")) : null, |
| 351 | ), |
| 352 | ); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 353 | await db.project.update({ |
| 354 | where: { |
| 355 | id: projectId, |
| 356 | }, |
| 357 | data: { |
| 358 | draft: state, |
| 359 | }, |
| 360 | }); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 361 | let deployKey: string | null = p.deployKey; |
| 362 | let deployKeyPublic: string | null = p.deployKeyPublic; |
| 363 | if (deployKeyPublic == null) { |
| 364 | [deployKeyPublic, deployKey] = await generateKey(tmp.dirSync().name); |
| 365 | await db.project.update({ |
| 366 | where: { id: projectId }, |
| 367 | data: { deployKeyPublic, deployKey }, |
| 368 | }); |
| 369 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 370 | let diff: RepoDiff | null = null; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 371 | const cfg: ConfigWithInput = { |
| 372 | ...config.data, |
| 373 | input: { |
| 374 | appId: projectId.toString(), |
| 375 | managerAddr: env.INTERNAL_API_ADDR!, |
| 376 | key: { |
| 377 | public: deployKeyPublic!, |
| 378 | private: deployKey!, |
| 379 | }, |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 380 | geminiApiKey: p.geminiApiKey ?? undefined, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 381 | }, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 382 | }; |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 383 | try { |
| 384 | if (p.instanceId == null) { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 385 | const deployResponse = await appManager.deploy(cfg); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 386 | await db.project.update({ |
| 387 | where: { |
| 388 | id: projectId, |
| 389 | }, |
| 390 | data: { |
| 391 | state, |
| 392 | draft: null, |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 393 | instanceId: deployResponse.id, |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 394 | access: JSON.stringify(deployResponse.access), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 395 | }, |
| 396 | }); |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 397 | diff = { toAdd: extractGithubRepos(state) }; |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 398 | } else { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 399 | const deployResponse = await appManager.update(p.instanceId, cfg); |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 400 | diff = calculateRepoDiff(extractGithubRepos(p.state), extractGithubRepos(state)); |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 401 | await db.project.update({ |
| 402 | where: { |
| 403 | id: projectId, |
| 404 | }, |
| 405 | data: { |
| 406 | state, |
| 407 | draft: null, |
| 408 | access: JSON.stringify(deployResponse.access), |
| 409 | }, |
| 410 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 411 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 412 | if (diff && p.githubToken && deployKey) { |
| 413 | const github = new GithubClient(p.githubToken); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 414 | await manageGithubRepos(github, diff, deployKeyPublic!, env.PUBLIC_ADDR); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 415 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 416 | resp.status(200); |
| 417 | } catch (error) { |
| 418 | console.error("Deployment error:", error); |
| 419 | resp.status(500); |
| 420 | resp.write(JSON.stringify({ error: "Deployment failed" })); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 421 | } |
| 422 | } catch (e) { |
| 423 | console.log(e); |
| 424 | resp.status(500); |
| 425 | } finally { |
| 426 | resp.end(); |
| 427 | } |
| 428 | }; |
| 429 | |
| 430 | const handleStatus: express.Handler = async (req, resp) => { |
| 431 | try { |
| 432 | const projectId = Number(req.params["projectId"]); |
| 433 | const p = await db.project.findUnique({ |
| 434 | where: { |
| 435 | id: projectId, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 436 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 437 | }, |
| 438 | select: { |
| 439 | instanceId: true, |
| 440 | }, |
| 441 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 442 | if (p === null) { |
| 443 | resp.status(404); |
| 444 | return; |
| 445 | } |
| 446 | if (p.instanceId == null) { |
| 447 | resp.status(404); |
| 448 | return; |
| 449 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 450 | try { |
| 451 | const status = await appManager.getStatus(p.instanceId); |
| 452 | resp.status(200); |
| 453 | resp.write(JSON.stringify(status)); |
| 454 | } catch (error) { |
| 455 | console.error("Error getting status:", error); |
| 456 | resp.status(500); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 457 | } |
| 458 | } catch (e) { |
| 459 | console.log(e); |
| 460 | resp.status(500); |
| 461 | } finally { |
| 462 | resp.end(); |
| 463 | } |
| 464 | }; |
| 465 | |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 466 | const handleConfigGet: express.Handler = async (req, resp) => { |
| 467 | try { |
| 468 | const projectId = Number(req.params["projectId"]); |
| 469 | const project = await db.project.findUnique({ |
| 470 | where: { |
| 471 | id: projectId, |
| 472 | }, |
| 473 | select: { |
| 474 | state: true, |
| 475 | }, |
| 476 | }); |
| 477 | |
| 478 | if (!project || !project.state) { |
| 479 | resp.status(404).send({ error: "No deployed configuration found." }); |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | const state = JSON.parse(Buffer.from(project.state).toString("utf8")); |
| 484 | const env = await getEnv(projectId, resp.locals.userId, resp.locals.username); |
| 485 | const config = generateDodoConfig(projectId.toString(), state.nodes, env); |
| 486 | |
| 487 | if (!config) { |
| 488 | resp.status(500).send({ error: "Failed to generate configuration." }); |
| 489 | return; |
| 490 | } |
| 491 | resp.status(200).json(config); |
| 492 | } catch (e) { |
| 493 | console.log(e); |
| 494 | resp.status(500).send({ error: "Internal server error" }); |
| 495 | } finally { |
| 496 | console.log("config get done"); |
| 497 | resp.end(); |
| 498 | } |
| 499 | }; |
| 500 | |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 501 | const handleRemoveDeployment: express.Handler = async (req, resp) => { |
| 502 | try { |
| 503 | const projectId = Number(req.params["projectId"]); |
| 504 | const p = await db.project.findUnique({ |
| 505 | where: { |
| 506 | id: projectId, |
| 507 | userId: resp.locals.userId, |
| 508 | }, |
| 509 | select: { |
| 510 | instanceId: true, |
| 511 | githubToken: true, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 512 | deployKeyPublic: true, |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 513 | state: true, |
| 514 | draft: true, |
| 515 | }, |
| 516 | }); |
| 517 | if (p === null) { |
| 518 | resp.status(404); |
| 519 | resp.write(JSON.stringify({ error: "Project not found" })); |
| 520 | return; |
| 521 | } |
| 522 | if (p.instanceId == null) { |
| 523 | resp.status(400); |
| 524 | resp.write(JSON.stringify({ error: "Project not deployed" })); |
| 525 | return; |
| 526 | } |
| 527 | const removed = await appManager.removeInstance(p.instanceId); |
| 528 | if (!removed) { |
| 529 | resp.status(500); |
| 530 | resp.write(JSON.stringify({ error: "Failed to remove deployment from cluster" })); |
| 531 | return; |
| 532 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 533 | if (p.githubToken && p.deployKeyPublic && p.state) { |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 534 | try { |
| 535 | const github = new GithubClient(p.githubToken); |
| 536 | const repos = extractGithubRepos(p.state); |
| 537 | const diff = { toDelete: repos, toAdd: [] }; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 538 | await manageGithubRepos(github, diff, p.deployKeyPublic, env.PUBLIC_ADDR); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 539 | } catch (error) { |
| 540 | console.error("Error removing GitHub deploy keys:", error); |
| 541 | } |
| 542 | } |
| 543 | await db.project.update({ |
| 544 | where: { |
| 545 | id: projectId, |
| 546 | }, |
| 547 | data: { |
| 548 | instanceId: null, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 549 | deployKeyPublic: null, |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 550 | access: null, |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 551 | state: null, |
| 552 | draft: p.draft ?? p.state, |
| 553 | }, |
| 554 | }); |
| 555 | resp.status(200); |
| 556 | resp.write(JSON.stringify({ success: true })); |
| 557 | } catch (e) { |
| 558 | console.error("Error removing deployment:", e); |
| 559 | resp.status(500); |
| 560 | resp.write(JSON.stringify({ error: "Internal server error" })); |
| 561 | } finally { |
| 562 | resp.end(); |
| 563 | } |
| 564 | }; |
| 565 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 566 | const handleGithubRepos: express.Handler = async (req, resp) => { |
| 567 | try { |
| 568 | const projectId = Number(req.params["projectId"]); |
| 569 | const project = await db.project.findUnique({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 570 | where: { |
| 571 | id: projectId, |
| 572 | userId: resp.locals.userId, |
| 573 | }, |
| 574 | select: { |
| 575 | githubToken: true, |
| 576 | }, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 577 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 578 | if (!project?.githubToken) { |
| 579 | resp.status(400); |
| 580 | resp.write(JSON.stringify({ error: "GitHub token not configured" })); |
| 581 | return; |
| 582 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 583 | const github = new GithubClient(project.githubToken); |
| 584 | const repositories = await github.getRepositories(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 585 | resp.status(200); |
| 586 | resp.header("Content-Type", "application/json"); |
| 587 | resp.write(JSON.stringify(repositories)); |
| 588 | } catch (e) { |
| 589 | console.log(e); |
| 590 | resp.status(500); |
| 591 | resp.write(JSON.stringify({ error: "Failed to fetch repositories" })); |
| 592 | } finally { |
| 593 | resp.end(); |
| 594 | } |
| 595 | }; |
| 596 | |
| 597 | const handleUpdateGithubToken: express.Handler = async (req, resp) => { |
| 598 | try { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 599 | await db.project.update({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 600 | where: { |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 601 | id: Number(req.params["projectId"]), |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 602 | userId: resp.locals.userId, |
| 603 | }, |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 604 | data: { |
| 605 | githubToken: req.body.githubToken, |
| 606 | }, |
| 607 | }); |
| 608 | resp.status(200); |
| 609 | } catch (e) { |
| 610 | console.log(e); |
| 611 | resp.status(500); |
| 612 | } finally { |
| 613 | resp.end(); |
| 614 | } |
| 615 | }; |
| 616 | |
| 617 | const handleUpdateGeminiToken: express.Handler = async (req, resp) => { |
| 618 | try { |
| 619 | await db.project.update({ |
| 620 | where: { |
| 621 | id: Number(req.params["projectId"]), |
| 622 | userId: resp.locals.userId, |
| 623 | }, |
| 624 | data: { |
| 625 | geminiApiKey: req.body.geminiApiKey, |
| 626 | }, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 627 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 628 | resp.status(200); |
| 629 | } catch (e) { |
| 630 | console.log(e); |
| 631 | resp.status(500); |
| 632 | } finally { |
| 633 | resp.end(); |
| 634 | } |
| 635 | }; |
| 636 | |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 637 | const getNetworks = (username?: string | undefined): Network[] => { |
| 638 | return [ |
| 639 | { |
| 640 | name: "Trial", |
| 641 | domain: "trial.dodoapp.xyz", |
| 642 | hasAuth: false, |
| 643 | }, |
| 644 | // TODO(gio): Remove |
| 645 | ].concat( |
| 646 | username === "gio" || 1 == 1 |
| 647 | ? [ |
| 648 | { |
| 649 | name: "Public", |
| 650 | domain: "v1.dodo.cloud", |
| 651 | hasAuth: true, |
| 652 | }, |
| 653 | { |
| 654 | name: "Private", |
| 655 | domain: "p.v1.dodo.cloud", |
| 656 | hasAuth: true, |
| 657 | }, |
| 658 | ] |
| 659 | : [], |
| 660 | ); |
| 661 | }; |
| 662 | |
| 663 | const getEnv = async (projectId: number, userId: string, username: string): Promise<Env> => { |
| 664 | const project = await db.project.findUnique({ |
| 665 | where: { |
| 666 | id: projectId, |
| 667 | userId, |
| 668 | }, |
| 669 | select: { |
| 670 | deployKeyPublic: true, |
| 671 | githubToken: true, |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 672 | geminiApiKey: true, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 673 | access: true, |
| 674 | instanceId: true, |
| 675 | }, |
| 676 | }); |
| 677 | if (!project) { |
| 678 | throw new Error("Project not found"); |
| 679 | } |
| 680 | const monitor = projectMonitors.get(projectId); |
| 681 | const serviceNames = monitor ? monitor.getAllServiceNames() : []; |
| 682 | const services = serviceNames.map((name: string) => ({ |
| 683 | name, |
| 684 | workers: [...(monitor ? monitor.getWorkerStatusesForService(name) : new Map()).entries()].map( |
| 685 | ([id, status]) => ({ |
| 686 | ...status, |
| 687 | id, |
| 688 | }), |
| 689 | ), |
| 690 | })); |
| 691 | return { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 692 | deployKeyPublic: project.deployKeyPublic == null ? undefined : project.deployKeyPublic, |
| 693 | instanceId: project.instanceId == null ? undefined : project.instanceId, |
| 694 | access: JSON.parse(project.access ?? "[]"), |
| 695 | integrations: { |
| 696 | github: !!project.githubToken, |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 697 | gemini: !!project.geminiApiKey, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 698 | }, |
| 699 | networks: getNetworks(username), |
| 700 | services, |
| 701 | user: { |
| 702 | id: userId, |
| 703 | username: username, |
| 704 | }, |
| 705 | }; |
| 706 | }; |
| 707 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 708 | const handleEnv: express.Handler = async (req, resp) => { |
| 709 | const projectId = Number(req.params["projectId"]); |
| 710 | try { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 711 | const env = await getEnv(projectId, resp.locals.userId, resp.locals.username); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 712 | resp.status(200); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 713 | resp.write(JSON.stringify(env)); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 714 | } catch (error) { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 715 | console.error("Error getting env:", error); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 716 | resp.status(500); |
| 717 | resp.write(JSON.stringify({ error: "Internal server error" })); |
| 718 | } finally { |
| 719 | resp.end(); |
| 720 | } |
| 721 | }; |
| 722 | |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 723 | const handleServiceLogs: express.Handler = async (req, resp) => { |
| 724 | try { |
| 725 | const projectId = Number(req.params["projectId"]); |
| 726 | const service = req.params["service"]; |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 727 | const workerId = req.params["workerId"]; |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 728 | const project = await db.project.findUnique({ |
| 729 | where: { |
| 730 | id: projectId, |
| 731 | userId: resp.locals.userId, |
| 732 | }, |
| 733 | }); |
| 734 | if (project == null) { |
| 735 | resp.status(404); |
| 736 | resp.write(JSON.stringify({ error: "Project not found" })); |
| 737 | return; |
| 738 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 739 | const monitor = projectMonitors.get(projectId); |
| 740 | if (!monitor || !monitor.hasLogs()) { |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 741 | resp.status(404); |
| 742 | resp.write(JSON.stringify({ error: "No logs found for this project" })); |
| 743 | return; |
| 744 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 745 | const serviceLog = monitor.getWorkerLog(service, workerId); |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 746 | if (!serviceLog) { |
| 747 | resp.status(404); |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 748 | resp.write(JSON.stringify({ error: "No logs found for this service/worker" })); |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 749 | return; |
| 750 | } |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 751 | resp.status(200); |
| 752 | resp.write(JSON.stringify({ logs: serviceLog })); |
| 753 | } catch (e) { |
| 754 | console.log(e); |
| 755 | resp.status(500); |
| 756 | resp.write(JSON.stringify({ error: "Failed to get service logs" })); |
| 757 | } finally { |
| 758 | resp.end(); |
| 759 | } |
| 760 | }; |
| 761 | |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 762 | const handleRegisterWorker: express.Handler = async (req, resp) => { |
| 763 | try { |
| 764 | const projectId = Number(req.params["projectId"]); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 765 | const result = WorkerSchema.safeParse(req.body); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 766 | if (!result.success) { |
| 767 | resp.status(400); |
| 768 | resp.write( |
| 769 | JSON.stringify({ |
| 770 | error: "Invalid request data", |
| 771 | details: result.error.format(), |
| 772 | }), |
| 773 | ); |
| 774 | return; |
| 775 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 776 | let monitor = projectMonitors.get(projectId); |
| 777 | if (!monitor) { |
| 778 | monitor = new ProjectMonitor(); |
| 779 | projectMonitors.set(projectId, monitor); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 780 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 781 | monitor.registerWorker(result.data); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 782 | resp.status(200); |
| 783 | resp.write( |
| 784 | JSON.stringify({ |
| 785 | success: true, |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 786 | }), |
| 787 | ); |
| 788 | } catch (e) { |
| 789 | console.log(e); |
| 790 | resp.status(500); |
| 791 | resp.write(JSON.stringify({ error: "Failed to register worker" })); |
| 792 | } finally { |
| 793 | resp.end(); |
| 794 | } |
| 795 | }; |
| 796 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 797 | async function reloadProject(projectId: number): Promise<boolean> { |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 798 | const monitor = projectMonitors.get(projectId); |
| 799 | const projectWorkers = monitor ? monitor.getWorkerAddresses() : []; |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 800 | const workerCount = projectWorkers.length; |
| 801 | if (workerCount === 0) { |
| 802 | return true; |
| 803 | } |
| 804 | const results = await Promise.all( |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 805 | projectWorkers.map(async (workerAddress: string) => { |
| 806 | try { |
| 807 | const { data } = await axios.get(`http://${workerAddress}/reload`); |
| 808 | return data.every((s: { status: string }) => s.status === "ok"); |
| 809 | } catch (error) { |
| 810 | console.error(`Failed to reload worker ${workerAddress}:`, error); |
| 811 | return false; |
| 812 | } |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 813 | }), |
| 814 | ); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 815 | return results.reduce((acc: boolean, curr: boolean) => acc && curr, true); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 818 | const handleReload: express.Handler = async (req, resp) => { |
| 819 | try { |
| 820 | const projectId = Number(req.params["projectId"]); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 821 | const projectAuth = await db.project.findFirst({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 822 | where: { |
| 823 | id: projectId, |
| 824 | userId: resp.locals.userId, |
| 825 | }, |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 826 | select: { id: true }, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 827 | }); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 828 | if (!projectAuth) { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 829 | resp.status(404); |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 830 | return; |
| 831 | } |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 832 | const success = await reloadProject(projectId); |
| 833 | if (success) { |
| 834 | resp.status(200); |
| 835 | } else { |
| 836 | resp.status(500); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 837 | } |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 838 | } catch (e) { |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 839 | console.error(e); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 840 | resp.status(500); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 841 | } |
| 842 | }; |
| 843 | |
| gio | 918780d | 2025-05-22 08:24:41 +0000 | [diff] [blame] | 844 | const handleReloadWorker: express.Handler = async (req, resp) => { |
| 845 | const projectId = Number(req.params["projectId"]); |
| 846 | const serviceName = req.params["serviceName"]; |
| 847 | const workerId = req.params["workerId"]; |
| 848 | |
| 849 | const projectMonitor = projectMonitors.get(projectId); |
| 850 | if (!projectMonitor) { |
| 851 | resp.status(404).send({ error: "Project monitor not found" }); |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | try { |
| 856 | await projectMonitor.reloadWorker(serviceName, workerId); |
| 857 | resp.status(200).send({ message: "Worker reload initiated" }); |
| 858 | } catch (error) { |
| 859 | console.error(`Failed to reload worker ${workerId} in service ${serviceName} for project ${projectId}:`, error); |
| 860 | const errorMessage = error instanceof Error ? error.message : "Unknown error"; |
| 861 | resp.status(500).send({ error: `Failed to reload worker: ${errorMessage}` }); |
| 862 | } |
| 863 | }; |
| 864 | |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 865 | const analyzeRepoReqSchema = z.object({ |
| 866 | address: z.string(), |
| 867 | }); |
| 868 | |
| 869 | const handleAnalyzeRepo: express.Handler = async (req, resp) => { |
| 870 | const projectId = Number(req.params["projectId"]); |
| 871 | const project = await db.project.findUnique({ |
| 872 | where: { |
| 873 | id: projectId, |
| 874 | userId: resp.locals.userId, |
| 875 | }, |
| 876 | select: { |
| 877 | githubToken: true, |
| 878 | deployKey: true, |
| 879 | deployKeyPublic: true, |
| 880 | }, |
| 881 | }); |
| 882 | if (!project) { |
| 883 | resp.status(404).send({ error: "Project not found" }); |
| 884 | return; |
| 885 | } |
| 886 | if (!project.githubToken) { |
| 887 | resp.status(400).send({ error: "GitHub token not configured" }); |
| 888 | return; |
| 889 | } |
| gio | 8e74dc0 | 2025-06-13 10:19:26 +0000 | [diff] [blame] | 890 | let tmpDir: tmp.DirResult | null = null; |
| 891 | try { |
| 892 | let deployKey: string | null = project.deployKey; |
| 893 | let deployKeyPublic: string | null = project.deployKeyPublic; |
| 894 | if (!deployKeyPublic) { |
| 895 | [deployKeyPublic, deployKey] = await generateKey(tmp.dirSync().name); |
| 896 | await db.project.update({ |
| 897 | where: { id: projectId }, |
| 898 | data: { |
| 899 | deployKeyPublic: deployKeyPublic, |
| 900 | deployKey: deployKey, |
| 901 | }, |
| 902 | }); |
| 903 | } |
| 904 | const github = new GithubClient(project.githubToken); |
| 905 | const result = analyzeRepoReqSchema.safeParse(req.body); |
| 906 | if (!result.success) { |
| 907 | resp.status(400).send({ error: "Invalid request data" }); |
| 908 | return; |
| 909 | } |
| 910 | const { address } = result.data; |
| 911 | tmpDir = tmp.dirSync({ |
| 912 | unsafeCleanup: true, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 913 | }); |
| gio | 8e74dc0 | 2025-06-13 10:19:26 +0000 | [diff] [blame] | 914 | await github.addDeployKey(address, deployKeyPublic); |
| 915 | await fs.promises.writeFile(path.join(tmpDir.name, "key"), deployKey!, { |
| 916 | mode: 0o600, |
| 917 | }); |
| 918 | shell.exec( |
| 919 | `GIT_SSH_COMMAND='ssh -i ${tmpDir.name}/key -o IdentitiesOnly=yes' git clone ${address} ${tmpDir.name}/code`, |
| 920 | ); |
| 921 | const fsc = new RealFileSystem(`${tmpDir.name}/code`); |
| 922 | const analyzer = new NodeJSAnalyzer(); |
| 923 | const info = await analyzer.analyze(fsc, "/"); |
| 924 | resp.status(200).send([info]); |
| 925 | } catch (e) { |
| 926 | console.error(e); |
| 927 | resp.status(500).send({ error: "Failed to analyze repository" }); |
| 928 | } finally { |
| 929 | if (tmpDir) { |
| 930 | tmpDir.removeCallback(); |
| 931 | } |
| 932 | resp.end(); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 933 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 934 | }; |
| 935 | |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 936 | const auth = (req: express.Request, resp: express.Response, next: express.NextFunction) => { |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 937 | // Hardcoded user for development |
| 938 | resp.locals.userId = "1"; |
| 939 | resp.locals.username = "gio"; |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 940 | next(); |
| 941 | }; |
| 942 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 943 | const handleGithubPushWebhook: express.Handler = async (req, resp) => { |
| 944 | try { |
| 945 | // TODO(gio): Implement GitHub signature verification for security |
| 946 | const webhookSchema = z.object({ |
| 947 | repository: z.object({ |
| 948 | ssh_url: z.string(), |
| 949 | }), |
| 950 | }); |
| 951 | |
| 952 | const result = webhookSchema.safeParse(req.body); |
| 953 | if (!result.success) { |
| 954 | console.warn("GitHub webhook: Invalid payload:", result.error.issues); |
| 955 | resp.status(400).json({ error: "Invalid webhook payload" }); |
| 956 | return; |
| 957 | } |
| 958 | const { ssh_url: addr } = result.data.repository; |
| 959 | const allProjects = await db.project.findMany({ |
| 960 | select: { |
| 961 | id: true, |
| 962 | state: true, |
| 963 | }, |
| 964 | where: { |
| 965 | instanceId: { |
| 966 | not: null, |
| 967 | }, |
| 968 | }, |
| 969 | }); |
| 970 | // TODO(gio): This should run in background |
| 971 | new Promise<boolean>((resolve, reject) => { |
| 972 | setTimeout(() => { |
| 973 | const projectsToReloadIds: number[] = []; |
| 974 | for (const project of allProjects) { |
| 975 | if (project.state && project.state.length > 0) { |
| 976 | const projectRepos = extractGithubRepos(project.state); |
| 977 | if (projectRepos.includes(addr)) { |
| 978 | projectsToReloadIds.push(project.id); |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | Promise.all(projectsToReloadIds.map((id) => reloadProject(id))) |
| 983 | .then((results) => { |
| 984 | resolve(results.reduce((acc, curr) => acc && curr, true)); |
| 985 | }) |
| 986 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 987 | .catch((reason: any) => reject(reason)); |
| 988 | }, 10); |
| 989 | }); |
| 990 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 991 | } catch (error: any) { |
| 992 | console.error(error); |
| 993 | resp.status(500); |
| 994 | } |
| 995 | }; |
| 996 | |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 997 | const handleValidateConfig: express.Handler = async (req, resp) => { |
| 998 | try { |
| 999 | const validationResult = ConfigSchema.safeParse(req.body); |
| 1000 | if (!validationResult.success) { |
| 1001 | resp.status(400); |
| 1002 | resp.header("Content-Type", "application/json"); |
| 1003 | resp.write(JSON.stringify({ success: false, errors: validationResult.error.flatten() })); |
| 1004 | } else { |
| 1005 | resp.status(200); |
| 1006 | resp.header("Content-Type", "application/json"); |
| 1007 | resp.write(JSON.stringify({ success: true })); |
| 1008 | } |
| 1009 | } catch (e) { |
| 1010 | console.log(e); |
| 1011 | resp.status(500); |
| 1012 | } finally { |
| 1013 | resp.end(); |
| 1014 | } |
| 1015 | }; |
| 1016 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1017 | async function start() { |
| 1018 | await db.$connect(); |
| 1019 | const app = express(); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 1020 | app.set("json spaces", 2); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1021 | app.use(express.json()); // Global JSON parsing |
| 1022 | |
| 1023 | // Public webhook route - no auth needed |
| 1024 | app.post("/api/webhook/github/push", handleGithubPushWebhook); |
| 1025 | |
| 1026 | // Authenticated project routes |
| 1027 | const projectRouter = express.Router(); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 1028 | projectRouter.use(auth); |
| 1029 | projectRouter.post("/:projectId/analyze", handleAnalyzeRepo); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1030 | projectRouter.post("/:projectId/saved", handleSave); |
| 1031 | projectRouter.get("/:projectId/saved/deploy", handleSavedGet("deploy")); |
| 1032 | projectRouter.get("/:projectId/saved/draft", handleSavedGet("draft")); |
| 1033 | projectRouter.post("/:projectId/deploy", handleDeploy); |
| 1034 | projectRouter.get("/:projectId/status", handleStatus); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 1035 | projectRouter.get("/:projectId/config", handleConfigGet); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 1036 | projectRouter.delete("/:projectId", handleProjectDelete); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1037 | projectRouter.get("/:projectId/repos/github", handleGithubRepos); |
| 1038 | projectRouter.post("/:projectId/github-token", handleUpdateGithubToken); |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 1039 | projectRouter.post("/:projectId/gemini-token", handleUpdateGeminiToken); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1040 | projectRouter.get("/:projectId/env", handleEnv); |
| gio | 918780d | 2025-05-22 08:24:41 +0000 | [diff] [blame] | 1041 | projectRouter.post("/:projectId/reload/:serviceName/:workerId", handleReloadWorker); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1042 | projectRouter.post("/:projectId/reload", handleReload); |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 1043 | projectRouter.get("/:projectId/logs/:service/:workerId", handleServiceLogs); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1044 | projectRouter.post("/:projectId/remove-deployment", handleRemoveDeployment); |
| 1045 | projectRouter.get("/", handleProjectAll); |
| 1046 | projectRouter.post("/", handleProjectCreate); |
| gio | 918780d | 2025-05-22 08:24:41 +0000 | [diff] [blame] | 1047 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1048 | app.use("/api/project", projectRouter); // Mount the authenticated router |
| 1049 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1050 | app.use("/", express.static("../front/dist")); |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 1051 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1052 | const internalApi = express(); |
| 1053 | internalApi.use(express.json()); |
| 1054 | internalApi.post("/api/project/:projectId/workers", handleRegisterWorker); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame] | 1055 | internalApi.get("/api/project/:projectId/config", handleConfigGet); |
| 1056 | internalApi.post("/api/project/:projectId/deploy", handleDeploy); |
| 1057 | internalApi.post("/api/validate-config", handleValidateConfig); |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 1058 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1059 | app.listen(env.DODO_PORT_WEB, () => { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 1060 | console.log("Web server started on port", env.DODO_PORT_WEB); |
| 1061 | }); |
| 1062 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1063 | internalApi.listen(env.DODO_PORT_API, () => { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 1064 | console.log("Internal API server started on port", env.DODO_PORT_API); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1065 | }); |
| 1066 | } |
| 1067 | |
| 1068 | start(); |