blob: 6117e46bbf869b22890571924650fa65275ec9d5 [file] [log] [blame]
gio5f2f1002025-03-20 18:38:48 +04001"use strict";
2var __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};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const client_1 = require("@prisma/client");
gio5f2f1002025-03-20 18:38:48 +040016const express_1 = __importDefault(require("express"));
gio218e8132025-04-22 17:11:58 +000017const node_process_1 = require("node:process");
18const axios_1 = __importDefault(require("axios"));
gio5f2f1002025-03-20 18:38:48 +040019const db = new client_1.PrismaClient();
20const handleProjectCreate = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
21 try {
22 const { id } = yield db.project.create({
23 data: {
gio218e8132025-04-22 17:11:58 +000024 userId: "gio", // req.get("x-forwarded-userid")!,
gio5f2f1002025-03-20 18:38:48 +040025 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});
42const handleProjectAll = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
43 try {
44 const r = yield db.project.findMany({
45 where: {
gio218e8132025-04-22 17:11:58 +000046 userId: "gio", // req.get("x-forwarded-userid")!,
gio5f2f1002025-03-20 18:38:48 +040047 },
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});
64const 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});
84const 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: {
gio218e8132025-04-22 17:11:58 +000091 state: true,
gio5f2f1002025-03-20 18:38:48 +040092 draft: true,
giob41ecae2025-04-24 08:46:50 +000093 },
gio5f2f1002025-03-20 18:38:48 +040094 });
95 if (r == null) {
96 resp.status(404);
97 }
98 else {
99 resp.status(200);
100 resp.header("content-type", "application/json");
101 if (r.draft == null) {
gio218e8132025-04-22 17:11:58 +0000102 if (r.state == null) {
103 resp.send({
104 nodes: [],
105 edges: [],
106 viewport: { x: 0, y: 0, zoom: 1 },
107 });
108 }
109 else {
giob7a3e122025-04-23 09:19:58 +0000110 resp.send(JSON.parse(Buffer.from(r.state).toString("utf8")));
gio218e8132025-04-22 17:11:58 +0000111 }
gio5f2f1002025-03-20 18:38:48 +0400112 }
113 else {
giob7a3e122025-04-23 09:19:58 +0000114 resp.send(JSON.parse(Buffer.from(r.draft).toString("utf8")));
gio5f2f1002025-03-20 18:38:48 +0400115 }
116 }
117 }
118 catch (e) {
119 console.log(e);
120 resp.status(500);
121 }
122 finally {
123 resp.end();
124 }
125});
126const handleDeploy = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
127 try {
giob7a3e122025-04-23 09:19:58 +0000128 const projectId = Number(req.params["projectId"]);
gio5f2f1002025-03-20 18:38:48 +0400129 const state = Buffer.from(JSON.stringify(req.body.state));
giob7a3e122025-04-23 09:19:58 +0000130 const p = yield db.project.findUnique({
131 where: {
132 id: projectId,
133 },
134 select: {
135 instanceId: true,
giob41ecae2025-04-24 08:46:50 +0000136 },
giob7a3e122025-04-23 09:19:58 +0000137 });
138 if (p === null) {
139 resp.status(404);
140 return;
141 }
gio5f2f1002025-03-20 18:38:48 +0400142 yield db.project.update({
143 where: {
giob7a3e122025-04-23 09:19:58 +0000144 id: projectId,
gio5f2f1002025-03-20 18:38:48 +0400145 },
146 data: {
147 draft: state,
148 },
149 });
giob7a3e122025-04-23 09:19:58 +0000150 let r;
151 if (p.instanceId == null) {
152 r = yield axios_1.default.request({
153 url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app",
154 method: "post",
gio5f2f1002025-03-20 18:38:48 +0400155 data: {
giob7a3e122025-04-23 09:19:58 +0000156 config: req.body.config,
giob41ecae2025-04-24 08:46:50 +0000157 },
gio5f2f1002025-03-20 18:38:48 +0400158 });
giob7a3e122025-04-23 09:19:58 +0000159 if (r.status === 200) {
160 yield db.project.update({
161 where: {
162 id: projectId,
163 },
164 data: {
165 state,
166 draft: null,
167 instanceId: r.data.id,
168 deployKey: r.data.deployKey,
169 },
170 });
171 }
172 }
173 else {
174 r = yield axios_1.default.request({
175 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`,
176 method: "put",
177 data: {
178 config: req.body.config,
giob41ecae2025-04-24 08:46:50 +0000179 },
giob7a3e122025-04-23 09:19:58 +0000180 });
181 if (r.status === 200) {
182 yield db.project.update({
183 where: {
184 id: projectId,
185 },
186 data: {
187 state,
188 draft: null,
189 },
190 });
191 }
gio5f2f1002025-03-20 18:38:48 +0400192 }
193 }
194 catch (e) {
195 console.log(e);
196 resp.status(500);
197 }
198 finally {
199 resp.end();
200 }
201});
202function start() {
203 return __awaiter(this, void 0, void 0, function* () {
204 yield db.$connect();
205 const app = (0, express_1.default)();
206 app.use(express_1.default.json());
207 app.post("/api/project/:projectId/saved", handleSave);
208 app.get("/api/project/:projectId/saved", handleSavedGet);
209 app.post("/api/project/:projectId/deploy", handleDeploy);
210 app.get("/api/project", handleProjectAll);
211 app.post("/api/project", handleProjectCreate);
gio218e8132025-04-22 17:11:58 +0000212 app.use("/", express_1.default.static("../front/dist"));
213 app.listen(node_process_1.env.DODO_PORT_WEB, () => {
gio5f2f1002025-03-20 18:38:48 +0400214 console.log("started");
215 });
216 });
217}
218start();