blob: 32c28cda03b5a7963153348b2925d0973b60ebf1 [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});
giob68003c2025-04-25 03:05:21 +0000126const handleDelete = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
127 try {
128 const projectId = Number(req.params["projectId"]);
129 const p = yield db.project.findUnique({
130 where: {
131 id: projectId,
132 },
133 select: {
134 instanceId: true,
135 },
136 });
137 if (p === null) {
138 resp.status(404);
139 return;
140 }
141 const r = yield axios_1.default.request({
142 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/instance/${p.instanceId}/remove`,
143 method: "post",
144 });
145 if (r.status === 200) {
146 yield db.project.delete({
147 where: {
148 id: projectId,
149 },
150 });
151 }
152 resp.status(200);
153 }
154 catch (e) {
155 console.log(e);
156 resp.status(500);
157 }
158 finally {
159 resp.end();
160 }
161});
gio5f2f1002025-03-20 18:38:48 +0400162const handleDeploy = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
163 try {
giob7a3e122025-04-23 09:19:58 +0000164 const projectId = Number(req.params["projectId"]);
gio5f2f1002025-03-20 18:38:48 +0400165 const state = Buffer.from(JSON.stringify(req.body.state));
giob7a3e122025-04-23 09:19:58 +0000166 const p = yield db.project.findUnique({
167 where: {
168 id: projectId,
169 },
170 select: {
171 instanceId: true,
giob41ecae2025-04-24 08:46:50 +0000172 },
giob7a3e122025-04-23 09:19:58 +0000173 });
174 if (p === null) {
175 resp.status(404);
176 return;
177 }
gio5f2f1002025-03-20 18:38:48 +0400178 yield db.project.update({
179 where: {
giob7a3e122025-04-23 09:19:58 +0000180 id: projectId,
gio5f2f1002025-03-20 18:38:48 +0400181 },
182 data: {
183 draft: state,
184 },
185 });
giob7a3e122025-04-23 09:19:58 +0000186 let r;
187 if (p.instanceId == null) {
188 r = yield axios_1.default.request({
189 url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app",
190 method: "post",
gio5f2f1002025-03-20 18:38:48 +0400191 data: {
giob7a3e122025-04-23 09:19:58 +0000192 config: req.body.config,
giob41ecae2025-04-24 08:46:50 +0000193 },
gio5f2f1002025-03-20 18:38:48 +0400194 });
giob7a3e122025-04-23 09:19:58 +0000195 if (r.status === 200) {
196 yield db.project.update({
197 where: {
198 id: projectId,
199 },
200 data: {
201 state,
202 draft: null,
203 instanceId: r.data.id,
204 deployKey: r.data.deployKey,
205 },
206 });
207 }
208 }
209 else {
210 r = yield axios_1.default.request({
211 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`,
212 method: "put",
213 data: {
214 config: req.body.config,
giob41ecae2025-04-24 08:46:50 +0000215 },
giob7a3e122025-04-23 09:19:58 +0000216 });
217 if (r.status === 200) {
218 yield db.project.update({
219 where: {
220 id: projectId,
221 },
222 data: {
223 state,
224 draft: null,
225 },
226 });
227 }
gio5f2f1002025-03-20 18:38:48 +0400228 }
229 }
230 catch (e) {
231 console.log(e);
232 resp.status(500);
233 }
234 finally {
235 resp.end();
236 }
237});
gio1dc800a2025-04-24 17:15:43 +0000238const handleStatus = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
239 try {
240 const projectId = Number(req.params["projectId"]);
241 const p = yield db.project.findUnique({
242 where: {
243 id: projectId,
244 },
245 select: {
246 instanceId: true,
247 },
248 });
249 console.log(projectId, p);
250 if (p === null) {
251 resp.status(404);
252 return;
253 }
254 if (p.instanceId == null) {
255 resp.status(404);
256 return;
257 }
258 const r = yield axios_1.default.request({
259 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/tasks/${p.instanceId}`,
260 method: "get",
261 });
262 resp.status(r.status);
263 if (r.status === 200) {
264 resp.write(JSON.stringify(r.data));
265 }
266 }
267 catch (e) {
268 console.log(e);
269 resp.status(500);
270 }
271 finally {
272 resp.end();
273 }
274});
gio5f2f1002025-03-20 18:38:48 +0400275function start() {
276 return __awaiter(this, void 0, void 0, function* () {
277 yield db.$connect();
278 const app = (0, express_1.default)();
279 app.use(express_1.default.json());
280 app.post("/api/project/:projectId/saved", handleSave);
281 app.get("/api/project/:projectId/saved", handleSavedGet);
282 app.post("/api/project/:projectId/deploy", handleDeploy);
gio1dc800a2025-04-24 17:15:43 +0000283 app.get("/api/project/:projectId/status", handleStatus);
giob68003c2025-04-25 03:05:21 +0000284 app.delete("/api/project/:projectId", handleDelete);
gio5f2f1002025-03-20 18:38:48 +0400285 app.get("/api/project", handleProjectAll);
286 app.post("/api/project", handleProjectCreate);
gio218e8132025-04-22 17:11:58 +0000287 app.use("/", express_1.default.static("../front/dist"));
288 app.listen(node_process_1.env.DODO_PORT_WEB, () => {
gio5f2f1002025-03-20 18:38:48 +0400289 console.log("started");
290 });
291 });
292}
293start();