Skip to content

FastAPI template for Spotifiuby - includes Docker, Datadog and Heroku configs

Notifications You must be signed in to change notification settings

taller2-grupo5-rostov-1c2022/fastapi-template

Repository files navigation

Pipeline codecov

FastAPI template

Installing the project

The only dependency required to use this template is poetry. The recommended method to install it is through pip.

$ pip3 install poetry
$ poetry config virtualenvs.in-project true

Remember to commit to the repo the poetry.lock file generated by poetry install.

Dependencies

The virtual environment is automatically created and activated via poetry.

$ cd to-project-path
$ poetry install

To make sure everything is set up correctly, run the following command which must show the virtual environment path:

$ poetry show -v

If still having problems, you can run source command to enable the virtual environment from the shell:

$ poetry show -v
Using virtualenv: /home/<User>/<path>/taller2/fastapi-template/.venv
$ source .venv/bin/activate

Adding new dependencies

Check the full poetry docs, but here goes a quick reminder,

poetry add <dependency> [--dev]

Style guide

This template follows PEP8.

For this purpose, we use:

  • black: an opinionated code formatting tool
  • flake8: a tool to enforce style guide
  • pylint: a source code, bug and quality checker

Linters

flake8 && pylint <module_name>

Formatter

black .

Running the server

  • Development: uvicorn src.main:app --reload
  • Production: uvicorn src.main:app
  • Production Container: docker-compose up -d

API Documentation

Documentation will be automatically generated at {app}/docs

Tests

We use the pytest framework to test. The easiest way to run tests is pytest. Remember to create functions with a name that starts with test_ (this is standard pytest conventions).

Github Actions

A few pipelines have been set to run on github actions to ensure code quality and deployment.

  • Run Linter
  • Run Tests
  • Upload Test Coverage
  • Deploy to Heroku using docker image

Upload Coverage to Codecov

The pipeline automatically generates a coverage report and uploads it to codecov

You'll need to set the following actions secrets:

  • CODECOV_TOKEN: Repo Token. Can be obtained on codecov when setting up or on settings

Heroku

You'll need to set the following actions secrets:

  • HEROKU_APP_NAME: App name
  • HEROKU_EMAIL: Account email
  • HEROKU_API_KEY: Account API key

Datadog

The heroku Dockerfile includes the DataDog agent. Create a new DataDog API Key from here. Also, you need to set the following config vars in Heroku (you can use Heroku CLI if you want):

DD_API_KEY=<api_key_from_datadog>
DD_DYNO_HOST=false
HEROKU_APP_NAME=<app_name>
DD_TAGS=service:<meaningful_tag_for_datadog>