| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | import { PrismaClient } from "@prisma/client" |
| 2 | import { request } from "node:http"; |
| 3 | import express, { response } from "express"; |
| 4 | import { Database, Statement } from "sqlite3"; |
| 5 | import fs from "node:fs/promises" |
| 6 | import { Http2ServerRequest } from "node:http2"; |
| 7 | |
| 8 | const privateKey = `-----BEGIN OPENSSH PRIVATE KEY----- |
| 9 | b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW |
| 10 | QyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQAAAKA42oIlONqC |
| 11 | JQAAAAtzc2gtZWQyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQ |
| 12 | AAAEC2CdpXtaFfqA8/mqjf9uITU1mrPOI4CeWgiQFEefFW1/K58vMu0MwIzdZT+mqpIBkh |
| 13 | l48p9+/YwDCZv7MgTesFAAAAGXJvb3RAY2FudmFzYnVpbGRlci1tYXN0ZXIBAgME |
| 14 | -----END OPENSSH PRIVATE KEY-----`; |
| 15 | |
| 16 | const db = new PrismaClient(); |
| 17 | |
| 18 | const handleProjectCreate: express.Handler = async (req, resp) => { |
| 19 | try { |
| 20 | const { id } = await db.project.create({ |
| 21 | data: { |
| 22 | userId: req.get("x-forwarded-userid")!, |
| 23 | name: req.body.name, |
| 24 | }, |
| 25 | }); |
| 26 | resp.status(200); |
| 27 | resp.header("Content-Type", "application/json"); |
| 28 | resp.write(JSON.stringify({ |
| 29 | id, |
| 30 | })) |
| 31 | } catch (e) { |
| 32 | console.log(e); |
| 33 | resp.status(500); |
| 34 | } finally { |
| 35 | resp.end(); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | const handleProjectAll: express.Handler = async (req, resp) => { |
| 40 | try { |
| 41 | const r = await db.project.findMany({ |
| 42 | where: { |
| 43 | userId: req.get("x-forwarded-userid")!, |
| 44 | }, |
| 45 | }); |
| 46 | resp.status(200); |
| 47 | resp.header("Content-Type", "application/json"); |
| 48 | resp.write(JSON.stringify(r.map((p) => ({ |
| 49 | id: p.id.toString(), |
| 50 | name: p.name, |
| 51 | })))) |
| 52 | } catch (e) { |
| 53 | console.log(e); |
| 54 | resp.status(500); |
| 55 | } finally { |
| 56 | resp.end(); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | const handleSave: express.Handler = async (req, resp) => { |
| 61 | try { |
| 62 | await db.project.update({ |
| 63 | where: { |
| 64 | id: Number(req.params["projectId"]), |
| 65 | }, |
| 66 | data: { |
| 67 | draft: Buffer.from(JSON.stringify(req.body)), |
| 68 | }, |
| 69 | }); |
| 70 | resp.status(200); |
| 71 | } catch (e) { |
| 72 | console.log(e); |
| 73 | resp.status(500) |
| 74 | } finally { |
| 75 | resp.end(); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | const handleSavedGet: express.Handler = async (req, resp) => { |
| 80 | try { |
| 81 | const r = await db.project.findUnique({ |
| 82 | where: { |
| 83 | id: Number(req.params["projectId"]), |
| 84 | }, |
| 85 | select: { |
| 86 | draft: true, |
| 87 | } |
| 88 | }); |
| 89 | if (r == null) { |
| 90 | resp.status(404); |
| 91 | } else { |
| 92 | resp.status(200); |
| 93 | resp.header("content-type", "application/json"); |
| 94 | if (r.draft == null) { |
| 95 | resp.send({ |
| 96 | nodes: [], |
| 97 | edges: [], |
| 98 | viewport: { x: 0, y: 0, zoom: 1}, |
| 99 | }); |
| 100 | } else { |
| 101 | resp.send(JSON.parse(r.draft!.toString())); |
| 102 | } |
| 103 | } |
| 104 | } catch (e) { |
| 105 | console.log(e); |
| 106 | resp.status(500); |
| 107 | } finally { |
| 108 | resp.end(); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | const handleDeploy: express.Handler = async (req, resp) => { |
| 113 | try { |
| 114 | console.log(req.params); |
| 115 | const state = Buffer.from(JSON.stringify(req.body.state)); |
| 116 | await db.project.update({ |
| 117 | where: { |
| 118 | id: Number(req.params["projectId"]), |
| 119 | }, |
| 120 | data: { |
| 121 | draft: state, |
| 122 | }, |
| 123 | }); |
| 124 | const result = await new Promise<number>((resolve) => { |
| 125 | const r = request("http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app", { |
| 126 | method: "POST", |
| 127 | headers: { |
| 128 | "content-type": "application/json", |
| 129 | }, |
| 130 | }, (res) => { |
| 131 | res.on("end", () => { |
| 132 | resolve(res.statusCode!); |
| 133 | }); |
| 134 | res.on("close", () => { |
| 135 | resolve(res.statusCode!); |
| 136 | }); |
| 137 | res.on("error", () => { |
| 138 | resolve(res.statusCode!); |
| 139 | }); |
| 140 | res.on("data", (data) => console.log(data)); |
| 141 | res.on("pause", () => console.log("pause")); |
| 142 | res.on("readable", () => console.log("readable")); |
| 143 | res.on("resume", () => console.log("resume")); |
| 144 | }); |
| 145 | r.write(JSON.stringify({ |
| 146 | id: req.params["projectId"], |
| 147 | sshPrivateKey: privateKey, |
| 148 | config: req.body.config, |
| 149 | })); |
| 150 | r.end(); |
| 151 | }); |
| 152 | resp.status(result); |
| 153 | if (result === 200) { |
| 154 | await db.project.update({ |
| 155 | where: { |
| 156 | id: Number(req.params["projectId"]), |
| 157 | }, |
| 158 | data: { |
| 159 | state, |
| 160 | }, |
| 161 | }); |
| 162 | } |
| 163 | } catch (e) { |
| 164 | console.log(e); |
| 165 | resp.status(500); |
| 166 | } finally { |
| 167 | resp.end(); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | async function start() { |
| 172 | await db.$connect(); |
| 173 | const app = express(); |
| 174 | app.use(express.json()); |
| 175 | app.post("/api/project/:projectId/saved", handleSave); |
| 176 | app.get("/api/project/:projectId/saved", handleSavedGet); |
| 177 | app.post("/api/project/:projectId/deploy", handleDeploy); |
| 178 | app.get("/api/project", handleProjectAll); |
| 179 | app.post("/api/project", handleProjectCreate); |
| 180 | app.use("/assets", express.static("../dist/assets")); |
| 181 | app.use("/", express.static("../dist")); |
| 182 | app.listen(3000, () => { |
| 183 | console.log("started"); |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | start(); |