diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 000000000..ace5daf5a --- /dev/null +++ b/Caddyfile @@ -0,0 +1,11 @@ +:3500 { + handle_path /admin* { + reverse_proxy localhost:3100 + } + + handle_path /backend* { + reverse_proxy localhost:3170 + } + + reverse_proxy localhost:3000 # Proxy other requests to your server running on port 3001 +} diff --git a/individual-containers-subpath-access.Caddyfile b/individual-containers-subpath-access.Caddyfile new file mode 100644 index 000000000..e69de29bb diff --git a/packages/hoppscotch-sh-admin/prod_run.mjs b/packages/hoppscotch-sh-admin/prod_run.mjs index 9e386cb98..0d94bb499 100755 --- a/packages/hoppscotch-sh-admin/prod_run.mjs +++ b/packages/hoppscotch-sh-admin/prod_run.mjs @@ -1,6 +1,36 @@ #!/usr/local/bin/node -import { execSync } from "child_process" +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("VITE_")) @@ -16,3 +46,19 @@ fs.writeFileSync("build.env", envFileContent) execSync(`npx import-meta-env -x build.env -e build.env -p "/site/**/*"`) fs.rmSync("build.env") + +const caddyFileName = process.env.ENABLE_SUBPATH_BASED_ACCESS === 'true' ? 'sh-admin-subpath-access.Caddyfile' : 'sh-admin-multiport-setup.Caddyfile' +const caddyProcess = runChildProcessWithPrefix("caddy", ["run", "--config", `/etc/caddy/${caddyFileName}`, "--adapter", "caddyfile"], "App/Admin Dashboard Caddy") + +caddyProcess.on("exit", (code) => { + console.log(`Exiting process because Caddy Server exited with code ${code}`) + process.exit(code) +}) + +process.on('SIGINT', () => { + console.log("SIGINT received, exiting...") + + caddyProcess.kill("SIGINT") + + process.exit(0) +}) diff --git a/packages/hoppscotch-sh-admin/sh-admin-multiport-setup.Caddyfile b/packages/hoppscotch-sh-admin/sh-admin-multiport-setup.Caddyfile new file mode 100644 index 000000000..b47835dfe --- /dev/null +++ b/packages/hoppscotch-sh-admin/sh-admin-multiport-setup.Caddyfile @@ -0,0 +1,5 @@ +:8080 { + try_files {path} / + root * /site/sh-admin-multiport-setup + file_server +} diff --git a/packages/hoppscotch-sh-admin/Caddyfile b/packages/hoppscotch-sh-admin/sh-admin-subpath-access.Caddyfile similarity index 53% rename from packages/hoppscotch-sh-admin/Caddyfile rename to packages/hoppscotch-sh-admin/sh-admin-subpath-access.Caddyfile index 4d9382b53..ecf61c2ee 100644 --- a/packages/hoppscotch-sh-admin/Caddyfile +++ b/packages/hoppscotch-sh-admin/sh-admin-subpath-access.Caddyfile @@ -1,5 +1,5 @@ :8080 { try_files {path} / - root * /site + root * /site/sh-admin-subpath-access file_server } diff --git a/prod.Dockerfile b/prod.Dockerfile index 2bad882b1..f456c3d48 100644 --- a/prod.Dockerfile +++ b/prod.Dockerfile @@ -48,8 +48,10 @@ RUN pnpm run build --outDir dist-subpath-access --base /admin/ FROM caddy:2-alpine as sh_admin WORKDIR /site COPY --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/prod_run.mjs /usr -COPY --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/Caddyfile /etc/caddy/Caddyfile -COPY --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/dist/ . +COPY --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/sh-admin-multiport-setup.Caddyfile /etc/caddy/sh-admin-multiport-setup.Caddyfile +COPY --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/sh-admin-subpath-access.Caddyfile /etc/caddy/sh-admin-subpath-access.Caddyfile +COPY --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/dist /site/sh-admin-multiport-setup +COPY --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/dist-subpath-access /site/sh-admin-subpath-access RUN apk add nodejs npm RUN npm install -g @import-meta-env/cli EXPOSE 8080