UNPKG

@metacall/faas

Version:

Reimplementation of MetaCall FaaS platform written in TypeScript.

64 lines (63 loc) 2.61 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deploy = void 0; const os_1 = require("os"); const fs = __importStar(require("fs")); const path_1 = __importDefault(require("path")); const app_1 = require("../app"); const appError_1 = __importDefault(require("../utils/appError")); const deploy_1 = require("../utils/deploy"); const install_1 = require("../utils/install"); const catch_1 = require("./catch"); exports.deploy = catch_1.catchAsync(async (req, res, next) => { try { const application = app_1.Applications[req.body.suffix]; // Check if the application exists and it is stored if (!application?.resource) { throw new appError_1.default(`Invalid deployment id: ${req.body.suffix}`, 400); } const resource = await application.resource; // Store the environment variables for when reloading the FaaS const env = {}; for (const envVar of req.body.env) { env[envVar.name] = envVar.value; } const envFilePath = path_1.default.join(resource.path, `.env`); const envFileContent = Object.entries(env) .map(([key, value]) => `${key}=${value}`) .join('\n'); fs.appendFileSync(envFilePath, envFileContent, 'utf-8'); await install_1.installDependencies(resource); await deploy_1.deployProcess(resource, env); return res.status(200).json({ prefix: os_1.hostname(), suffix: resource?.id, version: 'v1' }); } catch (err) { return next(err); } });