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