From 16d915bde0d4373dccb83acf1e4ea00506a430a9 Mon Sep 17 00:00:00 2001 From: yzxcj797 <1784931579@qq.com> Date: Thu, 13 Aug 2026 12:51:42 +0800 Subject: [PATCH] Allow configuring backend URLs at container startup Fixes #1025 --- .gitattributes | 1 + .github/workflows/build.yml | 2 + Dockerfile | 4 +- README.md | 12 ++++ docker/40-drawdb-runtime-config.sh | 24 +++++++ index.html | 1 + package.json | 1 + public/runtime-config.js | 4 ++ src/api/email.js | 3 +- src/api/gists.js | 6 +- src/config/runtime.js | 67 ++++++++++++++++++++ test/runtime-config-entrypoint.test.js | 51 +++++++++++++++ test/runtime-config.test.js | 86 ++++++++++++++++++++++++++ 13 files changed, 256 insertions(+), 6 deletions(-) create mode 100644 .gitattributes create mode 100644 docker/40-drawdb-runtime-config.sh create mode 100644 public/runtime-config.js create mode 100644 src/config/runtime.js create mode 100644 test/runtime-config-entrypoint.test.js create mode 100644 test/runtime-config.test.js diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..dfdb8b771 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.sh text eol=lf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a2fe5b206..851974075 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -24,6 +24,8 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'npm' - run: npm install + - name: Run tests + run: npm test - name: Run eslint run: npm run lint - name: Run vite build diff --git a/Dockerfile b/Dockerfile index 1f87fd391..d30539fee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,8 @@ RUN npm run build # Stage 2: Setup the Nginx Server to serve the app FROM docker.io/library/nginx:stable-alpine3.23 AS production COPY --from=build /app/dist /usr/share/nginx/html -RUN echo 'server { listen 80; server_name _; root /usr/share/nginx/html; location / { try_files $uri /index.html; } }' > /etc/nginx/conf.d/default.conf +COPY docker/40-drawdb-runtime-config.sh /docker-entrypoint.d/ +RUN chmod +x /docker-entrypoint.d/40-drawdb-runtime-config.sh +RUN echo 'server { listen 80; server_name _; root /usr/share/nginx/html; location = /runtime-config.js { add_header Cache-Control "no-store"; } location / { try_files $uri /index.html; } }' > /etc/nginx/conf.d/default.conf EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] diff --git a/README.md b/README.md index 61ce89ecb..1a9123f82 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,18 @@ docker run -p 3000:80 drawdb If you want to enable sharing, set up the [server](https://github.com/drawdb-io/drawdb-server) and environment variables according to `.env.sample`. This is optional unless you need to share files. +The prebuilt Docker image reads backend URLs when the container starts, so it +does not need to be rebuilt for each environment: + +```bash +docker run -p 3000:80 \ + -e VITE_BACKEND_URL=https://drawdb-server.example.com \ + ghcr.io/drawdb-io/drawdb:latest +``` + +Set `VITE_GIST_BACKEND_URL` as well to route sharing requests to a different +backend. + ## Contributing Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on how to contribute to this project. diff --git a/docker/40-drawdb-runtime-config.sh b/docker/40-drawdb-runtime-config.sh new file mode 100644 index 000000000..35841242c --- /dev/null +++ b/docker/40-drawdb-runtime-config.sh @@ -0,0 +1,24 @@ +#!/bin/sh +set -eu + +encode() { + printf '%s' "$1" | base64 | tr -d '\r\n' +} + +backend_url="$(encode "${VITE_BACKEND_URL:-}")" +gist_backend_url="$(encode "${VITE_GIST_BACKEND_URL:-}")" +output_path="${1:-/usr/share/nginx/html/runtime-config.js}" + +write_config() { + printf '%s\n' \ + 'globalThis.__DRAWDB_RUNTIME_CONFIG__ = Object.freeze({' \ + " VITE_BACKEND_URL: \"${backend_url}\"," \ + " VITE_GIST_BACKEND_URL: \"${gist_backend_url}\"," \ + '});' +} + +if [ "${output_path}" = '-' ]; then + write_config +else + write_config > "${output_path}" +fi diff --git a/index.html b/index.html index dd19a675f..6f575f60f 100644 --- a/index.html +++ b/index.html @@ -55,6 +55,7 @@
+