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