blob: c5ffd893d79d16fb60c6ffb694671669119ae950 [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,
93 }
94 });
95 if (r == null) {
96 resp.status(404);
97 }
98 else {
99 resp.status(200);
100 resp.header("content-type", "application/json");
gio218e8132025-04-22 17:11:58 +0000101 console.log(r);
gio5f2f1002025-03-20 18:38:48 +0400102 if (r.draft == null) {
gio218e8132025-04-22 17:11:58 +0000103 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 }
gio5f2f1002025-03-20 18:38:48 +0400113 }
114 else {
gio218e8132025-04-22 17:11:58 +0000115 resp.send(JSON.parse(Buffer.from(r.state).toString("utf8")));
gio5f2f1002025-03-20 18:38:48 +0400116 }
117 }
118 }
119 catch (e) {
120 console.log(e);
121 resp.status(500);
122 }
123 finally {
124 resp.end();
125 }
126});
127const 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 });
gio218e8132025-04-22 17:11:58 +0000139 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;
gio5f2f1002025-03-20 18:38:48 +0400142 yield db.project.update({
143 where: {
144 id: Number(req.params["projectId"]),
145 },
146 data: {
147 state,
gio218e8132025-04-22 17:11:58 +0000148 draft: null,
149 instanceId: d.id,
150 deployKey: d.deployKey,
gio5f2f1002025-03-20 18:38:48 +0400151 },
152 });
153 }
154 }
155 catch (e) {
156 console.log(e);
157 resp.status(500);
158 }
159 finally {
160 resp.end();
161 }
162});
163function 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);
gio218e8132025-04-22 17:11:58 +0000173 app.use("/", express_1.default.static("../front/dist"));
174 app.listen(node_process_1.env.DODO_PORT_WEB, () => {
gio5f2f1002025-03-20 18:38:48 +0400175 console.log("started");
176 });
177 });
178}
179start();