This project was generated using fastapi_template.
This project uses poetry. It's a modern dependency management tool.
To run the project use this set of commands:
poetry install
poetry run python -m content_discovery
This will start the server on the configured host.
You can find swagger documentation at /api/docs
.
You can read more about poetry here: https://python-poetry.org/
You can start the project with docker using this command:
docker compose -f deploy/docker-compose.yml --project-directory . up --build
If you want to develop in docker with autoreload add -f deploy/docker-compose.dev.yml
to your docker command.
Like this:
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up --build
This command exposes the web application on port 9000, mounts current directory and enables autoreload.
But you have to rebuild image every time you modify poetry.lock
or pyproject.toml
with this command:
docker compose -f deploy/docker-compose.yml --project-directory . build
$ tree "content_discovery"
content_discovery
├── conftest.py # Fixtures for all tests.
├── db # module contains db configurations
│ ├── dao # Data Access Objects. Contains different classes to interact with database.
│ └── models # Package contains different models for ORMs.
├── __main__.py # Startup script. Starts uvicorn.
├── services # Package for different external services such as rabbit or redis etc.
├── settings.py # Main configuration settings for project.
├── static # Static content.
├── tests # Tests for project.
└── web # Package contains web server. Handlers, startup config.
├── api # Package with all handlers.
│ └── router.py # Main router.
├── application.py # FastAPI application configuration.
└── lifetime.py # Contains actions to perform on startup and shutdown.
This application can be configured with environment variables.
This application can be configured with environment variables.
cp .env_template .env
CREATE NETWORK BTW MICROSERVICES
docker network create -d bridge microservices
All environment variables should start with "CONTENT_DISCOVERY_" prefix.
For example if you see in your "content_discovery/settings.py" a variable named like
random_parameter
, you should provide the "CONTENT_DISCOVERY_RANDOM_PARAMETER"
variable to configure the value. This behaviour can be changed by overriding env_prefix
property
in content_discovery.settings.Settings.Config
.
An example of .env file:
CONTENT_DISCOVERY_RELOAD="True"
CONTENT_DISCOVERY_PORT="9000"
CONTENT_DISCOVERY_ENVIRONMENT="dev"
You can read more about BaseSettings class here: https://pydantic-docs.helpmanual.io/usage/settings/
To install pre-commit simply run inside the shell:
pre-commit install
pre-commit is very useful to check your code before publishing it. It's configured using .pre-commit-config.yaml file.
By default it runs:
- black (formats your code);
- mypy (validates types);
- isort (sorts imports in all files);
- flake8 (spots possible bugs);
You can read more about pre-commit here: https://pre-commit.com/
To run your app in kubernetes just run:
kubectl apply -f deploy/kube
It will create needed components.
If you haven't pushed to docker registry yet, you can build image locally.
docker compose -f deploy/docker-compose.yml --project-directory . build
docker save --output content_discovery.tar content_discovery:latest
If you want to migrate your database, you should run following commands:
# To run all migrations until the migration with revision_id.
alembic upgrade "<revision_id>"
# To perform all pending migrations.
alembic upgrade "head"
If you want to revert migrations, you should run:
# revert all migrations up to: revision_id.
alembic downgrade <revision_id>
# Revert everything.
alembic downgrade base
To generate migrations you should run:
# For automatic change detection.
alembic revision --autogenerate
# For empty file generation.
alembic revision
If you want to run it in docker, simply run:
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . run --build --rm api pytest -vv .
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . down
For running tests on your local machine.
- you need to start a database.
I prefer doing it with docker:
docker run -p "5432:5432" -e "POSTGRES_PASSWORD=content_discovery" -e "POSTGRES_USER=content_discovery" -e "POSTGRES_DB=content_discovery" postgres:13.8-bullseye
- Run the pytest.
pytest -vv .