| 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"); |
| 85 | console.log(r); |
| 86 | if (r.draft == null) { |
| 87 | if (r.state == null) { |
| 88 | resp.send({ |
| 89 | nodes: [], |
| 90 | edges: [], |
| 91 | viewport: { x: 0, y: 0, zoom: 1}, |
| 92 | }); |
| 93 | } else { |
| 94 | resp.send(JSON.parse(r.state.toString())); |
| 95 | } |
| 96 | } else { |
| 97 | resp.send(JSON.parse(Buffer.from(r.state!).toString("utf8"))); |
| 98 | } |
| 99 | } |
| 100 | } catch (e) { |
| 101 | console.log(e); |
| 102 | resp.status(500); |
| 103 | } finally { |
| 104 | resp.end(); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | const handleDeploy: express.Handler = async (req, resp) => { |
| 109 | try { |
| 110 | console.log(req.params); |
| 111 | const state = Buffer.from(JSON.stringify(req.body.state)); |
| 112 | await db.project.update({ |
| 113 | where: { |
| 114 | id: Number(req.params["projectId"]), |
| 115 | }, |
| 116 | data: { |
| 117 | draft: state, |
| 118 | }, |
| 119 | }); |
| 120 | const r = await axios.post("http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app", { config: req.body.config }); |
| 121 | if (r.status === 200) { |
| 122 | const d = r.data as { id: string, deployKey: string }; |
| 123 | await db.project.update({ |
| 124 | where: { |
| 125 | id: Number(req.params["projectId"]), |
| 126 | }, |
| 127 | data: { |
| 128 | state, |
| 129 | draft: null, |
| 130 | instanceId: d.id, |
| 131 | deployKey: d.deployKey, |
| 132 | }, |
| 133 | }); |
| 134 | } |
| 135 | } catch (e) { |
| 136 | console.log(e); |
| 137 | resp.status(500); |
| 138 | } finally { |
| 139 | resp.end(); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | async function start() { |
| 144 | await db.$connect(); |
| 145 | const app = express(); |
| 146 | app.use(express.json()); |
| 147 | app.post("/api/project/:projectId/saved", handleSave); |
| 148 | app.get("/api/project/:projectId/saved", handleSavedGet); |
| 149 | app.post("/api/project/:projectId/deploy", handleDeploy); |
| 150 | app.get("/api/project", handleProjectAll); |
| 151 | app.post("/api/project", handleProjectCreate); |
| 152 | app.use("/", express.static("../front/dist")); |
| 153 | app.listen(env.DODO_PORT_WEB, () => { |
| 154 | console.log("started"); |
| 155 | }); |
| 156 | } |
| 157 | |
| 158 | start(); |