blob: cdcf9a004ab99da5606e8dbe40bd74096cae47fb [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";
gio7f98e772025-05-07 11:00:14 +00005import { GithubClient } from "./github";
gio218e8132025-04-22 17:11:58 +00006
7const db = new PrismaClient();
8
9const handleProjectCreate: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +000010 try {
11 const { id } = await db.project.create({
12 data: {
13 userId: "gio", // req.get("x-forwarded-userid")!,
14 name: req.body.name,
15 },
16 });
17 resp.status(200);
18 resp.header("Content-Type", "application/json");
19 resp.write(
20 JSON.stringify({
21 id,
22 }),
23 );
24 } catch (e) {
25 console.log(e);
26 resp.status(500);
27 } finally {
28 resp.end();
29 }
gio218e8132025-04-22 17:11:58 +000030};
31
32const handleProjectAll: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +000033 try {
34 const r = await db.project.findMany({
35 where: {
36 userId: "gio", // req.get("x-forwarded-userid")!,
37 },
38 });
39 resp.status(200);
40 resp.header("Content-Type", "application/json");
41 resp.write(
42 JSON.stringify(
43 r.map((p) => ({
44 id: p.id.toString(),
45 name: p.name,
46 })),
47 ),
48 );
49 } catch (e) {
50 console.log(e);
51 resp.status(500);
52 } finally {
53 resp.end();
54 }
gio218e8132025-04-22 17:11:58 +000055};
56
57const handleSave: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +000058 try {
59 await db.project.update({
60 where: {
61 id: Number(req.params["projectId"]),
62 },
63 data: {
64 draft: Buffer.from(JSON.stringify(req.body)),
65 },
66 });
67 resp.status(200);
68 } catch (e) {
69 console.log(e);
70 resp.status(500);
71 } finally {
72 resp.end();
73 }
gio218e8132025-04-22 17:11:58 +000074};
75
76const handleSavedGet: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +000077 try {
78 const r = await db.project.findUnique({
79 where: {
80 id: Number(req.params["projectId"]),
81 },
82 select: {
83 state: true,
84 draft: true,
85 },
86 });
87 if (r == null) {
88 resp.status(404);
gio218e8132025-04-22 17:11:58 +000089 } else {
gio1dc800a2025-04-24 17:15:43 +000090 resp.status(200);
91 resp.header("content-type", "application/json");
92 if (r.draft == null) {
93 if (r.state == null) {
94 resp.send({
95 nodes: [],
96 edges: [],
97 viewport: { x: 0, y: 0, zoom: 1 },
98 });
99 } else {
100 resp.send(JSON.parse(Buffer.from(r.state).toString("utf8")));
101 }
102 } else {
103 resp.send(JSON.parse(Buffer.from(r.draft).toString("utf8")));
104 }
gio218e8132025-04-22 17:11:58 +0000105 }
gio1dc800a2025-04-24 17:15:43 +0000106 } catch (e) {
107 console.log(e);
108 resp.status(500);
109 } finally {
110 resp.end();
gio218e8132025-04-22 17:11:58 +0000111 }
112};
113
giob68003c2025-04-25 03:05:21 +0000114const handleDelete: express.Handler = async (req, resp) => {
115 try {
116 const projectId = Number(req.params["projectId"]);
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;
128 }
129 const r = await axios.request({
130 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/instance/${p.instanceId}/remove`,
131 method: "post",
132 });
133 if (r.status === 200) {
134 await db.project.delete({
135 where: {
136 id: projectId,
137 },
gio7f98e772025-05-07 11:00:14 +0000138 });
giob68003c2025-04-25 03:05:21 +0000139 }
140 resp.status(200);
141 } catch (e) {
142 console.log(e);
143 resp.status(500);
144 } finally {
145 resp.end();
146 }
147};
148
gio218e8132025-04-22 17:11:58 +0000149const handleDeploy: express.Handler = async (req, resp) => {
gio1dc800a2025-04-24 17:15:43 +0000150 try {
151 const projectId = Number(req.params["projectId"]);
152 const state = Buffer.from(JSON.stringify(req.body.state));
153 const p = await db.project.findUnique({
154 where: {
155 id: projectId,
156 },
157 select: {
158 instanceId: true,
gio7f98e772025-05-07 11:00:14 +0000159 githubToken: true,
160 deployKey: true,
gio1dc800a2025-04-24 17:15:43 +0000161 },
giob41ecae2025-04-24 08:46:50 +0000162 });
gio1dc800a2025-04-24 17:15:43 +0000163 if (p === null) {
164 resp.status(404);
165 return;
166 }
giob41ecae2025-04-24 08:46:50 +0000167 await db.project.update({
gio1dc800a2025-04-24 17:15:43 +0000168 where: {
169 id: projectId,
170 },
171 data: {
172 draft: state,
173 },
giob41ecae2025-04-24 08:46:50 +0000174 });
gio1dc800a2025-04-24 17:15:43 +0000175 let r: { status: number; data: { id: string; deployKey: string } };
176 if (p.instanceId == null) {
177 r = await axios.request({
178 url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app",
179 method: "post",
180 data: {
181 config: req.body.config,
182 },
183 });
gio7f98e772025-05-07 11:00:14 +0000184 console.log(r);
gio1dc800a2025-04-24 17:15:43 +0000185 if (r.status === 200) {
186 await db.project.update({
187 where: {
188 id: projectId,
189 },
190 data: {
191 state,
192 draft: null,
193 instanceId: r.data.id,
194 deployKey: r.data.deployKey,
195 },
196 });
gio7f98e772025-05-07 11:00:14 +0000197
198 if (p.githubToken && r.data.deployKey) {
199 const stateObj = JSON.parse(JSON.parse(state.toString()));
200 const githubNodes = stateObj.nodes.filter((n: any) => n.type === "github" && n.data?.repository?.id);
201
202 const github = new GithubClient(p.githubToken);
203 for (const node of githubNodes) {
204 try {
205 await github.addDeployKey(node.data.repository.sshURL, r.data.deployKey);
206 } catch (error) {
207 console.error(`Failed to add deploy key to repository ${node.data.repository.sshURL}:`, error);
208 }
209 }
210 }
gio1dc800a2025-04-24 17:15:43 +0000211 }
212 } else {
213 r = await axios.request({
214 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`,
215 method: "put",
216 data: {
217 config: req.body.config,
218 },
219 });
220 if (r.status === 200) {
221 await db.project.update({
222 where: {
223 id: projectId,
224 },
225 data: {
226 state,
227 draft: null,
228 },
229 });
230 }
231 }
232 } catch (e) {
233 console.log(e);
234 resp.status(500);
235 } finally {
236 resp.end();
giob41ecae2025-04-24 08:46:50 +0000237 }
gio1dc800a2025-04-24 17:15:43 +0000238};
239
240const handleStatus: express.Handler = async (req, resp) => {
241 try {
242 const projectId = Number(req.params["projectId"]);
243 const p = await db.project.findUnique({
244 where: {
245 id: projectId,
246 },
247 select: {
248 instanceId: true,
249 },
250 });
251 console.log(projectId, p);
252 if (p === null) {
253 resp.status(404);
254 return;
255 }
256 if (p.instanceId == null) {
257 resp.status(404);
258 return;
259 }
260 const r = await axios.request({
giof8acc612025-04-26 08:20:55 +0400261 url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/instance/${p.instanceId}/status`,
gio1dc800a2025-04-24 17:15:43 +0000262 method: "get",
263 });
264 resp.status(r.status);
265 if (r.status === 200) {
266 resp.write(JSON.stringify(r.data));
267 }
268 } catch (e) {
269 console.log(e);
270 resp.status(500);
271 } finally {
272 resp.end();
273 }
gio218e8132025-04-22 17:11:58 +0000274};
275
gio7f98e772025-05-07 11:00:14 +0000276const handleGithubRepos: express.Handler = async (req, resp) => {
277 try {
278 const projectId = Number(req.params["projectId"]);
279 const project = await db.project.findUnique({
280 where: { id: projectId },
281 select: { githubToken: true }
282 });
283
284 if (!project?.githubToken) {
285 resp.status(400);
286 resp.write(JSON.stringify({ error: "GitHub token not configured" }));
287 return;
288 }
289
290 const github = new GithubClient(project.githubToken);
291 const repositories = await github.getRepositories();
292
293 resp.status(200);
294 resp.header("Content-Type", "application/json");
295 resp.write(JSON.stringify(repositories));
296 } catch (e) {
297 console.log(e);
298 resp.status(500);
299 resp.write(JSON.stringify({ error: "Failed to fetch repositories" }));
300 } finally {
301 resp.end();
302 }
303};
304
305const handleUpdateGithubToken: express.Handler = async (req, resp) => {
306 try {
307 const projectId = Number(req.params["projectId"]);
308 const { githubToken } = req.body;
309
310 await db.project.update({
311 where: { id: projectId },
312 data: { githubToken },
313 });
314
315 resp.status(200);
316 } catch (e) {
317 console.log(e);
318 resp.status(500);
319 } finally {
320 resp.end();
321 }
322};
323
324const handleEnv: express.Handler = async (req, resp) => {
325 const projectId = Number(req.params["projectId"]);
326 try {
327 const project = await db.project.findUnique({
328 where: { id: projectId },
329 select: {
330 deployKey: true,
331 githubToken: true
332 }
333 });
334
335 if (!project) {
336 resp.status(404);
337 resp.write(JSON.stringify({ error: "Project not found" }));
338 return;
339 }
340
341 resp.status(200);
342 resp.write(JSON.stringify({
343 deployKey: project.deployKey,
344 integrations: {
345 github: !!project.githubToken,
346 },
347 networks: [{
348 name: "Public",
349 domain: "v1.dodo.cloud",
350 }, {
351 name: "Private",
352 domain: "p.v1.dodo.cloud",
353 }]
354 }));
355 } catch (error) {
356 console.error("Error checking integrations:", error);
357 resp.status(500);
358 resp.write(JSON.stringify({ error: "Internal server error" }));
359 } finally {
360 resp.end();
361 }
362};
363
gio218e8132025-04-22 17:11:58 +0000364async function start() {
gio1dc800a2025-04-24 17:15:43 +0000365 await db.$connect();
366 const app = express();
367 app.use(express.json());
368 app.post("/api/project/:projectId/saved", handleSave);
369 app.get("/api/project/:projectId/saved", handleSavedGet);
370 app.post("/api/project/:projectId/deploy", handleDeploy);
371 app.get("/api/project/:projectId/status", handleStatus);
giob68003c2025-04-25 03:05:21 +0000372 app.delete("/api/project/:projectId", handleDelete);
gio1dc800a2025-04-24 17:15:43 +0000373 app.get("/api/project", handleProjectAll);
374 app.post("/api/project", handleProjectCreate);
gio7f98e772025-05-07 11:00:14 +0000375 app.get("/api/project/:projectId/repos/github", handleGithubRepos);
376 app.post("/api/project/:projectId/github-token", handleUpdateGithubToken);
377 app.get("/api/project/:projectId/env", handleEnv);
gio1dc800a2025-04-24 17:15:43 +0000378 app.use("/", express.static("../front/dist"));
379 app.listen(env.DODO_PORT_WEB, () => {
380 console.log("started");
381 });
gio218e8132025-04-22 17:11:58 +0000382}
383
giob41ecae2025-04-24 08:46:50 +0000384start();