From df161ef38ecf872855b9639547a540af9c3d999c Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Thu, 24 Jun 2021 21:31:49 -0700 Subject: [PATCH] Docker: add .env configurable FASTAPI_BACKEND By default we now use uvicorn because it has a much better developer feedback out of the box. We'll work on hypercorn logging, but for now, hypercorn is usable via: `docker-compose --env-file docker/hypercorn.env up nginx`. Signed-off-by: Kevin Morris --- .env | 1 + Dockerfile | 4 ++-- docker-compose.yml | 4 ++-- docker/health/fastapi.sh | 2 +- docker/hypercorn.env | 1 + docker/scripts/run-fastapi.sh | 22 ++++++++++++++++------ 6 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 .env create mode 100644 docker/hypercorn.env diff --git a/.env b/.env new file mode 100644 index 00000000..f4a585ba --- /dev/null +++ b/.env @@ -0,0 +1 @@ +FASTAPI_BACKEND="uvicorn" diff --git a/Dockerfile b/Dockerfile index 002f135a..5fd3ec07 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,8 @@ RUN pacman -Syu --noconfirm --noprogressbar \ python-requests python-aiofiles python-python-multipart \ python-pytest-asyncio python-coverage hypercorn python-bcrypt \ python-email-validator openssh python-lxml mariadb mariadb-libs \ - python-isort flake8 cgit uwsgi uwsgi-plugin-cgi php php-fpm + python-isort flake8 cgit uwsgi uwsgi-plugin-cgi php php-fpm \ + python-asgiref uvicorn RUN useradd -U -d /aurweb -c 'AUR User' aur @@ -30,4 +31,3 @@ ENV PYTHONPATH=/aurweb ENV AUR_CONFIG=conf/config RUN make -C po all install -RUN python setup.py install diff --git a/docker-compose.yml b/docker-compose.yml index 0f61db75..f99264af 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -124,9 +124,9 @@ services: environment: - AUR_CONFIG=conf/config entrypoint: /docker/fastapi-entrypoint.sh - command: /docker/scripts/run-fastapi.sh + command: /docker/scripts/run-fastapi.sh "${FASTAPI_BACKEND}" healthcheck: - test: "bash /docker/health/fastapi.sh" + test: "bash /docker/health/fastapi.sh ${FASTAPI_BACKEND}" interval: 2s timeout: 30s depends_on: diff --git a/docker/health/fastapi.sh b/docker/health/fastapi.sh index 19a1c733..e1df348d 100755 --- a/docker/health/fastapi.sh +++ b/docker/health/fastapi.sh @@ -1,2 +1,2 @@ #!/bin/bash -exec pgrep hypercorn +exec pgrep "$1" diff --git a/docker/hypercorn.env b/docker/hypercorn.env new file mode 100644 index 00000000..0d65e241 --- /dev/null +++ b/docker/hypercorn.env @@ -0,0 +1 @@ +FASTAPI_BACKEND="hypercorn" diff --git a/docker/scripts/run-fastapi.sh b/docker/scripts/run-fastapi.sh index 6c420497..1db4c505 100755 --- a/docker/scripts/run-fastapi.sh +++ b/docker/scripts/run-fastapi.sh @@ -3,9 +3,19 @@ # Initialize the new database; ignore errors. python -m aurweb.initdb 2>/dev/null || /bin/true -exec hypercorn --reload \ - --certfile /cache/localhost.cert.pem \ - --keyfile /cache/localhost.key.pem \ - --error-logfile - \ - --log-config docker/logging.conf \ - -b "0.0.0.0:8000" aurweb.asgi:app +if [ "$1" == "uvicorn" ] || [ "$1" == "" ]; then + exec uvicorn --reload \ + --ssl-certfile /cache/localhost.cert.pem \ + --ssl-keyfile /cache/localhost.key.pem \ + --log-config /docker/logging.conf \ + --host "0.0.0.0" \ + --port 8000 \ + aurweb.asgi:app +else + exec hypercorn --reload \ + --certfile /cache/localhost.cert.pem \ + --keyfile /cache/localhost.key.pem \ + --log-config /docker/logging.conf \ + -b "0.0.0.0:8000" \ + aurweb.asgi:app +fi