Python microservice template, inspired by my Kotlin based microservice template https://github.com/debuglevel/greeting-microservice, but with way less enthusiasm; and I'm not too sure that too much works here, because I just copied a bunch of stuff from my various other projects to have them at one place at least.
- Python environment
- Python 3.8
- venv
- Code quality
- Formatting
- black
- Type annotations
- mypy
- Testing
- pytest
- tox
- Formatting
- REST
- FastAPI
- Deployment
- Docker
- docker-compose
- IDE
- PyCharm Community
- Logging that does not completely suck
- would nice to be configurable via ENV
- Testing?
- tox (which seems to test against different Python versions. WTF compatibility? But it does not install those environments/python versions!)
- linting?
- Formatting?
python3 -m venv venvsource ./venv/bin/activateNote: If created on Windows, the directory is named Scripts because Python is weird.
.\venv\Scripts\Activate.ps1pip install -r requirements-dev.txtpip list --outdated shows outdated (transitive) dependencies.
black is used for formatting, because black does not ask about your opinion about how Python code should be formatted.
black .mypy checks the type annotations:
mypy app testspytestpytest-watch -c # -c clears terminal before pytest runstox- Open Swagger UI or ReDoc
- OpenAPI specs are available (as JSON) at http://localhost:8080/openapi.json
- Update
openapi.jsonviapython update-openapi.py
tox.ini is also configured to run some additional commands (like a Makefile):
black .for formattingpython update-openapi.pyto updateopenapi.yamlpytestfor testing
uvicorn app.rest.main:fastapi --port=8080 --reload --log-config=app/logging-config.yamlThis should be quite okay:
uvicorn app.rest.main:fastapi --port=8080 --log-config=app/logging-config.yamlBut some docs mention that gunicorn can be used as a manager.
docker compose up --build