blob: 6193a905844d22582085d41dd092e2b5ed89be0b [file] [log] [blame]
gio218e8132025-04-22 17:11:58 +00001import { PrismaClient } from "@prisma/client";
2import express, { response } from "express";
3import { env } from "node:process";
4import axios from "axios";
5
6const db = new PrismaClient();
7
8const handleProjectCreate: express.Handler = async (req, resp) => {
9 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(JSON.stringify({
19 id,
20 }))
21 } catch (e) {
22 console.log(e);
23 resp.status(500);
24 } finally {
25 resp.end();
26 }
27};
28
29const handleProjectAll: express.Handler = async (req, resp) => {
30 try {
31 const r = await db.project.findMany({
32 where: {
33 userId: "gio", // req.get("x-forwarded-userid")!,
34 },
35 });
36 resp.status(200);
37 resp.header("Content-Type", "application/json");
38 resp.write(JSON.stringify(r.map((p) => ({
39 id: p.id.toString(),
40 name: p.name,
41 }))))
42 } catch (e) {
43 console.log(e);
44 resp.status(500);
45 } finally {
46 resp.end();
47 }
48};
49
50const handleSave: express.Handler = async (req, resp) => {
51 try {
52 await db.project.update({
53 where: {
54 id: Number(req.params["projectId"]),
55 },
56 data: {
57 draft: Buffer.from(JSON.stringify(req.body)),
58 },
59 });
60 resp.status(200);
61 } catch (e) {
62 console.log(e);
63 resp.status(500)
64 } finally {
65 resp.end();
66 }
67};
68
69const handleSavedGet: express.Handler = async (req, resp) => {
70 try {
71 const r = await db.project.findUnique({
72 where: {
73 id: Number(req.params["projectId"]),
74 },
75 select: {
76 state: true,
77 draft: true,
78 }
79 });
80 if (r == null) {
81 resp.status(404);
82 } else {
83 resp.status(200);
84 resp.header("content-type", "application/json");
gio218e8132025-04-22 17:11:58 +000085 if (r.draft == null) {
86 if (r.state == null) {
87 resp.send({
giob7a3e122025-04-23 09:19:58 +000088 nodes: [],
gio218e8132025-04-22 17:11:58 +000089 edges: [],
giob7a3e122025-04-23 09:19:58 +000090 viewport: { x: 0, y: 0, zoom: 1 },
91 });
gio218e8132025-04-22 17:11:58 +000092 } else {
giob7a3e122025-04-23 09:19:58 +000093 resp.send(JSON.parse(Buffer.from(r.state).toString("utf8")));
gio218e8132025-04-22 17:11:58 +000094 }
95 } else {
giob7a3e122025-04-23 09:19:58 +000096 resp.send(JSON.parse(Buffer.from(r.draft).toString("utf8")));
gio218e8132025-04-22 17:11:58 +000097 }
98 }
99 } catch (e) {
100 console.log(e);
101 resp.status(500);
102 } finally {
103 resp.end();
104 }
105};
106
107const handleDeploy: express.Handler = async (req, resp) => {
108 try {
giob7a3e122025-04-23 09:19:58 +0000109 const projectId = Number(req.params["projectId"]);
gio218e8132025-04-22 17:11:58 +0000110 const state = Buffer.from(JSON.stringify(req.body.state));
giob7a3e122025-04-23 09:19:58 +0000111 const p = await db.project.findUnique({
112 where: {
113 id: projectId,
114 },
115 select: {
116 instanceId: true,
117 }
118 });
119 if (p === null) {
120 resp.status(404);
121 return;
122 }
gio218e8132025-04-22 17:11:58 +0000123 await db.project.update({
124 where: {
giob7a3e122025-04-23 09:19:58 +0000125 id: projectId,
gio218e8132025-04-22 17:11:58 +0000126 },
127 data: {
128 draft: state,
129 },
130 });
giob7a3e122025-04-23 09:19:58 +0000131 let r: { status: number, data: { id: string, deployKey: string } };
132 if (p.instanceId == null) {
133 r = await axios.request({
134 url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app",
135 method: "post",
gio218e8132025-04-22 17:11:58 +0000136 data: {
giob7a3e122025-04-23 09:19:58 +0000137 config: req.body.config,
138 }
gio218e8132025-04-22 17:11:58 +0000139 });
giob7a3e122025-04-23 09:19:58 +0000140 if (r.status === 200) {
141 await db.project.update({
142 where: {
143 id: projectId,
144 },
145 data: {
146 state,
147 draft: null,
148 instanceId: r.data.id,
149 deployKey: r.data.deployKey,
150 },
151 });
152 }
153 } else {
154 r = await axios.request({
155 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`,
156 method: "put",
157 data: {
158 config: req.body.config,
159 }
160 });
161 if (r.status === 200) {
162 await db.project.update({
163 where: {
164 id: projectId,
165 },
166 data: {
167 state,
168 draft: null,
169 },
170 });
171 }
gio218e8132025-04-22 17:11:58 +0000172 }
173 } catch (e) {
174 console.log(e);
175 resp.status(500);
176 } finally {
177 resp.end();
178 }
179};
180
181async function start() {
182 await db.$connect();
183 const app = express();
184 app.use(express.json());
185 app.post("/api/project/:projectId/saved", handleSave);
186 app.get("/api/project/:projectId/saved", handleSavedGet);
187 app.post("/api/project/:projectId/deploy", handleDeploy);
188 app.get("/api/project", handleProjectAll);
189 app.post("/api/project", handleProjectCreate);
190 app.use("/", express.static("../front/dist"));
191 app.listen(env.DODO_PORT_WEB, () => {
192 console.log("started");
193 });
194}
195
196start();