| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 1 | import { PrismaClient } from "@prisma/client"; |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 2 | import express from "express"; |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 3 | import { env } from "node:process"; |
| 4 | import axios from "axios"; |
| 5 | |
| 6 | const db = new PrismaClient(); |
| 7 | |
| 8 | const handleProjectCreate: express.Handler = async (req, resp) => { |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 9 | try { |
| 10 | const { id } = await db.project.create({ |
| 11 | data: { |
| 12 | userId: "gio", // req.get("x-forwarded-userid")!, |
| 13 | name: req.body.name, |
| 14 | }, |
| 15 | }); |
| 16 | resp.status(200); |
| 17 | resp.header("Content-Type", "application/json"); |
| 18 | resp.write( |
| 19 | JSON.stringify({ |
| 20 | id, |
| 21 | }), |
| 22 | ); |
| 23 | } catch (e) { |
| 24 | console.log(e); |
| 25 | resp.status(500); |
| 26 | } finally { |
| 27 | resp.end(); |
| 28 | } |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | const handleProjectAll: express.Handler = async (req, resp) => { |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 32 | try { |
| 33 | const r = await db.project.findMany({ |
| 34 | where: { |
| 35 | userId: "gio", // req.get("x-forwarded-userid")!, |
| 36 | }, |
| 37 | }); |
| 38 | resp.status(200); |
| 39 | resp.header("Content-Type", "application/json"); |
| 40 | resp.write( |
| 41 | JSON.stringify( |
| 42 | r.map((p) => ({ |
| 43 | id: p.id.toString(), |
| 44 | name: p.name, |
| 45 | })), |
| 46 | ), |
| 47 | ); |
| 48 | } catch (e) { |
| 49 | console.log(e); |
| 50 | resp.status(500); |
| 51 | } finally { |
| 52 | resp.end(); |
| 53 | } |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | const handleSave: express.Handler = async (req, resp) => { |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 57 | try { |
| 58 | await db.project.update({ |
| 59 | where: { |
| 60 | id: Number(req.params["projectId"]), |
| 61 | }, |
| 62 | data: { |
| 63 | draft: Buffer.from(JSON.stringify(req.body)), |
| 64 | }, |
| 65 | }); |
| 66 | resp.status(200); |
| 67 | } catch (e) { |
| 68 | console.log(e); |
| 69 | resp.status(500); |
| 70 | } finally { |
| 71 | resp.end(); |
| 72 | } |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | const handleSavedGet: express.Handler = async (req, resp) => { |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 76 | try { |
| 77 | const r = await db.project.findUnique({ |
| 78 | where: { |
| 79 | id: Number(req.params["projectId"]), |
| 80 | }, |
| 81 | select: { |
| 82 | state: true, |
| 83 | draft: true, |
| 84 | }, |
| 85 | }); |
| 86 | if (r == null) { |
| 87 | resp.status(404); |
| 88 | } else { |
| 89 | resp.status(200); |
| 90 | resp.header("content-type", "application/json"); |
| 91 | if (r.draft == null) { |
| 92 | if (r.state == null) { |
| 93 | resp.send({ |
| 94 | nodes: [], |
| 95 | edges: [], |
| 96 | viewport: { x: 0, y: 0, zoom: 1 }, |
| 97 | }); |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 98 | } else { |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 99 | resp.send(JSON.parse(Buffer.from(r.state).toString("utf8"))); |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 100 | } |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 101 | } else { |
| 102 | resp.send(JSON.parse(Buffer.from(r.draft).toString("utf8"))); |
| 103 | } |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 104 | } |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 105 | } catch (e) { |
| 106 | console.log(e); |
| 107 | resp.status(500); |
| 108 | } finally { |
| 109 | resp.end(); |
| 110 | } |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | const handleDeploy: express.Handler = async (req, resp) => { |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 114 | try { |
| 115 | const projectId = Number(req.params["projectId"]); |
| 116 | const state = Buffer.from(JSON.stringify(req.body.state)); |
| 117 | const p = await db.project.findUnique({ |
| 118 | where: { |
| 119 | id: projectId, |
| 120 | }, |
| 121 | select: { |
| 122 | instanceId: true, |
| 123 | }, |
| 124 | }); |
| 125 | if (p === null) { |
| 126 | resp.status(404); |
| 127 | return; |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 128 | } |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 129 | await db.project.update({ |
| 130 | where: { |
| 131 | id: projectId, |
| 132 | }, |
| 133 | data: { |
| 134 | draft: state, |
| 135 | }, |
| 136 | }); |
| 137 | let r: { status: number; data: { id: string; deployKey: string } }; |
| 138 | if (p.instanceId == null) { |
| 139 | r = await axios.request({ |
| 140 | url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app", |
| 141 | method: "post", |
| 142 | data: { |
| 143 | config: req.body.config, |
| 144 | }, |
| 145 | }); |
| 146 | if (r.status === 200) { |
| 147 | await db.project.update({ |
| 148 | where: { |
| 149 | id: projectId, |
| 150 | }, |
| 151 | data: { |
| 152 | state, |
| 153 | draft: null, |
| 154 | instanceId: r.data.id, |
| 155 | deployKey: r.data.deployKey, |
| 156 | }, |
| 157 | }); |
| 158 | } |
| 159 | } else { |
| 160 | r = await axios.request({ |
| 161 | url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`, |
| 162 | method: "put", |
| 163 | data: { |
| 164 | config: req.body.config, |
| 165 | }, |
| 166 | }); |
| 167 | if (r.status === 200) { |
| 168 | await db.project.update({ |
| 169 | where: { |
| 170 | id: projectId, |
| 171 | }, |
| 172 | data: { |
| 173 | state, |
| 174 | draft: null, |
| 175 | }, |
| 176 | }); |
| 177 | } |
| 178 | } |
| 179 | } catch (e) { |
| 180 | console.log(e); |
| 181 | resp.status(500); |
| 182 | } finally { |
| 183 | resp.end(); |
| 184 | } |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | async function start() { |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 188 | await db.$connect(); |
| 189 | const app = express(); |
| 190 | app.use(express.json()); |
| 191 | app.post("/api/project/:projectId/saved", handleSave); |
| 192 | app.get("/api/project/:projectId/saved", handleSavedGet); |
| 193 | app.post("/api/project/:projectId/deploy", handleDeploy); |
| 194 | app.get("/api/project", handleProjectAll); |
| 195 | app.post("/api/project", handleProjectCreate); |
| 196 | app.use("/", express.static("../front/dist")); |
| 197 | app.listen(env.DODO_PORT_WEB, () => { |
| 198 | console.log("started"); |
| 199 | }); |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| gio | b41ecae | 2025-04-24 08:46:50 +0000 | [diff] [blame] | 202 | start(); |