feat(package): ship the web UI in the wheel and serve it from the API

pip install docsgpt now brings the web UI with it: `docsgpt api` serves the
API and the UI on one port.

- scripts/build_frontend.sh builds the frontend into docsgpt/static
  (gitignored) the way the frontend image does: .env.development as the
  production baseline, and index.html loading /config.js ahead of the
  bundle. hatch admits the directory into the wheel and the sdist through
  `artifacts`; the package workflows run the script before `uv build` and
  fail if the wheel lacks the UI. The backend image keeps ignoring it.
- docsgpt/ui.py serves the build in front of Flask: files as they are,
  hashed assets immutable, Flask's own path prefixes (taken from its URL map,
  so new blueprints need no registration) passed through, every other GET
  rendered as index.html for the client-side router. /config.js is generated
  per request with VITE_API_HOST and VITE_BASE_URL set to the page's origin,
  VITE_* environment variables winning. SERVE_UI=false leaves the API alone.
- docsgpt api configures gunicorn in code (gunicorn.app.base.Application)
  instead of rewriting sys.argv, so the SIGUSR2 re-exec that gunicorn uses
  for zero-downtime upgrades runs the docsgpt console script again and
  works; verified with a live handover.
- Docs: the pip page says the UI is included, that DOCSGPT_HOME and
  DOCSGPT_ENV_FILE are process environment variables rather than .env
  entries, and the settings page describes SERVE_UI.
This commit is contained in:
Alex committed 2026-09-09 11:09:09 +01:00
1 parent 0c348917b1
commit 43f493b926
15 files changed
+419 -39

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
# Build the web UI into docsgpt/static so the Python package ships it.
#
# The API serves that directory when it exists (docsgpt/ui.py). The package
# workflows run this before `uv build`; run it locally to test the served UI
# or to build a wheel with the UI in it.
set -euo pipefail
cd "$(dirname "$0")/../frontend"
if [ ! -d node_modules ] || [ "${CI:-}" = "true" ]; then
npm ci --include=dev
fi
if [ -f .env.local ]; then
echo "note: frontend/.env.local exists; its VITE_* values are baked into this build" >&2
fi
# As in frontend/Dockerfile: the committed .env.development is the baseline
# of the production build. The API rewrites VITE_API_HOST and VITE_BASE_URL
# at runtime through /config.js, so those baked values never reach a browser.
cp .env.development .env.production.local
trap 'rm -f .env.production.local' EXIT
npm run build
# Load the runtime config ahead of the bundle, as the nginx image does.
sed -i.bak 's|<head>|<head><script src="/config.js"></script>|' dist/index.html
rm -f dist/index.html.bak
grep -q 'src="/config.js"' dist/index.html
rm -rf ../docsgpt/static
cp -R dist ../docsgpt/static
echo "built docsgpt/static ($(du -sh ../docsgpt/static | cut -f1))"