| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1 | import { PrismaClient } from "@prisma/client"; |
| 2 | import express from "express"; |
| 3 | import { env } from "node:process"; |
| 4 | import axios from "axios"; |
| 5 | import { GithubClient } from "./github"; |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 6 | import { z } from "zod"; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 7 | |
| 8 | const db = new PrismaClient(); |
| 9 | |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 10 | // Map to store worker addresses by project ID |
| 11 | const workers = new Map<number, string[]>(); |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 12 | const logs = new Map<number, Map<string, string>>(); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 13 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 14 | const handleProjectCreate: express.Handler = async (req, resp) => { |
| 15 | try { |
| 16 | const { id } = await db.project.create({ |
| 17 | data: { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 18 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 19 | name: req.body.name, |
| 20 | }, |
| 21 | }); |
| 22 | resp.status(200); |
| 23 | resp.header("Content-Type", "application/json"); |
| 24 | resp.write( |
| 25 | JSON.stringify({ |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame^] | 26 | id: id.toString(), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 27 | }), |
| 28 | ); |
| 29 | } catch (e) { |
| 30 | console.log(e); |
| 31 | resp.status(500); |
| 32 | } finally { |
| 33 | resp.end(); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | const handleProjectAll: express.Handler = async (req, resp) => { |
| 38 | try { |
| 39 | const r = await db.project.findMany({ |
| 40 | where: { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 41 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 42 | }, |
| 43 | }); |
| 44 | resp.status(200); |
| 45 | resp.header("Content-Type", "application/json"); |
| 46 | resp.write( |
| 47 | JSON.stringify( |
| 48 | r.map((p) => ({ |
| 49 | id: p.id.toString(), |
| 50 | name: p.name, |
| 51 | })), |
| 52 | ), |
| 53 | ); |
| 54 | } catch (e) { |
| 55 | console.log(e); |
| 56 | resp.status(500); |
| 57 | } finally { |
| 58 | resp.end(); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | const handleSave: express.Handler = async (req, resp) => { |
| 63 | try { |
| 64 | await db.project.update({ |
| 65 | where: { |
| 66 | id: Number(req.params["projectId"]), |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 67 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 68 | }, |
| 69 | data: { |
| 70 | draft: Buffer.from(JSON.stringify(req.body)), |
| 71 | }, |
| 72 | }); |
| 73 | resp.status(200); |
| 74 | } catch (e) { |
| 75 | console.log(e); |
| 76 | resp.status(500); |
| 77 | } finally { |
| 78 | resp.end(); |
| 79 | } |
| 80 | }; |
| 81 | |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 82 | function handleSavedGet(state: "deploy" | "draft"): express.Handler { |
| 83 | return async (req, resp) => { |
| 84 | try { |
| 85 | const r = await db.project.findUnique({ |
| 86 | where: { |
| 87 | id: Number(req.params["projectId"]), |
| 88 | userId: resp.locals.userId, |
| 89 | }, |
| 90 | select: { |
| 91 | state: true, |
| 92 | draft: true, |
| 93 | }, |
| 94 | }); |
| 95 | if (r == null) { |
| 96 | resp.status(404); |
| 97 | return; |
| 98 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 99 | resp.status(200); |
| 100 | resp.header("content-type", "application/json"); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 101 | if (state === "deploy") { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 102 | if (r.state == null) { |
| 103 | resp.send({ |
| 104 | nodes: [], |
| 105 | edges: [], |
| 106 | viewport: { x: 0, y: 0, zoom: 1 }, |
| 107 | }); |
| 108 | } else { |
| 109 | resp.send(JSON.parse(Buffer.from(r.state).toString("utf8"))); |
| 110 | } |
| 111 | } else { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 112 | if (r.draft == null) { |
| 113 | if (r.state == null) { |
| 114 | resp.send({ |
| 115 | nodes: [], |
| 116 | edges: [], |
| 117 | viewport: { x: 0, y: 0, zoom: 1 }, |
| 118 | }); |
| 119 | } else { |
| 120 | resp.send(JSON.parse(Buffer.from(r.state).toString("utf8"))); |
| 121 | } |
| 122 | } else { |
| 123 | resp.send(JSON.parse(Buffer.from(r.draft).toString("utf8"))); |
| 124 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 125 | } |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 126 | } catch (e) { |
| 127 | console.log(e); |
| 128 | resp.status(500); |
| 129 | } finally { |
| 130 | resp.end(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 131 | } |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 132 | }; |
| 133 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 134 | |
| 135 | const handleDelete: express.Handler = async (req, resp) => { |
| 136 | try { |
| 137 | const projectId = Number(req.params["projectId"]); |
| 138 | const p = await db.project.findUnique({ |
| 139 | where: { |
| 140 | id: projectId, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 141 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 142 | }, |
| 143 | select: { |
| 144 | instanceId: true, |
| 145 | }, |
| 146 | }); |
| 147 | if (p === null) { |
| 148 | resp.status(404); |
| 149 | return; |
| 150 | } |
| gio | e440db8 | 2025-05-13 12:21:44 +0000 | [diff] [blame] | 151 | let ok = false; |
| 152 | if (p.instanceId === null) { |
| 153 | ok = true; |
| 154 | } else { |
| 155 | const r = await axios.request({ |
| 156 | url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/instance/${p.instanceId}/remove`, |
| 157 | method: "post", |
| 158 | }); |
| 159 | ok = r.status === 200; |
| 160 | } |
| 161 | if (ok) { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 162 | await db.project.delete({ |
| 163 | where: { |
| 164 | id: projectId, |
| 165 | }, |
| 166 | }); |
| 167 | } |
| 168 | resp.status(200); |
| 169 | } catch (e) { |
| 170 | console.log(e); |
| 171 | resp.status(500); |
| 172 | } finally { |
| 173 | resp.end(); |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | const handleDeploy: express.Handler = async (req, resp) => { |
| 178 | try { |
| 179 | const projectId = Number(req.params["projectId"]); |
| 180 | const state = Buffer.from(JSON.stringify(req.body.state)); |
| 181 | const p = await db.project.findUnique({ |
| 182 | where: { |
| 183 | id: projectId, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 184 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 185 | }, |
| 186 | select: { |
| 187 | instanceId: true, |
| 188 | githubToken: true, |
| 189 | deployKey: true, |
| 190 | }, |
| 191 | }); |
| 192 | if (p === null) { |
| 193 | resp.status(404); |
| 194 | return; |
| 195 | } |
| 196 | await db.project.update({ |
| 197 | where: { |
| 198 | id: projectId, |
| 199 | }, |
| 200 | data: { |
| 201 | draft: state, |
| 202 | }, |
| 203 | }); |
| 204 | let r: { status: number; data: { id: string; deployKey: string } }; |
| 205 | if (p.instanceId == null) { |
| 206 | r = await axios.request({ |
| 207 | url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app", |
| 208 | method: "post", |
| 209 | data: { |
| 210 | config: req.body.config, |
| 211 | }, |
| 212 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 213 | if (r.status === 200) { |
| 214 | await db.project.update({ |
| 215 | where: { |
| 216 | id: projectId, |
| 217 | }, |
| 218 | data: { |
| 219 | state, |
| 220 | draft: null, |
| 221 | instanceId: r.data.id, |
| 222 | deployKey: r.data.deployKey, |
| 223 | }, |
| 224 | }); |
| 225 | |
| 226 | if (p.githubToken && r.data.deployKey) { |
| 227 | const stateObj = JSON.parse(JSON.parse(state.toString())); |
| gio | 0b4002c | 2025-05-11 15:48:51 +0000 | [diff] [blame] | 228 | const githubNodes = stateObj.nodes.filter( |
| 229 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 230 | (n: any) => n.type === "github" && n.data?.repository?.id, |
| 231 | ); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 232 | |
| 233 | const github = new GithubClient(p.githubToken); |
| 234 | for (const node of githubNodes) { |
| 235 | try { |
| 236 | await github.addDeployKey(node.data.repository.sshURL, r.data.deployKey); |
| 237 | } catch (error) { |
| 238 | console.error( |
| 239 | `Failed to add deploy key to repository ${node.data.repository.sshURL}:`, |
| 240 | error, |
| 241 | ); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } else { |
| 247 | r = await axios.request({ |
| 248 | url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`, |
| 249 | method: "put", |
| 250 | data: { |
| 251 | config: req.body.config, |
| 252 | }, |
| 253 | }); |
| 254 | if (r.status === 200) { |
| 255 | await db.project.update({ |
| 256 | where: { |
| 257 | id: projectId, |
| 258 | }, |
| 259 | data: { |
| 260 | state, |
| 261 | draft: null, |
| 262 | }, |
| 263 | }); |
| 264 | } |
| 265 | } |
| 266 | } catch (e) { |
| 267 | console.log(e); |
| 268 | resp.status(500); |
| 269 | } finally { |
| 270 | resp.end(); |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | const handleStatus: express.Handler = async (req, resp) => { |
| 275 | try { |
| 276 | const projectId = Number(req.params["projectId"]); |
| 277 | const p = await db.project.findUnique({ |
| 278 | where: { |
| 279 | id: projectId, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 280 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 281 | }, |
| 282 | select: { |
| 283 | instanceId: true, |
| 284 | }, |
| 285 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 286 | if (p === null) { |
| 287 | resp.status(404); |
| 288 | return; |
| 289 | } |
| 290 | if (p.instanceId == null) { |
| 291 | resp.status(404); |
| 292 | return; |
| 293 | } |
| 294 | const r = await axios.request({ |
| 295 | url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/instance/${p.instanceId}/status`, |
| 296 | method: "get", |
| 297 | }); |
| 298 | resp.status(r.status); |
| 299 | if (r.status === 200) { |
| 300 | resp.write(JSON.stringify(r.data)); |
| 301 | } |
| 302 | } catch (e) { |
| 303 | console.log(e); |
| 304 | resp.status(500); |
| 305 | } finally { |
| 306 | resp.end(); |
| 307 | } |
| 308 | }; |
| 309 | |
| 310 | const handleGithubRepos: express.Handler = async (req, resp) => { |
| 311 | try { |
| 312 | const projectId = Number(req.params["projectId"]); |
| 313 | const project = await db.project.findUnique({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 314 | where: { |
| 315 | id: projectId, |
| 316 | userId: resp.locals.userId, |
| 317 | }, |
| 318 | select: { |
| 319 | githubToken: true, |
| 320 | }, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 321 | }); |
| 322 | |
| 323 | if (!project?.githubToken) { |
| 324 | resp.status(400); |
| 325 | resp.write(JSON.stringify({ error: "GitHub token not configured" })); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | const github = new GithubClient(project.githubToken); |
| 330 | const repositories = await github.getRepositories(); |
| 331 | |
| 332 | resp.status(200); |
| 333 | resp.header("Content-Type", "application/json"); |
| 334 | resp.write(JSON.stringify(repositories)); |
| 335 | } catch (e) { |
| 336 | console.log(e); |
| 337 | resp.status(500); |
| 338 | resp.write(JSON.stringify({ error: "Failed to fetch repositories" })); |
| 339 | } finally { |
| 340 | resp.end(); |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | const handleUpdateGithubToken: express.Handler = async (req, resp) => { |
| 345 | try { |
| 346 | const projectId = Number(req.params["projectId"]); |
| 347 | const { githubToken } = req.body; |
| 348 | |
| 349 | await db.project.update({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 350 | where: { |
| 351 | id: projectId, |
| 352 | userId: resp.locals.userId, |
| 353 | }, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 354 | data: { githubToken }, |
| 355 | }); |
| 356 | |
| 357 | resp.status(200); |
| 358 | } catch (e) { |
| 359 | console.log(e); |
| 360 | resp.status(500); |
| 361 | } finally { |
| 362 | resp.end(); |
| 363 | } |
| 364 | }; |
| 365 | |
| 366 | const handleEnv: express.Handler = async (req, resp) => { |
| 367 | const projectId = Number(req.params["projectId"]); |
| 368 | try { |
| 369 | const project = await db.project.findUnique({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 370 | where: { |
| 371 | id: projectId, |
| 372 | userId: resp.locals.userId, |
| 373 | }, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 374 | select: { |
| 375 | deployKey: true, |
| 376 | githubToken: true, |
| 377 | }, |
| 378 | }); |
| 379 | |
| 380 | if (!project) { |
| 381 | resp.status(404); |
| 382 | resp.write(JSON.stringify({ error: "Project not found" })); |
| 383 | return; |
| 384 | } |
| 385 | |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 386 | const projectLogs = logs.get(projectId) || new Map(); |
| 387 | const services = Array.from(projectLogs.keys()); |
| 388 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 389 | resp.status(200); |
| 390 | resp.write( |
| 391 | JSON.stringify({ |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 392 | // TODO(gio): get from env or command line flags |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 393 | managerAddr: "http://10.42.0.211:8081", |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 394 | deployKey: project.deployKey, |
| 395 | integrations: { |
| 396 | github: !!project.githubToken, |
| 397 | }, |
| 398 | networks: [ |
| 399 | { |
| 400 | name: "Public", |
| 401 | domain: "v1.dodo.cloud", |
| 402 | }, |
| 403 | { |
| 404 | name: "Private", |
| 405 | domain: "p.v1.dodo.cloud", |
| 406 | }, |
| 407 | ], |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 408 | services, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 409 | }), |
| 410 | ); |
| 411 | } catch (error) { |
| 412 | console.error("Error checking integrations:", error); |
| 413 | resp.status(500); |
| 414 | resp.write(JSON.stringify({ error: "Internal server error" })); |
| 415 | } finally { |
| 416 | resp.end(); |
| 417 | } |
| 418 | }; |
| 419 | |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 420 | const handleServiceLogs: express.Handler = async (req, resp) => { |
| 421 | try { |
| 422 | const projectId = Number(req.params["projectId"]); |
| 423 | const service = req.params["service"]; |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 424 | const project = await db.project.findUnique({ |
| 425 | where: { |
| 426 | id: projectId, |
| 427 | userId: resp.locals.userId, |
| 428 | }, |
| 429 | }); |
| 430 | if (project == null) { |
| 431 | resp.status(404); |
| 432 | resp.write(JSON.stringify({ error: "Project not found" })); |
| 433 | return; |
| 434 | } |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 435 | |
| 436 | const projectLogs = logs.get(projectId); |
| 437 | if (!projectLogs) { |
| 438 | resp.status(404); |
| 439 | resp.write(JSON.stringify({ error: "No logs found for this project" })); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | const serviceLog = projectLogs.get(service); |
| 444 | if (!serviceLog) { |
| 445 | resp.status(404); |
| 446 | resp.write(JSON.stringify({ error: "No logs found for this service" })); |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | resp.status(200); |
| 451 | resp.write(JSON.stringify({ logs: serviceLog })); |
| 452 | } catch (e) { |
| 453 | console.log(e); |
| 454 | resp.status(500); |
| 455 | resp.write(JSON.stringify({ error: "Failed to get service logs" })); |
| 456 | } finally { |
| 457 | resp.end(); |
| 458 | } |
| 459 | }; |
| 460 | |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 461 | const WorkerSchema = z.object({ |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 462 | service: z.string(), |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 463 | address: z.string().url(), |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 464 | logs: z.optional(z.string()), |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 465 | }); |
| 466 | |
| 467 | const handleRegisterWorker: express.Handler = async (req, resp) => { |
| 468 | try { |
| 469 | const projectId = Number(req.params["projectId"]); |
| 470 | |
| 471 | const result = WorkerSchema.safeParse(req.body); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 472 | if (!result.success) { |
| 473 | resp.status(400); |
| 474 | resp.write( |
| 475 | JSON.stringify({ |
| 476 | error: "Invalid request data", |
| 477 | details: result.error.format(), |
| 478 | }), |
| 479 | ); |
| 480 | return; |
| 481 | } |
| 482 | |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 483 | const { service, address, logs: log } = result.data; |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 484 | |
| 485 | // Get existing workers or initialize empty array |
| 486 | const projectWorkers = workers.get(projectId) || []; |
| 487 | |
| 488 | // Add new worker if not already present |
| 489 | if (!projectWorkers.includes(address)) { |
| 490 | projectWorkers.push(address); |
| 491 | } |
| 492 | |
| 493 | workers.set(projectId, projectWorkers); |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 494 | if (log) { |
| 495 | const svcLogs: Map<string, string> = logs.get(projectId) || new Map(); |
| 496 | svcLogs.set(service, log); |
| 497 | logs.set(projectId, svcLogs); |
| 498 | } |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 499 | resp.status(200); |
| 500 | resp.write( |
| 501 | JSON.stringify({ |
| 502 | success: true, |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 503 | }), |
| 504 | ); |
| 505 | } catch (e) { |
| 506 | console.log(e); |
| 507 | resp.status(500); |
| 508 | resp.write(JSON.stringify({ error: "Failed to register worker" })); |
| 509 | } finally { |
| 510 | resp.end(); |
| 511 | } |
| 512 | }; |
| 513 | |
| 514 | const handleReload: express.Handler = async (req, resp) => { |
| 515 | try { |
| 516 | const projectId = Number(req.params["projectId"]); |
| 517 | const projectWorkers = workers.get(projectId) || []; |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 518 | const project = await db.project.findUnique({ |
| 519 | where: { |
| 520 | id: projectId, |
| 521 | userId: resp.locals.userId, |
| 522 | }, |
| 523 | }); |
| 524 | if (project == null) { |
| 525 | resp.status(404); |
| 526 | resp.write(JSON.stringify({ error: "Project not found" })); |
| 527 | return; |
| 528 | } |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 529 | |
| 530 | if (projectWorkers.length === 0) { |
| 531 | resp.status(404); |
| 532 | resp.write(JSON.stringify({ error: "No workers registered for this project" })); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | await Promise.all( |
| 537 | projectWorkers.map(async (workerAddress) => { |
| 538 | try { |
| 539 | const updateEndpoint = `${workerAddress}/update`; |
| 540 | await axios.post(updateEndpoint); |
| gio | 0b4002c | 2025-05-11 15:48:51 +0000 | [diff] [blame] | 541 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 542 | } catch (error: any) { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 543 | console.log(`Failed to update worker ${workerAddress}: ${error.message || "Unknown error"}`); |
| 544 | } |
| 545 | }), |
| 546 | ); |
| 547 | |
| 548 | resp.status(200); |
| 549 | resp.write(JSON.stringify({ success: true })); |
| 550 | } catch (e) { |
| 551 | console.log(e); |
| 552 | resp.status(500); |
| 553 | resp.write(JSON.stringify({ error: "Failed to reload workers" })); |
| 554 | } finally { |
| 555 | resp.end(); |
| 556 | } |
| 557 | }; |
| 558 | |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 559 | const auth = (req: express.Request, resp: express.Response, next: express.NextFunction) => { |
| 560 | const userId = req.get("x-forwarded-userid"); |
| 561 | if (userId === undefined) { |
| 562 | resp.status(401); |
| 563 | resp.write("Unauthorized"); |
| 564 | resp.end(); |
| 565 | return; |
| 566 | } |
| 567 | resp.locals.userId = userId; |
| 568 | next(); |
| 569 | }; |
| 570 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 571 | async function start() { |
| 572 | await db.$connect(); |
| 573 | const app = express(); |
| 574 | app.use(express.json()); |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 575 | app.use(auth); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 576 | app.post("/api/project/:projectId/saved", handleSave); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 577 | app.get("/api/project/:projectId/saved/deploy", handleSavedGet("deploy")); |
| 578 | app.get("/api/project/:projectId/saved/draft", handleSavedGet("draft")); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 579 | app.post("/api/project/:projectId/deploy", handleDeploy); |
| 580 | app.get("/api/project/:projectId/status", handleStatus); |
| 581 | app.delete("/api/project/:projectId", handleDelete); |
| 582 | app.get("/api/project", handleProjectAll); |
| 583 | app.post("/api/project", handleProjectCreate); |
| 584 | app.get("/api/project/:projectId/repos/github", handleGithubRepos); |
| 585 | app.post("/api/project/:projectId/github-token", handleUpdateGithubToken); |
| 586 | app.get("/api/project/:projectId/env", handleEnv); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 587 | app.post("/api/project/:projectId/reload", handleReload); |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 588 | app.get("/api/project/:projectId/logs/:service", handleServiceLogs); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 589 | app.use("/", express.static("../front/dist")); |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 590 | |
| 591 | const api = express(); |
| 592 | api.use(express.json()); |
| 593 | api.post("/api/project/:projectId/workers", handleRegisterWorker); |
| 594 | |
| 595 | // Start both servers |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 596 | app.listen(env.DODO_PORT_WEB, () => { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 597 | console.log("Web server started on port", env.DODO_PORT_WEB); |
| 598 | }); |
| 599 | |
| 600 | api.listen(env.DODO_PORT_API, () => { |
| 601 | console.log("Internal API server started on port", env.DODO_PORT_API); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 602 | }); |
| 603 | } |
| 604 | |
| 605 | start(); |