App Backend

Template for fastapi backend. This project has been configured using poetry.

Poetry Installation

Install poetry using the following command:

pip install poetry

Verify installation using the following command:

poetry --version

Project Installation

The Project working directory is the src directory. The root folder only contains poetry and docker config files. Hence, all the alembic and FastAPI commands can be run only from the working directory

Add a .env file inside env_config the working directory (you can find an example at .env.example)

Install the project dependencies using the following command:

poetry install

this command automatically creates a virtual environment and then installs the dependencies

Poetry virtual environment can be activated using

poetry run activate

or alternatively use source $(poetry env info --path)/bin/activate

and run:

python main.py

A local postgres database has to be set up.

If your IDE wants a virtual environment configured, run poetry env info and copy the path of the virtual environment and paste it into your IDE's configuration.

NOTE: If you are facing ModuleNotFoundError: No module named 'src' set the env variable PYTHONPATH using export PYTHONPATH="${PYTHONPATH}:/path/to/approot" and you can use set PYTHONPATH="${PYTHONPATH}:/path/to/approot" on a windows machine

Alternatively

docker-compose.yaml is configured with a persistent database volume which is retained in your system unless cleared explicitly and the server and local database can be started using the following command:

poetry run local

or alternatively docker-compose up -d

The database migrations has to be applied by running poetry run migrate. If you are using docker, run this command in app-web-server container

The API documentation can be accessed here

Packages

Few packages have already been added to the project.

You can add new packages using the following command

poetry add {package-name}

If you wish to add a package as a dev dependency. Use the following command

poetry add --group dev {package-name}

Database migrations using alembic

To generate migration files run the following command

poetry run make_migrations

alternatively, you can use alembic revision --autogenerate -m "migration message"

After generating the migration files, run the following command to migrate models

poetry run migrate

or alternatively alembic upgrade head

Note - Alembic is not 100% accurate always so check migration file manually and make corrections if needed

Refer alembic documentation here

Versioning

You can use the poetry version command to automatically bump the version of your project. Here's an example of how to use it to bump the patch version:

poetry version patch

This will update the version number in your pyproject.toml file and create a new commit with the updated version number.

You can also use minor or major instead of patch to bump the minor or major version, respectively.

poetry version patch
git add [files]
git commit -m "commit message"
git tag $(poetry version --short)

This will create a new commit with the updated version number and a new git tag with the same version number.

Testing

Testing has been set up using httpx and pytest-asyncio. Test can be found under tests package

Testing will be done on a test database which needs to be configured on the same host. The name of the database has to be added to .env as POSTGRES_TEST_DB

Run the following command to run all the tests

poetry run tests

or alternatively run pytest {file_name} to run particular test module

Test fixtures can be configured in tests/conftest.py

NOTE: All the test files must be of the format test_*.py and can use the test_client found in test_main.py

Celery - Redis

Celery worker and a redis server has been set up in docker-compose.yaml

Follow this blog to setup a redis server locally

Celery worker can be created using the following command:

celery -A main.celery worker -B -l info

Celery tasks can be configured but the path to these tasks have to be added in celery.conf.imports in src/main.py. And once new tasks are added. The celery workers have to be restarted.

The tasks can be started by using .delay() method.

Code Formatting

black python formatter added and project is using default configurations.

To format all the files in the project, you can run:

poetry run lint

or alternatively, black {source_file_or_directory}

Make sure to run the formatter before committing the files. You can configure your IDE to auto format everytime you save your file.

Debugging

Python - Ice Cream has been installed in this project as a dev dependency. You can use the ic() to debug you code.

For more info visit