From aa88ddcc2fb670dc694831d3198f9b9e516234ad Mon Sep 17 00:00:00 2001 From: Devin Cowan Date: Fri, 12 Jan 2024 14:56:42 -0500 Subject: [PATCH 1/2] WIP use caddy --- app/docker-compose.yml | 2 +- app/frontend/Dockerfile | 43 +++++++++++++++++------ app/frontend/caddy.docker.json | 58 +++++++++++++++++++++++++++++++ app/frontend/docker-entrypoint.sh | 5 ++- 4 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 app/frontend/caddy.docker.json diff --git a/app/docker-compose.yml b/app/docker-compose.yml index 3f5023c1..6409b52f 100644 --- a/app/docker-compose.yml +++ b/app/docker-compose.yml @@ -24,7 +24,7 @@ services: dockerfile: ./frontend/Dockerfile context: ./ ports: - - 8080:5003 + - 8080:8080 restart: unless-stopped depends_on: - api diff --git a/app/frontend/Dockerfile b/app/frontend/Dockerfile index b5d09d43..c1820c91 100644 --- a/app/frontend/Dockerfile +++ b/app/frontend/Dockerfile @@ -13,14 +13,35 @@ RUN npm install ADD frontend . RUN npm run build -FROM nginx:1.24.0 as production-stage -RUN mkdir /app -COPY --from=build-stage /app/dist /app -# COPY --from=build-stage .env .env -COPY --from=build-stage /app/nginx.conf /etc/nginx/nginx.conf - -# Allow substitution of env vars at runtime -COPY frontend/docker-entrypoint.sh / -RUN chmod +x /docker-entrypoint.sh -ENTRYPOINT ["/docker-entrypoint.sh"] -EXPOSE 5003 \ No newline at end of file +# FROM nginx:1.24.0 as production-stage +# RUN mkdir /app +# COPY --from=build-stage /app/dist /app +# # COPY --from=build-stage .env .env +# COPY --from=build-stage /app/nginx.conf /etc/nginx/nginx.conf + +# # Allow substitution of env vars at runtime +# COPY frontend/docker-entrypoint.sh / +# RUN chmod +x /docker-entrypoint.sh +# ENTRYPOINT ["/docker-entrypoint.sh"] +# EXPOSE 5003 +# CMD ["nginx" "-g" "daemon off;"] + +# Production layer +FROM caddy:2.7.6-alpine as prod + +# Install envsubst command for replacing __env files +# RUN set -x \ +# && apk add gettext libintl + +COPY frontend/docker-entrypoint.sh /usr/local/bin/ + +# Copy config +COPY frontend/caddy.docker.json /etc/caddy/caddy.json + +# Copy source dist +COPY --from=build-stage /app/dist /srv + +EXPOSE 8080 + +ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] +CMD ["caddy", "run", "--config", "/etc/caddy/caddy.json"] \ No newline at end of file diff --git a/app/frontend/caddy.docker.json b/app/frontend/caddy.docker.json new file mode 100644 index 00000000..403ffad4 --- /dev/null +++ b/app/frontend/caddy.docker.json @@ -0,0 +1,58 @@ +{ + "apps": + { + "http": + { + "servers": + { + "web": + { + "automatic_https": + { + "disable": true + }, + "listen": + [ + ":8080" + ], + "routes": + [ + { + "handle": + [ + { + "handler": "file_server", + "root": "/srv" + } + ], + "match": + [ + { + "path_regexp": + { + "name": "asset", + "pattern": "\\.(br|css|gz|ico|jpg|js|mp3|mp4|png|svg)$" + } + } + ], + "terminal": true + }, + { + "handle": + [ + { + "handler": "rewrite", + "uri": "/index.html" + }, + { + "handler": "file_server", + "root": "/srv" + } + ] + } + ] + } + } + } + } +} \ No newline at end of file diff --git a/app/frontend/docker-entrypoint.sh b/app/frontend/docker-entrypoint.sh index 1f50e151..5951b171 100755 --- a/app/frontend/docker-entrypoint.sh +++ b/app/frontend/docker-entrypoint.sh @@ -1,5 +1,5 @@ #!/bin/sh -ROOT_DIR=/app +ROOT_DIR=/srv # Replace env vars in files served by NGINX for file in $ROOT_DIR/assets/*.js $ROOT_DIR/index.html; do @@ -10,5 +10,4 @@ do sed -i 's|VITE_APP_BASE_PLACEHOLDER|'${VITE_APP_BASE}'|g' $file done -echo "Starting Nginx" -nginx -g 'daemon off;' \ No newline at end of file +exec "$@" \ No newline at end of file From cd6e31ccd9cfbd8cbb688685851a7d1d962ea340 Mon Sep 17 00:00:00 2001 From: Devin Cowan Date: Fri, 12 Jan 2024 15:34:10 -0500 Subject: [PATCH 2/2] app_base env trailing slash --- app/env.template | 1 + app/frontend/src/auth.js | 2 +- app/frontend/src/constants.js | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/env.template b/app/env.template index 0a368497..94ed3045 100644 --- a/app/env.template +++ b/app/env.template @@ -23,6 +23,7 @@ VITE_APP_ORIGIN=https://localhost # for nested static deployment, set VITE_APP_BASE=/domain-subsetter/ VITE_APP_BASE=/ +# note must end with trailing slash due to vite_app_base # VITE_APP_FULL_URL=${VITE_APP_ORIGIN}${VITE_APP_BASE} VITE_APP_FULL_URL=https://localhost/ diff --git a/app/frontend/src/auth.js b/app/frontend/src/auth.js index c7e10fe7..97fd4311 100644 --- a/app/frontend/src/auth.js +++ b/app/frontend/src/auth.js @@ -12,7 +12,7 @@ export async function logIn(callback) { const authUrl = new URL(json.authorization_url) // TODO: use an env var for auth redirect instead of hard-coding // "#" hash routing was not passed from github env secret so had to hard code here. - authUrl.searchParams.set('redirect_uri', `${APP_URL}/#/auth-redirect`) + authUrl.searchParams.set('redirect_uri', `${APP_URL}#/auth-redirect`) window.open( authUrl.toString(), '_blank', diff --git a/app/frontend/src/constants.js b/app/frontend/src/constants.js index 4a2e77df..bb370123 100644 --- a/app/frontend/src/constants.js +++ b/app/frontend/src/constants.js @@ -1,6 +1,8 @@ -export const APP_URL = import.meta.env.VITE_APP_FULL_URL || "VITE_APP_FULL_URL_PLACEHOLDER"; export const APP_BASE = import.meta.env.VITE_APP_BASE || "VITE_APP_BASE_PLACEHOLDER"; +let APP_URL_IN = import.meta.env.VITE_APP_FULL_URL || "VITE_APP_FULL_URL_PLACEHOLDER"; +export const APP_URL = APP_URL_IN.endsWith("/") ? APP_URL_IN : `${APP_URL_IN}/` + export const API_BASE = import.meta.env.VITE_APP_API_URL || "VITE_APP_API_URL_PLACEHOLDER"; export const ENDPOINTS = { openapi: `${API_BASE}/openapi.json`,