Skip to content

omidraha/FastAPI-Example

Repository files navigation

Intro

Sample FastAPI app to demostrate how to use FastAPI

About

This is a GitHub Score Application to rank repositories based on forks and stars.

More info (Issues):

Stack

In this application we used:

  • Python 3.9.7
  • FastAPI 0.68.1
  • Docker 20.10.8
  • Docker-compose 1.29.2
  • Docker-compose file version: 3.4
  • Gunicorn 20.1.0
  • uvicorn 0.15.0

Images

  • tiangolo/uvicorn-gunicorn-fastapi:python3.9

More info:

Backend topology

gunicorn -> gunicorn worker -> uvicorn -> main.py

More info:

Environments

Before run the app or run their tests, copy example.env as new .env file:

$ cp example.env .env

Set the value of GITHUB_ACCESS_TOKEN to your valid GITHUB access token.

Run application

Set Environments as previous section and then run the application:

$ docker-compose -p score build
$ docker-compose -p score up

For run in daemon, use -d if it's required:

$ docker-compose -p score up -d

Run tests

Set Environments as previous section and then:

$ docker-compose -p test run --rm web pytest

Health check

To see health check of service, use this command:

$ docker-compose -p score ps

Sample output:

score_web_1   /start.sh   Up (healthy)   0.0.0.0:80->80/tcp,:::80->80/tcp

To see logs of health check:

docker inspect --format='{{json .State.Health}}' score_web_1

Browse

http://127.0.0.1/docs/

http://127.0.0.1/score/

Curl

$ curl -X 'POST' 'http://127.0.0.1/score/' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"repo":"python/cpython"}'

TODO

  • Cache external GitHub service API response for some short period of the time for example for 1 hour in the Redis, And maybe provide a new cache option for example Cache-Control: no-cache header to the score API

  • Use async library for communicate with Github service API

Extra useful links