/fastapi-toy-server

This is a toy FastAPI server, demonstrating basic functionalities of it. Docker concepts are also practiced.

Primary LanguagePython

fastapi-toy-server

This is a toy FastAPI server, demonstrating basic functionalities of it. It's also related to a series of docker challenges in my company (Tosan), in which the project should be dockerized.

Python

Installation

  • Install requirements:
    1. Install "python3.7"
    2. Install "virtualenv"
      pip install virtualenv
    3. Create the virtual environment using following command:
      virtualenv .env
    4. Active virtualenv:
      • For windows:
        .env\Scripts\activate
    5. Now you can install libraries and dependencies listed in requirements file:
      pip install -r requirements.txt
    6. You can exit from virtual environment using following command:
      deactivate

Running the app

  • Set environment variables:
    • Set the path to config file:
      FAST_API_TOY_SERVER_ENV=path\to\config
    • Specify the port number (if you don't, it will default to 8080):
      PORT_NUMBER=5000
    • For development:
      FAST_API_ENV=development
    • For production:
      FAST_API_ENV=production
  • Run the app:
    python asgi.py

Running with Docker

    To install and run the app locally with docker, follow these steps:
  • First, build the docker image from dockerfile:
    docker build -t imagetag .
  • Then, create a container from the image and run it:
    docker run -d --name mycontainer -p 8080:8080 myimage
  • Ulternatively, you can just run the following command to automate the process:
    docker-compose up

Documentation

Navigate to the following links to browse the server documentation:
  • Redoc documentation:
    http://localhost:8080/redoc
  • Swagger UI documentation:
    http://localhost:8080/docs

Testing

  • To test the Server just run the following commands:
    • For unit tests:
      pytest -m unit
    • For end to end tests:
      pytest -m endtoend
    • To check code coverage just add the below line at the end of above commands:
      --cov=app\main
Note: In order to use python in virtualenv please run the following command:
python -m pytest