Skip to content

Chapter 10

Chapter 10

  • Hypercorn & Daphne & Gunicorn
  • Hypercorn: Uvicorn doesn't supprt HTTP/2, but Hypercorn supports HTTP/2 and HTTP/1 and WebSocket.
    • with the user of aioquic library, there is also an experimental support for HTTP/3
    • depends on trio
    • install: pipi install hypercorn
    • run app: hypercorn app:app --reload
  • Daphne: it was originally developed to power Django. It supports HTTP/2 and WebSocket protocols.
    • depends on async loop with a twisted library
    • run app: daphne -e ssl:8000:privateKey=privatekey.key:certKey=certificate.pem main:app
    • HTTP is automatically enabled.
  • Gunicorn

    • With uvicorn, the FastAPI app is run as a single process. In production, we need some replication of processes.
    • Gunicorn is another HTTP application server. Although it is a WSGI-compliant server, its process manager class allows the user to choose which worker process class to use.
    • Uvicorn has a Gunicorn-compatible worker class. Gunicorn's process manager delegates the requests to the worker class for conversion of incoming data to the ASGI standard and further consumption by the FastAPI app.
    • run app: gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
  • HTTP/2:

  • better efficiency over HTTP1 because of several improvements:

    • 1) it uses the binary transfer protocol
    • it employs a multiplexing technique to send multiple streams of data at once over a single TCP connection
    • if the client requests for an index.html, which internally uses an image, logo.png, and a stylesheet, style.css, all the three resources are sent over a single connection rather than three as in the case of HTTP/1
  • HTTPS:

  • one of the important considerations while deploying an API is to ensure that the server accepts only secure requests from the client. For HTTPS, the server needs to have a certificate. We can create a self-signed certificate using the RSA cryptography algo.

    $ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout privatekey.key -out certificate.pem
    
  • this will generate two files: privatekey.key and certificate.pem. We can use them as values for keyfile and certfile for Uvicorn and Hypercorn

    $ uvicorn app:app --ssl-keyfile=./privatekey.key --ssl-certfile=./certificate.pem --reload
    
    $ hypercorn --keyfile privatekey.key --certfile certificate.pem app:app
    

  • Render/Heroku/Cyclic

  • Render: render.com (has $0 tier)
    • environment: python3
    • build command: pip install -r requirements.txt
    • start command: uvicorn app:app -host 0.0.0.0 -port 10000
  • Heroku: heroku.com (NO $0 tier anymore)
  • Cyclic: cyclic.sh (No $0 tier)

  • Docker

  • building container iamges consisting all the dependencies of the app.
  • containers are lightweight as compared to virtual machines (VMs)
    |---container 1---|---container 2---|---container 3---|
    |    app A        |     app B       |     app C       |
    |    libs         |     libs        |     libs        |
    |-----------------|-----------------|-----------------|
    |             docker  engine layer                    |
    |-----------------------------------------------------|
    |             hosting operating system                |
    |-----------------------------------------------------|
    |             hosting machin hardware                 |
    |-----------------------------------------------------|
    
  • keep your main.py file along with the requirements.txt file in a folder. Alongside it, save the following text as Dockerfile (this files doesn't have any extension):
    FROM python:3.10
    copy ./app app
    RUN pip install -r app/requirements.txt
    CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
    
  • then we can build and run

    • build: docker build -t image1 . (. directory has the DockerFile)
    • run: docker run -d --name $namemycontainer -p 8000:8000 image1 (this will export the port)
  • GCP

  • A FastAPI can be hosted on the Google Cloud in two ways:
    • 1) using the Docker image and deploying it
    • 2) host the application code on GCP along with its dependencies and serve the application on the public IP address provided by Google Cloud
  • Details about approach (2)
    • GCP needs a text file named app.yaml
      runtime: python37
      entrypoint: gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app
      
    • After logging in GCP, go to console and create a new project
    • Running the Cloud Shell, clone the Github repository to GCP, then run the application the same way your run locally
    • Then we want to deploy the application to public access. in cloud shell
      $ gcloud config set project $YOUR_PROJECT_ID
      $ gcloud app create
      
    • The final step is to deploy: Google Cloud uses the YAML file available in the project directory.