Canvas: build application infrastructure with drag and drop
Change-Id: I5cfd12e67794f3376c5c025af29470d52d77cf16
diff --git a/apps/canvas/server/index.js b/apps/canvas/server/index.js
new file mode 100644
index 0000000..0cbf5e0
--- /dev/null
+++ b/apps/canvas/server/index.js
@@ -0,0 +1,203 @@
+"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 node_http_1 = require("node:http");
+const express_1 = __importDefault(require("express"));
+const privateKey = `-----BEGIN OPENSSH PRIVATE KEY-----
+b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
+QyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQAAAKA42oIlONqC
+JQAAAAtzc2gtZWQyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQ
+AAAEC2CdpXtaFfqA8/mqjf9uITU1mrPOI4CeWgiQFEefFW1/K58vMu0MwIzdZT+mqpIBkh
+l48p9+/YwDCZv7MgTesFAAAAGXJvb3RAY2FudmFzYnVpbGRlci1tYXN0ZXIBAgME
+-----END OPENSSH PRIVATE KEY-----`;
+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: 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: 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: {
+ draft: true,
+ }
+ });
+ if (r == null) {
+ resp.status(404);
+ }
+ else {
+ resp.status(200);
+ resp.header("content-type", "application/json");
+ if (r.draft == null) {
+ resp.send({
+ nodes: [],
+ edges: [],
+ viewport: { x: 0, y: 0, zoom: 1 },
+ });
+ }
+ else {
+ resp.send(JSON.parse(r.draft.toString()));
+ }
+ }
+ }
+ catch (e) {
+ console.log(e);
+ resp.status(500);
+ }
+ finally {
+ resp.end();
+ }
+});
+const handleDeploy = (req, resp) => __awaiter(void 0, void 0, void 0, function* () {
+ try {
+ console.log(req.params);
+ const state = Buffer.from(JSON.stringify(req.body.state));
+ yield db.project.update({
+ where: {
+ id: Number(req.params["projectId"]),
+ },
+ data: {
+ draft: state,
+ },
+ });
+ const result = yield new Promise((resolve) => {
+ const r = (0, node_http_1.request)("http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app", {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ },
+ }, (res) => {
+ res.on("end", () => {
+ resolve(res.statusCode);
+ });
+ res.on("close", () => {
+ resolve(res.statusCode);
+ });
+ res.on("error", () => {
+ resolve(res.statusCode);
+ });
+ res.on("data", (data) => console.log(data));
+ res.on("pause", () => console.log("pause"));
+ res.on("readable", () => console.log("readable"));
+ res.on("resume", () => console.log("resume"));
+ });
+ r.write(JSON.stringify({
+ id: req.params["projectId"],
+ sshPrivateKey: privateKey,
+ config: req.body.config,
+ }));
+ r.end();
+ });
+ resp.status(result);
+ if (result === 200) {
+ yield db.project.update({
+ where: {
+ id: Number(req.params["projectId"]),
+ },
+ data: {
+ state,
+ },
+ });
+ }
+ }
+ 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", handleProjectAll);
+ app.post("/api/project", handleProjectCreate);
+ app.use("/assets", express_1.default.static("../dist/assets"));
+ app.use("/", express_1.default.static("../dist"));
+ app.listen(3000, () => {
+ console.log("started");
+ });
+ });
+}
+start();