blob: 0f7a5734a390337b7c412964adb91f8889858dd0 [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) => {
giob41ecae2025-04-24 08:46:50 +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) => {
giob41ecae2025-04-24 08:46:50 +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) => {
giob41ecae2025-04-24 08:46:50 +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) => {
giob41ecae2025-04-24 08:46:50 +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);
88 } else {
89 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 });
gio218e8132025-04-22 17:11:58 +000098 } else {
giob41ecae2025-04-24 08:46:50 +000099 resp.send(JSON.parse(Buffer.from(r.state).toString("utf8")));
gio218e8132025-04-22 17:11:58 +0000100 }
giob41ecae2025-04-24 08:46:50 +0000101 } else {
102 resp.send(JSON.parse(Buffer.from(r.draft).toString("utf8")));
103 }
gio218e8132025-04-22 17:11:58 +0000104 }
giob41ecae2025-04-24 08:46:50 +0000105 } catch (e) {
106 console.log(e);
107 resp.status(500);
108 } finally {
109 resp.end();
110 }
gio218e8132025-04-22 17:11:58 +0000111};
112
113const handleDeploy: express.Handler = async (req, resp) => {
giob41ecae2025-04-24 08:46:50 +0000114 try {
115 const projectId = Number(req.params["projectId"]);
116 const state = Buffer.from(JSON.stringify(req.body.state));
117 const p = await db.project.findUnique({
118 where: {
119 id: projectId,
120 },
121 select: {
122 instanceId: true,
123 },
124 });
125 if (p === null) {
126 resp.status(404);
127 return;
gio218e8132025-04-22 17:11:58 +0000128 }
giob41ecae2025-04-24 08:46:50 +0000129 await db.project.update({
130 where: {
131 id: projectId,
132 },
133 data: {
134 draft: state,
135 },
136 });
137 let r: { status: number; data: { id: string; deployKey: string } };
138 if (p.instanceId == null) {
139 r = await axios.request({
140 url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app",
141 method: "post",
142 data: {
143 config: req.body.config,
144 },
145 });
146 if (r.status === 200) {
147 await db.project.update({
148 where: {
149 id: projectId,
150 },
151 data: {
152 state,
153 draft: null,
154 instanceId: r.data.id,
155 deployKey: r.data.deployKey,
156 },
157 });
158 }
159 } else {
160 r = await axios.request({
161 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`,
162 method: "put",
163 data: {
164 config: req.body.config,
165 },
166 });
167 if (r.status === 200) {
168 await db.project.update({
169 where: {
170 id: projectId,
171 },
172 data: {
173 state,
174 draft: null,
175 },
176 });
177 }
178 }
179 } catch (e) {
180 console.log(e);
181 resp.status(500);
182 } finally {
183 resp.end();
184 }
gio218e8132025-04-22 17:11:58 +0000185};
186
187async function start() {
giob41ecae2025-04-24 08:46:50 +0000188 await db.$connect();
189 const app = express();
190 app.use(express.json());
191 app.post("/api/project/:projectId/saved", handleSave);
192 app.get("/api/project/:projectId/saved", handleSavedGet);
193 app.post("/api/project/:projectId/deploy", handleDeploy);
194 app.get("/api/project", handleProjectAll);
195 app.post("/api/project", handleProjectCreate);
196 app.use("/", express.static("../front/dist"));
197 app.listen(env.DODO_PORT_WEB, () => {
198 console.log("started");
199 });
gio218e8132025-04-22 17:11:58 +0000200}
201
giob41ecae2025-04-24 08:46:50 +0000202start();