Skip to content

Conversation

@br3ndonland
Copy link
Owner

@br3ndonland br3ndonland commented Aug 15, 2020

Description

This PR will add main.py app modules for ASGI/Uvicorn, FastAPI, and Starlette, a gunicorn_conf.py Gunicorn configuration file, and prestart.py and start.py start scripts.

Changes

App modules

  • Add app modules (22c9960)
  • Specify app modules in Dockerfile (1fb5eff)
  • Improve directory structure for downstream use (f8211d2)
  • Avoid namespace conflicts between deps and dirs (da0b76d)
  • Customize API response for server in use (4aa2c2c)

Gunicorn

  • Add Gunicorn configuration instructions to README (2fb35f9)
  • Add Gunicorn configuration file (896419a)

Start scripts

  • Translate prestart and start scripts into Python (be69eee)
  • Correct subprocess.run() syntax (70dfe21)
  • Simplify instructions for pre-start script (10762f5)
  • Use same order for app module and Gunicorn conf (20ecdff)

Related

- Clarify instructions for running with mounted volume
- Add instructions for hitting API endpoints
- identify 1.4.25 -> 1.4.26
- isort 5.3.2 -> 5.4.2
- Base (simple Uvicorn ASGI server)
- FastAPI
- Starlette
- This commit will refactor and translate the startup scripts from
  https://github.com/tiangolo/uvicorn-gunicorn-docker.
- prestart.py will replace prestart.sh
- start.py will replace start.sh and start-reload.sh
First argument must be a list, otherwise an error may be seen, like
`bufsize must be an integer` (`bufsize` is the second argument).
- Create /app directory that will be overwritten by downstream images
- Copy start.py and gunicorn_conf.py to root to avoid overwriting
- Add PYTHONPATH=/app environment variable to simplify module paths
- Don't install root package in Docker image: Poetry wasn't installing
  the project, even when running `poetry install` without `--no-root`. I
  don't really need the interlink package installed in the Docker image,
  because I'm changing the directory structure when copying it in.
- Remove extra `RUN` commands: only need one in the base image.
- Improve wording and correct URLs in description
- Expand instructions for using the images in a Dockerfile
- Provide a more thorough explanation of how to hit API endpoints
- Add "One programming language" note on pure Python
The container will now look for main.py and gunicorn_conf.py in:

1. /app/main.py or /app/gunicorn_conf.py
2. /app/app/main.py or /app/app/gunicorn_conf.py

The order was reversed before.
Separate configuration options into general and Gunicorn/Uvicorn
It's getting a little long to be considered a quickstart.
There were app directories named inboard/fastapi and inboard/starlette.
When running main.py files within the Docker containers, Python thought
app/fastapi was FastAPI and app/starlette was Starlette.

This commit will rename the directories to fastapibase and starlettebase
to avoid further conflicts.
5d92088

Hopefully this prevents me from copying __pycache__ directories.

https://docs.docker.com/engine/reference/builder/#dockerignore-file
Previously, the base main.py modules would always return f"Hello World,
from Uvicorn, Gunicorn, and Python {version}", even if Gunicorn wasn't
in use. This commit will update the base main.py modules to return
"Uvicorn" if Gunicorn isn't in use, or "Uvicorn, Gunicorn," if Gunicorn.
@br3ndonland br3ndonland merged commit 3e844d1 into develop Aug 15, 2020
@br3ndonland br3ndonland deleted the br3ndonland/config branch August 15, 2020 21:31
br3ndonland added a commit that referenced this pull request Apr 4, 2021
#2
#3
ff9155a

The core logic for running the Uvicorn and Gunicorn+Uvicorn servers is
located in start.py. The `start.start_server()` method is what actually
starts the servers. Uvicorn and Gunicorn are configured differently, so
and the `start.start_server()` method ended up getting quite complex in
order to handle these differences.

This commit will refactor the configuration options passed to Gunicorn
and Uvicorn into separate functions. The result is a start script with
the same programming API and almost exactly the same line length, but
improved readability and separation of concerns.

- Refactor Gunicorn and Uvicorn options into separate functions,
  `start.set_gunicorn_options()` and `start.set_uvicorn_options()`.
- Remove `start.set_conf_path()`: this function was written in a general
  way to find either Gunicorn or logging configuration files. Starting
  with ff9155a, it became used only for Gunicorn configuration files.
  Now that the configuration options for Gunicorn are in a separate
  function, the logic from `set_conf_path()` can be moved there.
- Simplify logger type annotations: simply `import logging` and annotate
  functions with `logging.Logger` instead of having a separate import
  for `from logging import Logger`.
- Reorganize test_start.py to more clearly reflect order of start.py
br3ndonland added a commit that referenced this pull request Mar 6, 2022
#2

inboard optionally runs a pre-start script before starting the server.
The path to a pre-start script can be specified with the environment
variable `PRE_START_PATH`. If the environment variable is set to a
nonzero value, inboard will run the script at the provided path, using
the `subprocess` standard library package.

Previously, if the script exited with an error, inboard would continue
starting the server. However, it may be preferable to stop the server
if the pre-start script fails. This commit will update the subprocess
call to include `check=True`. If the pre-start script exits with an
error, inboard will not start the server.

The behavior of successful pre-start script runs will not change.
However, failed pre-start script runs will now exit with error codes,
and prevent the server from starting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants