From 9e595ec5943d3c35710096ff9a530f168f08542b Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Sun, 16 Jul 2023 01:19:17 +0530 Subject: [PATCH] feat: initial aio container implementation --- aio.Caddyfile | 11 +++++++++ aio_run.mjs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ prod.Dockerfile | 15 ++++++++++-- 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 aio.Caddyfile create mode 100644 aio_run.mjs diff --git a/aio.Caddyfile b/aio.Caddyfile new file mode 100644 index 000000000..65919f6e8 --- /dev/null +++ b/aio.Caddyfile @@ -0,0 +1,11 @@ +:3000 { + try_files {path} / + root * /site/selfhost-web + file_server +} + +:3100 { + try_files {path} / + root * /site/sh-admin + file_server +} diff --git a/aio_run.mjs b/aio_run.mjs new file mode 100644 index 000000000..eadfeec5b --- /dev/null +++ b/aio_run.mjs @@ -0,0 +1,62 @@ +#!/usr/local/bin/node +// @ts-check + +import { execSync, spawn } from "child_process" +import fs from "fs" +import process from "process" + +function runChildProcessWithPrefix(command, args, prefix) { + const childProcess = spawn(command, args); + + childProcess.stdout.on('data', (data) => { + const output = data.toString().trim().split('\n'); + output.forEach((line) => { + console.log(`${prefix} | ${line}`); + }); + }); + + childProcess.stderr.on('data', (data) => { + const error = data.toString().trim().split('\n'); + error.forEach((line) => { + console.error(`${prefix} | ${line}`); + }); + }); + + childProcess.on('close', (code) => { + console.log(`${prefix} Child process exited with code ${code}`); + }); + + childProcess.on('error', (stuff) => { + console.log("error") + console.log(stuff) + }) + + return childProcess +} + +const envFileContent = Object.entries(process.env) + .filter(([env]) => env.startsWith("APP_")) + .map(([env, val]) => `${env}=${ + (val.startsWith("\"") && val.endsWith("\"")) + ? val + : `"${val}"` + }`) + .join("\n") + +fs.writeFileSync("build.env", envFileContent) + +execSync(`npx import-meta-env -x build.env -e build.env -p "/site/**/*"`) + +fs.rmSync("build.env") + +const caddyProcess = runChildProcessWithPrefix("caddy", ["run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"], "App/Admin Dashboard Caddy") +const backendProcess = runChildProcessWithPrefix("pnpm", ["run", "start:prod"], "Backend Server") + +process.on('SIGINT', () => { + console.log("SIGINT received, exiting...") + + caddyProcess.kill("SIGINT") + backendProcess.kill("SIGINT") + + process.exit(0) +}) diff --git a/prod.Dockerfile b/prod.Dockerfile index 93ce72bc2..88f2fe8ea 100644 --- a/prod.Dockerfile +++ b/prod.Dockerfile @@ -1,4 +1,4 @@ -FROM node:18 as base_builder +FROM node:18-bookworm as base_builder WORKDIR /usr/src/app @@ -7,7 +7,7 @@ COPY pnpm-lock.yaml . RUN pnpm fetch COPY . . -RUN pnpm install --force --offline +RUN pnpm install --force --prefer-offline FROM base_builder as backend WORKDIR /usr/src/app/packages/hoppscotch-backend @@ -44,3 +44,14 @@ RUN npm install -g @import-meta-env/cli EXPOSE 8080 CMD ["/bin/sh", "-c", "node /usr/prod_run.mjs && caddy run --config /etc/caddy/Caddyfile --adapter caddyfile"] +FROM backend as aio +RUN apt-get update +RUN apt-get install -y caddy +RUN npm install -g @import-meta-env/cli +COPY --from=fe_builder /usr/src/app/packages/hoppscotch-selfhost-web/dist /site/selfhost-web +COPY --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/dist /site/sh-admin +COPY aio.Caddyfile /etc/caddy/Caddyfile +CMD ["/bin/sh", "-c", "node /usr/src/app/aio_run.mjs"] +EXPOSE 3170 +EXPOSE 3000 +EXPOSE 3100