From aebcbac9794af9a66586f6aaf46ce78cb8015a3f Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 1 Nov 2023 14:42:10 +0530 Subject: [PATCH] chore: generate multiple builds for sh-admin Enables seamless transition to/from subpath based access --- .env.example | 2 +- aio.Caddyfile | 81 +++++++++++++++++++++++++--------------------- docker-compose.yml | 1 + prod.Dockerfile | 4 +++ 4 files changed, 50 insertions(+), 38 deletions(-) diff --git a/.env.example b/.env.example index e962469a0..37e8ad0db 100644 --- a/.env.example +++ b/.env.example @@ -61,4 +61,4 @@ VITE_APP_TOS_LINK=https://docs.hoppscotch.io/support/terms VITE_APP_PRIVACY_POLICY_LINK=https://docs.hoppscotch.io/support/privacy # Set to `true` for subpath based access -VITE_INCLUDE_ADMIN_PREFIX=false +ENABLE_SUBPATH_BASED_ACCESS=false diff --git a/aio.Caddyfile b/aio.Caddyfile index 101a03468..b9844e040 100644 --- a/aio.Caddyfile +++ b/aio.Caddyfile @@ -1,40 +1,47 @@ -# TODO: Uncomment the below lines and update the subpath-based implementation to use standard HTTP/HTTPS ports - -# :3000 { -# try_files {path} / -# root * /site/selfhost-web -# file_server -# } - -# :3100 { -# try_files {path} / -# root * /site/sh-admin -# file_server -# } +# TODO: Update the subpath-based implementation to use standard HTTP(S) ports :3000 { - # Serve the `selfhost-web` SPA by default - root * /site/selfhost-web - file_server - - # Handle requests under `/admin*` - handle_path /admin* { - root * /site/sh-admin - file_server - - # Ensures any non-existent file in the server is routed to the SPA - try_files {path} / - } - - # Handle requests under `/backend*` path - handle_path /backend* { - reverse_proxy localhost:3170 - } - - # Catch-all route for unknown paths, serves `selfhost-web` SPA - handle { - root * /site/selfhost-web - file_server - try_files {path} / - } + try_files {path} / + root * /site/selfhost-web + file_server +} + +:3100 { + try_files {path} / + root * /site/sh-admin + file_server +} + +:8080 { + # Serve the `selfhost-web` SPA by default + root * /site/selfhost-web + file_server + + handle_path /admin* { + # Check the ENABLE_SUBPATH_BASED_ACCESS environment variable + @enabledSubpathBasedAccess { + expression {env.ENABLE_SUBPATH_BASED_ACCESS} == "true" + } + + # Conditionally serve /site/sh-admin-subpath-access if ENABLE_SUBPATH_BASED_ACCESS is true + handle @enabledSubpathBasedAccess { + root * /site/sh-admin-subpath-access + file_server + + # Ensures any non-existent file in the server is routed to the SPA + try_files {path} / + } + } + + # Handle requests under `/backend*` path + handle_path /backend* { + reverse_proxy localhost:3170 + } + + # Catch-all route for unknown paths, serves `selfhost-web` SPA + handle { + root * /site/selfhost-web + file_server + try_files {path} / + } } diff --git a/docker-compose.yml b/docker-compose.yml index c9d5d6684..423a8cce0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -76,6 +76,7 @@ services: - "3000:3000" - "3100:3100" - "3170:3170" + - "3500:8080" # The preset DB service, you can delete/comment the below lines if # you are using an external postgres instance diff --git a/prod.Dockerfile b/prod.Dockerfile index 28cbab316..c6e0bfce3 100644 --- a/prod.Dockerfile +++ b/prod.Dockerfile @@ -41,7 +41,9 @@ CMD ["/bin/sh", "-c", "node /usr/prod_run.mjs && caddy run --config /etc/caddy/C FROM base_builder as sh_admin_builder WORKDIR /usr/src/app/packages/hoppscotch-sh-admin +# Generate two builds for `sh-admin`, one based on subpath-access and the regular build RUN pnpm run build +RUN pnpm run build --outDir dist-subpath-access --base /admin/ FROM caddy:2-alpine as sh_admin WORKDIR /site @@ -58,6 +60,7 @@ RUN apk add caddy tini 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 --from=sh_admin_builder /usr/src/app/packages/hoppscotch-sh-admin/dist-subpath-access /site/sh-admin-subpath-access COPY aio.Caddyfile /etc/caddy/Caddyfile ENTRYPOINT [ "tini", "--" ] RUN apk --no-cache add curl @@ -67,3 +70,4 @@ CMD ["node", "/usr/src/app/aio_run.mjs"] EXPOSE 3170 EXPOSE 3000 EXPOSE 3100 +EXPOSE 3500