| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | "use strict"; |
| 2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | }); |
| 10 | }; |
| 11 | var __importDefault = (this && this.__importDefault) || function (mod) { |
| 12 | return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 13 | }; |
| 14 | Object.defineProperty(exports, "__esModule", { value: true }); |
| 15 | const client_1 = require("@prisma/client"); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 16 | const express_1 = __importDefault(require("express")); |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 17 | const node_process_1 = require("node:process"); |
| 18 | const axios_1 = __importDefault(require("axios")); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 19 | const db = new client_1.PrismaClient(); |
| 20 | const handleProjectCreate = (req, resp) => __awaiter(void 0, void 0, void 0, function* () { |
| 21 | try { |
| 22 | const { id } = yield db.project.create({ |
| 23 | data: { |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 24 | userId: "gio", // req.get("x-forwarded-userid")!, |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 25 | name: req.body.name, |
| 26 | }, |
| 27 | }); |
| 28 | resp.status(200); |
| 29 | resp.header("Content-Type", "application/json"); |
| 30 | resp.write(JSON.stringify({ |
| 31 | id, |
| 32 | })); |
| 33 | } |
| 34 | catch (e) { |
| 35 | console.log(e); |
| 36 | resp.status(500); |
| 37 | } |
| 38 | finally { |
| 39 | resp.end(); |
| 40 | } |
| 41 | }); |
| 42 | const handleProjectAll = (req, resp) => __awaiter(void 0, void 0, void 0, function* () { |
| 43 | try { |
| 44 | const r = yield db.project.findMany({ |
| 45 | where: { |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 46 | userId: "gio", // req.get("x-forwarded-userid")!, |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 47 | }, |
| 48 | }); |
| 49 | resp.status(200); |
| 50 | resp.header("Content-Type", "application/json"); |
| 51 | resp.write(JSON.stringify(r.map((p) => ({ |
| 52 | id: p.id.toString(), |
| 53 | name: p.name, |
| 54 | })))); |
| 55 | } |
| 56 | catch (e) { |
| 57 | console.log(e); |
| 58 | resp.status(500); |
| 59 | } |
| 60 | finally { |
| 61 | resp.end(); |
| 62 | } |
| 63 | }); |
| 64 | const handleSave = (req, resp) => __awaiter(void 0, void 0, void 0, function* () { |
| 65 | try { |
| 66 | yield db.project.update({ |
| 67 | where: { |
| 68 | id: Number(req.params["projectId"]), |
| 69 | }, |
| 70 | data: { |
| 71 | draft: Buffer.from(JSON.stringify(req.body)), |
| 72 | }, |
| 73 | }); |
| 74 | resp.status(200); |
| 75 | } |
| 76 | catch (e) { |
| 77 | console.log(e); |
| 78 | resp.status(500); |
| 79 | } |
| 80 | finally { |
| 81 | resp.end(); |
| 82 | } |
| 83 | }); |
| 84 | const handleSavedGet = (req, resp) => __awaiter(void 0, void 0, void 0, function* () { |
| 85 | try { |
| 86 | const r = yield db.project.findUnique({ |
| 87 | where: { |
| 88 | id: Number(req.params["projectId"]), |
| 89 | }, |
| 90 | select: { |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 91 | state: true, |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 92 | draft: true, |
| 93 | } |
| 94 | }); |
| 95 | if (r == null) { |
| 96 | resp.status(404); |
| 97 | } |
| 98 | else { |
| 99 | resp.status(200); |
| 100 | resp.header("content-type", "application/json"); |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 101 | console.log(r); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 102 | if (r.draft == null) { |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 103 | if (r.state == null) { |
| 104 | resp.send({ |
| 105 | nodes: [], |
| 106 | edges: [], |
| 107 | viewport: { x: 0, y: 0, zoom: 1 }, |
| 108 | }); |
| 109 | } |
| 110 | else { |
| 111 | resp.send(JSON.parse(r.state.toString())); |
| 112 | } |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 113 | } |
| 114 | else { |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 115 | resp.send(JSON.parse(Buffer.from(r.state).toString("utf8"))); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | catch (e) { |
| 120 | console.log(e); |
| 121 | resp.status(500); |
| 122 | } |
| 123 | finally { |
| 124 | resp.end(); |
| 125 | } |
| 126 | }); |
| 127 | const handleDeploy = (req, resp) => __awaiter(void 0, void 0, void 0, function* () { |
| 128 | try { |
| 129 | console.log(req.params); |
| 130 | const state = Buffer.from(JSON.stringify(req.body.state)); |
| 131 | yield db.project.update({ |
| 132 | where: { |
| 133 | id: Number(req.params["projectId"]), |
| 134 | }, |
| 135 | data: { |
| 136 | draft: state, |
| 137 | }, |
| 138 | }); |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 139 | const r = yield axios_1.default.post("http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app", { config: req.body.config }); |
| 140 | if (r.status === 200) { |
| 141 | const d = r.data; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 142 | yield db.project.update({ |
| 143 | where: { |
| 144 | id: Number(req.params["projectId"]), |
| 145 | }, |
| 146 | data: { |
| 147 | state, |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 148 | draft: null, |
| 149 | instanceId: d.id, |
| 150 | deployKey: d.deployKey, |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 151 | }, |
| 152 | }); |
| 153 | } |
| 154 | } |
| 155 | catch (e) { |
| 156 | console.log(e); |
| 157 | resp.status(500); |
| 158 | } |
| 159 | finally { |
| 160 | resp.end(); |
| 161 | } |
| 162 | }); |
| 163 | function start() { |
| 164 | return __awaiter(this, void 0, void 0, function* () { |
| 165 | yield db.$connect(); |
| 166 | const app = (0, express_1.default)(); |
| 167 | app.use(express_1.default.json()); |
| 168 | app.post("/api/project/:projectId/saved", handleSave); |
| 169 | app.get("/api/project/:projectId/saved", handleSavedGet); |
| 170 | app.post("/api/project/:projectId/deploy", handleDeploy); |
| 171 | app.get("/api/project", handleProjectAll); |
| 172 | app.post("/api/project", handleProjectCreate); |
| gio | 218e813 | 2025-04-22 17:11:58 +0000 | [diff] [blame] | 173 | app.use("/", express_1.default.static("../front/dist")); |
| 174 | app.listen(node_process_1.env.DODO_PORT_WEB, () => { |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 175 | console.log("started"); |
| 176 | }); |
| 177 | }); |
| 178 | } |
| 179 | start(); |