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

+22 -11
View File
@@ -35,21 +35,32 @@ class TestTopLevel:
class TestApi:
def test_gunicorn_argv(self, monkeypatch, capsys):
run = MagicMock()
monkeypatch.setitem(sys.modules, "gunicorn.app.wsgiapp", types.SimpleNamespace(run=run))
def test_gunicorn_runs_with_the_image_settings_and_leaves_argv_alone(self, monkeypatch, capsys):
application = MagicMock()
factory = MagicMock(return_value=application)
monkeypatch.setattr(cli, "_gunicorn_application", factory)
monkeypatch.setattr(sys, "platform", "linux")
monkeypatch.setattr(sys, "argv", ["docsgpt"])
monkeypatch.setattr(sys, "argv", ["/venv/bin/docsgpt", "api", "--port", "8000"])
assert cli.main(["api", "--port", "8000", "--workers", "2"]) == 0
run.assert_called_once_with()
argv = sys.argv
assert argv[0] == "gunicorn" and argv[-1] == "docsgpt.asgi:asgi_app"
assert argv[argv.index("--bind") + 1] == "127.0.0.1:8000"
assert argv[argv.index("-w") + 1] == "2"
assert argv[argv.index("-k") + 1] == "docsgpt.gunicorn_worker.BoundedDrainUvicornWorker"
assert argv[argv.index("--config") + 1] == "python:docsgpt.gunicorn_conf"
application.run.assert_called_once_with()
options = factory.call_args.args[0]
assert options["bind"] == "127.0.0.1:8000"
assert options["workers"] == 2
assert options["worker_class"] == "docsgpt.gunicorn_worker.BoundedDrainUvicornWorker"
assert options["max_requests"] == 5000
# gunicorn re-executes sys.argv on SIGUSR2; it must still be the docsgpt invocation.
assert sys.argv == ["/venv/bin/docsgpt", "api", "--port", "8000"]
assert "data home" in capsys.readouterr().err
def test_the_gunicorn_application_carries_the_settings_and_the_log_config(self):
app = cli._gunicorn_application(cli._gunicorn_options("127.0.0.1", 8001, 3))
assert app.cfg.bind == ["127.0.0.1:8001"]
assert app.cfg.workers == 3
assert app.cfg.worker_class_str == "docsgpt.gunicorn_worker.BoundedDrainUvicornWorker"
assert app.cfg.keepalive == 5
assert app.cfg.graceful_timeout == 120
assert "ncsa_access" in app.cfg.logconfig_dict["formatters"], "docsgpt.gunicorn_conf was loaded"
def test_reload_uses_uvicorn(self, monkeypatch):
uvicorn = types.SimpleNamespace(run=MagicMock())
monkeypatch.setitem(sys.modules, "uvicorn", uvicorn)