This opinionated project demonstrates how use Falcon and various Python libraries to build a scalable REST API for a movie recommendation website.
Use the best tool for the job at hand.
- Most of the CRUD logic is in controllers, but if we have to do perform multiple tasks for an endpoint, a process is kicked off.
- Took inspiration from Twelve-Factor App
- Development environment leverages Docker-Compose to replicate production environment
Makefile
provides common operations for development- pre-commit hooks identify comomn code review issues before submission
- CI pipeline is triggered on push to branch and PR creation
make up
- Create virtual environment on local machine,
pip install -r requirements_dev.txt
to install dependencies locally - Point IDE's
PYTHONPATH
to thepython
instance in the virtual environment from above to get autocomplete and other tooling working - Install
pre-commit
on your development machine pre-commit install
will run existing hook scripts (from.pre-commit-config.yaml
)
Server available at http://0.0.0.0:7000/
- Implemented in console in console only, TODO: add search endpoint
Movie.reindex()
Movie.search("top gun", page=1, per_page=5)
- Code Formatter: Black
- Logging: Standard Library
- Static Type Checker: mypy
- Updating Dependencies: PyUp
- redis + rq
- rq-scheduler to schedule jobs
- rq-dashboard for monitoring. Available at http://0.0.0.0:9181/
- JWT authentication via falcon-auth
- CI builds with drone
- apispec + falcon-apispec to generate OpenAPI (aka Swagger) specification
- Serving Redoc-styled docs at http://localhost:7000/swagger/redoc.html
- Using elasticsearch via elasticsearch-py
- Leveraging developer utilities from Kibana
- Follow pattern described in SQLAlchemy docs
- Load database (well, declarative base) into the request object
- Remove database from request before sending response
- Migrations with Alembic
- Attach py-spy to Python process to profile
- Connect to Gunicorn process to profile web application
USAGE:
py-spy --duration <duration> --pid <pid> --rate <rate>
- Bandit is a static analysis tool to find security issues
- 10 common security gotchas in Python and how to avoid them
- Open Web Application Security Project: 2017 Top 10
- Marshmallow to serialize objects into JSON (response) and deserialize JSON into object (request)
- webargs to parse requests arguments (query string)
toasted-marshmallow
has 10x performance, investigate