blob: 0cbf5e0c3c2ca8f1355ba3e435efebb66b14b83e [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");
16const node_http_1 = require("node:http");
17const express_1 = __importDefault(require("express"));
18const privateKey = `-----BEGIN OPENSSH PRIVATE KEY-----
19b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
20QyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQAAAKA42oIlONqC
21JQAAAAtzc2gtZWQyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQ
22AAAEC2CdpXtaFfqA8/mqjf9uITU1mrPOI4CeWgiQFEefFW1/K58vMu0MwIzdZT+mqpIBkh
23l48p9+/YwDCZv7MgTesFAAAAGXJvb3RAY2FudmFzYnVpbGRlci1tYXN0ZXIBAgME
24-----END OPENSSH PRIVATE KEY-----`;
25const db = new client_1.PrismaClient();
26const handleProjectCreate = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
27 try {
28 const { id } = yield db.project.create({
29 data: {
30 userId: req.get("x-forwarded-userid"),
31 name: req.body.name,
32 },
33 });
34 resp.status(200);
35 resp.header("Content-Type", "application/json");
36 resp.write(JSON.stringify({
37 id,
38 }));
39 }
40 catch (e) {
41 console.log(e);
42 resp.status(500);
43 }
44 finally {
45 resp.end();
46 }
47});
48const handleProjectAll = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
49 try {
50 const r = yield db.project.findMany({
51 where: {
52 userId: req.get("x-forwarded-userid"),
53 },
54 });
55 resp.status(200);
56 resp.header("Content-Type", "application/json");
57 resp.write(JSON.stringify(r.map((p) => ({
58 id: p.id.toString(),
59 name: p.name,
60 }))));
61 }
62 catch (e) {
63 console.log(e);
64 resp.status(500);
65 }
66 finally {
67 resp.end();
68 }
69});
70const handleSave = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
71 try {
72 yield db.project.update({
73 where: {
74 id: Number(req.params["projectId"]),
75 },
76 data: {
77 draft: Buffer.from(JSON.stringify(req.body)),
78 },
79 });
80 resp.status(200);
81 }
82 catch (e) {
83 console.log(e);
84 resp.status(500);
85 }
86 finally {
87 resp.end();
88 }
89});
90const handleSavedGet = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
91 try {
92 const r = yield db.project.findUnique({
93 where: {
94 id: Number(req.params["projectId"]),
95 },
96 select: {
97 draft: true,
98 }
99 });
100 if (r == null) {
101 resp.status(404);
102 }
103 else {
104 resp.status(200);
105 resp.header("content-type", "application/json");
106 if (r.draft == null) {
107 resp.send({
108 nodes: [],
109 edges: [],
110 viewport: { x: 0, y: 0, zoom: 1 },
111 });
112 }
113 else {
114 resp.send(JSON.parse(r.draft.toString()));
115 }
116 }
117 }
118 catch (e) {
119 console.log(e);
120 resp.status(500);
121 }
122 finally {
123 resp.end();
124 }
125});
126const handleDeploy = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
127 try {
128 console.log(req.params);
129 const state = Buffer.from(JSON.stringify(req.body.state));
130 yield db.project.update({
131 where: {
132 id: Number(req.params["projectId"]),
133 },
134 data: {
135 draft: state,
136 },
137 });
138 const result = yield new Promise((resolve) => {
139 const r = (0, node_http_1.request)("http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app", {
140 method: "POST",
141 headers: {
142 "content-type": "application/json",
143 },
144 }, (res) => {
145 res.on("end", () => {
146 resolve(res.statusCode);
147 });
148 res.on("close", () => {
149 resolve(res.statusCode);
150 });
151 res.on("error", () => {
152 resolve(res.statusCode);
153 });
154 res.on("data", (data) => console.log(data));
155 res.on("pause", () => console.log("pause"));
156 res.on("readable", () => console.log("readable"));
157 res.on("resume", () => console.log("resume"));
158 });
159 r.write(JSON.stringify({
160 id: req.params["projectId"],
161 sshPrivateKey: privateKey,
162 config: req.body.config,
163 }));
164 r.end();
165 });
166 resp.status(result);
167 if (result === 200) {
168 yield db.project.update({
169 where: {
170 id: Number(req.params["projectId"]),
171 },
172 data: {
173 state,
174 },
175 });
176 }
177 }
178 catch (e) {
179 console.log(e);
180 resp.status(500);
181 }
182 finally {
183 resp.end();
184 }
185});
186function start() {
187 return __awaiter(this, void 0, void 0, function* () {
188 yield db.$connect();
189 const app = (0, express_1.default)();
190 app.use(express_1.default.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("/assets", express_1.default.static("../dist/assets"));
197 app.use("/", express_1.default.static("../dist"));
198 app.listen(3000, () => {
199 console.log("started");
200 });
201 });
202}
203start();