blob: 32c28cda03b5a7963153348b2925d0973b60ebf1 [file] [log] [blame]
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const client_1 = require("@prisma/client");
const express_1 = __importDefault(require("express"));
const node_process_1 = require("node:process");
const axios_1 = __importDefault(require("axios"));
const db = new client_1.PrismaClient();
const handleProjectCreate = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { id } = yield db.project.create({
data: {
userId: "gio", // req.get("x-forwarded-userid")!,
name: req.body.name,
},
});
resp.status(200);
resp.header("Content-Type", "application/json");
resp.write(JSON.stringify({
id,
}));
}
catch (e) {
console.log(e);
resp.status(500);
}
finally {
resp.end();
}
});
const handleProjectAll = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
try {
const r = yield db.project.findMany({
where: {
userId: "gio", // req.get("x-forwarded-userid")!,
},
});
resp.status(200);
resp.header("Content-Type", "application/json");
resp.write(JSON.stringify(r.map((p) => ({
id: p.id.toString(),
name: p.name,
}))));
}
catch (e) {
console.log(e);
resp.status(500);
}
finally {
resp.end();
}
});
const handleSave = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
try {
yield db.project.update({
where: {
id: Number(req.params["projectId"]),
},
data: {
draft: Buffer.from(JSON.stringify(req.body)),
},
});
resp.status(200);
}
catch (e) {
console.log(e);
resp.status(500);
}
finally {
resp.end();
}
});
const handleSavedGet = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
try {
const r = yield db.project.findUnique({
where: {
id: Number(req.params["projectId"]),
},
select: {
state: true,
draft: true,
},
});
if (r == null) {
resp.status(404);
}
else {
resp.status(200);
resp.header("content-type", "application/json");
if (r.draft == null) {
if (r.state == null) {
resp.send({
nodes: [],
edges: [],
viewport: { x: 0, y: 0, zoom: 1 },
});
}
else {
resp.send(JSON.parse(Buffer.from(r.state).toString("utf8")));
}
}
else {
resp.send(JSON.parse(Buffer.from(r.draft).toString("utf8")));
}
}
}
catch (e) {
console.log(e);
resp.status(500);
}
finally {
resp.end();
}
});
const handleDelete = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
try {
const projectId = Number(req.params["projectId"]);
const p = yield db.project.findUnique({
where: {
id: projectId,
},
select: {
instanceId: true,
},
});
if (p === null) {
resp.status(404);
return;
}
const r = yield axios_1.default.request({
url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/instance/${p.instanceId}/remove`,
method: "post",
});
if (r.status === 200) {
yield db.project.delete({
where: {
id: projectId,
},
});
}
resp.status(200);
}
catch (e) {
console.log(e);
resp.status(500);
}
finally {
resp.end();
}
});
const handleDeploy = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
try {
const projectId = Number(req.params["projectId"]);
const state = Buffer.from(JSON.stringify(req.body.state));
const p = yield db.project.findUnique({
where: {
id: projectId,
},
select: {
instanceId: true,
},
});
if (p === null) {
resp.status(404);
return;
}
yield db.project.update({
where: {
id: projectId,
},
data: {
draft: state,
},
});
let r;
if (p.instanceId == null) {
r = yield axios_1.default.request({
url: "http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app",
method: "post",
data: {
config: req.body.config,
},
});
if (r.status === 200) {
yield db.project.update({
where: {
id: projectId,
},
data: {
state,
draft: null,
instanceId: r.data.id,
deployKey: r.data.deployKey,
},
});
}
}
else {
r = yield axios_1.default.request({
url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app/${p.instanceId}`,
method: "put",
data: {
config: req.body.config,
},
});
if (r.status === 200) {
yield db.project.update({
where: {
id: projectId,
},
data: {
state,
draft: null,
},
});
}
}
}
catch (e) {
console.log(e);
resp.status(500);
}
finally {
resp.end();
}
});
const handleStatus = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
try {
const projectId = Number(req.params["projectId"]);
const p = yield db.project.findUnique({
where: {
id: projectId,
},
select: {
instanceId: true,
},
});
console.log(projectId, p);
if (p === null) {
resp.status(404);
return;
}
if (p.instanceId == null) {
resp.status(404);
return;
}
const r = yield axios_1.default.request({
url: `http://appmanager.hgrz-appmanager.svc.cluster.local/api/tasks/${p.instanceId}`,
method: "get",
});
resp.status(r.status);
if (r.status === 200) {
resp.write(JSON.stringify(r.data));
}
}
catch (e) {
console.log(e);
resp.status(500);
}
finally {
resp.end();
}
});
function start() {
return __awaiter(this, void 0, void 0, function* () {
yield db.$connect();
const app = (0, express_1.default)();
app.use(express_1.default.json());
app.post("/api/project/:projectId/saved", handleSave);
app.get("/api/project/:projectId/saved", handleSavedGet);
app.post("/api/project/:projectId/deploy", handleDeploy);
app.get("/api/project/:projectId/status", handleStatus);
app.delete("/api/project/:projectId", handleDelete);
app.get("/api/project", handleProjectAll);
app.post("/api/project", handleProjectCreate);
app.use("/", express_1.default.static("../front/dist"));
app.listen(node_process_1.env.DODO_PORT_WEB, () => {
console.log("started");
});
});
}
start();