blob: 258bad98fbca6d175f817309000e8be91d78c9e0 [file] [log] [blame]
gio218e8132025-04-22 17:11:58 +00001import { PrismaClient } from "@prisma/client";
giob41ecae2025-04-24 08:46:50 +00002import express from "express";
gio218e8132025-04-22 17:11:58 +00003import { env } from "node:process";
4import axios from "axios";
5
6const db = new PrismaClient();
7
8const handleProjectCreate: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +00009 try {
10 const { id } = await db.project.create({
11 data: {
12 userId: "gio", // req.get("x-forwarded-userid")!,
13 name: req.body.name,
14 },
15 });
16 resp.status(200);
17 resp.header("Content-Type", "application/json");
18 resp.write(
19 JSON.stringify({
20 id,
21 }),
22 );
23 } catch (e) {
24 console.log(e);
25 resp.status(500);
26 } finally {
27 resp.end();
28 }
gio218e8132025-04-22 17:11:58 +000029};
30
31const handleProjectAll: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +000032 try {
33 const r = await db.project.findMany({
34 where: {
35 userId: "gio", // req.get("x-forwarded-userid")!,
36 },
37 });
38 resp.status(200);
39 resp.header("Content-Type", "application/json");
40 resp.write(
41 JSON.stringify(
42 r.map((p) => ({
43 id: p.id.toString(),
44 name: p.name,
45 })),
46 ),
47 );
48 } catch (e) {
49 console.log(e);
50 resp.status(500);
51 } finally {
52 resp.end();
53 }
gio218e8132025-04-22 17:11:58 +000054};
55
56const handleSave: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +000057 try {
58 await db.project.update({
59 where: {
60 id: Number(req.params["projectId"]),
61 },
62 data: {
63 draft: Buffer.from(JSON.stringify(req.body)),
64 },
65 });
66 resp.status(200);
67 } catch (e) {
68 console.log(e);
69 resp.status(500);
70 } finally {
71 resp.end();
72 }
gio218e8132025-04-22 17:11:58 +000073};
74
75const handleSavedGet: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +000076 try {
77 const r = await db.project.findUnique({
78 where: {
79 id: Number(req.params["projectId"]),
80 },
81 select: {
82 state: true,
83 draft: true,
84 },
85 });
86 if (r == null) {
87 resp.status(404);
gio218e8132025-04-22 17:11:58 +000088 } else {
gio1dc800a2025-04-24 17:15:43 +000089 resp.status(200);
90 resp.header("content-type", "application/json");
91 if (r.draft == null) {
92 if (r.state == null) {
93 resp.send({
94 nodes: [],
95 edges: [],
96 viewport: { x: 0, y: 0, zoom: 1 },
97 });
98 } else {
99 resp.send(JSON.parse(Buffer.from(r.state).toString("utf8")));
100 }
101 } else {
102 resp.send(JSON.parse(Buffer.from(r.draft).toString("utf8")));
103 }
gio218e8132025-04-22 17:11:58 +0000104 }
gio1dc800a2025-04-24 17:15:43 +0000105 } catch (e) {
106 console.log(e);
107 resp.status(500);
108 } finally {
109 resp.end();
gio218e8132025-04-22 17:11:58 +0000110 }
111};
112
giob68003c2025-04-25 03:05:21 +0000113const handleDelete: express.Handler = async (req, resp) => {
114 try {
115 const projectId = Number(req.params["projectId"]);
116 const p = await db.project.findUnique({
117 where: {
118 id: projectId,
119 },
120 select: {
121 instanceId: true,
122 },
123 });
124 if (p === null) {
125 resp.status(404);
126 return;
127 }
128 const r = await axios.request({
129 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/instance/${p.instanceId}/remove`,
130 method: "post",
131 });
132 if (r.status === 200) {
133 await db.project.delete({
134 where: {
135 id: projectId,
136 },
137 });
138 }
139 resp.status(200);
140 } catch (e) {
141 console.log(e);
142 resp.status(500);
143 } finally {
144 resp.end();
145 }
146};
147
gio218e8132025-04-22 17:11:58 +0000148const handleDeploy: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +0000149 try {
150 const projectId = Number(req.params["projectId"]);
151 const state = Buffer.from(JSON.stringify(req.body.state));
152 const p = await db.project.findUnique({
153 where: {
154 id: projectId,
155 },
156 select: {
157 instanceId: true,
158 },
giob41ecae2025-04-24 08:46:50 +0000159 });
gio1dc800a2025-04-24 17:15:43 +0000160 if (p === null) {
161 resp.status(404);
162 return;
163 }
giob41ecae2025-04-24 08:46:50 +0000164 await db.project.update({
gio1dc800a2025-04-24 17:15:43 +0000165 where: {
166 id: projectId,
167 },
168 data: {
169 draft: state,
170 },
giob41ecae2025-04-24 08:46:50 +0000171 });
gio1dc800a2025-04-24 17:15:43 +0000172 let r: { status: number; data: { id: string; deployKey: string } };
173 if (p.instanceId == null) {
174 r = await axios.request({
175 url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app",
176 method: "post",
177 data: {
178 config: req.body.config,
179 },
180 });
181 if (r.status === 200) {
182 await db.project.update({
183 where: {
184 id: projectId,
185 },
186 data: {
187 state,
188 draft: null,
189 instanceId: r.data.id,
190 deployKey: r.data.deployKey,
191 },
192 });
193 }
194 } else {
195 r = await axios.request({
196 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`,
197 method: "put",
198 data: {
199 config: req.body.config,
200 },
201 });
202 if (r.status === 200) {
203 await db.project.update({
204 where: {
205 id: projectId,
206 },
207 data: {
208 state,
209 draft: null,
210 },
211 });
212 }
213 }
214 } catch (e) {
215 console.log(e);
216 resp.status(500);
217 } finally {
218 resp.end();
giob41ecae2025-04-24 08:46:50 +0000219 }
gio1dc800a2025-04-24 17:15:43 +0000220};
221
222const handleStatus: express.Handler = async (req, resp) => {
223 try {
224 const projectId = Number(req.params["projectId"]);
225 const p = await db.project.findUnique({
226 where: {
227 id: projectId,
228 },
229 select: {
230 instanceId: true,
231 },
232 });
233 console.log(projectId, p);
234 if (p === null) {
235 resp.status(404);
236 return;
237 }
238 if (p.instanceId == null) {
239 resp.status(404);
240 return;
241 }
242 const r = await axios.request({
giof8acc612025-04-26 08:20:55 +0400243 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/instance/${p.instanceId}/status`,
gio1dc800a2025-04-24 17:15:43 +0000244 method: "get",
245 });
246 resp.status(r.status);
247 if (r.status === 200) {
248 resp.write(JSON.stringify(r.data));
249 }
250 } catch (e) {
251 console.log(e);
252 resp.status(500);
253 } finally {
254 resp.end();
255 }
gio218e8132025-04-22 17:11:58 +0000256};
257
258async function start() {
gio1dc800a2025-04-24 17:15:43 +0000259 await db.$connect();
260 const app = express();
261 app.use(express.json());
262 app.post("/api/project/:projectId/saved", handleSave);
263 app.get("/api/project/:projectId/saved", handleSavedGet);
264 app.post("/api/project/:projectId/deploy", handleDeploy);
265 app.get("/api/project/:projectId/status", handleStatus);
giob68003c2025-04-25 03:05:21 +0000266 app.delete("/api/project/:projectId", handleDelete);
gio1dc800a2025-04-24 17:15:43 +0000267 app.get("/api/project", handleProjectAll);
268 app.post("/api/project", handleProjectCreate);
269 app.use("/", express.static("../front/dist"));
270 app.listen(env.DODO_PORT_WEB, () => {
271 console.log("started");
272 });
gio218e8132025-04-22 17:11:58 +0000273}
274
giob41ecae2025-04-24 08:46:50 +0000275start();