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