Skip to content

Mbaoma/python-rest-apis

Repository files navigation

Project Aim

To:

  • Build and deploy a container (Flask application) to an Azure Web App
  • Build a Flask project with a REST API
  • Handle HTTP requests with Connexion
  • Define API endpoints using the OpenAPI specification
  • Interact with my API to manage data
  • Build API documentation with Swagger UI and Connexion
  • Configure a SQLite database for my Flask project
  • Use SQLAlchemy to save Python objects to my database
  • Leverage the Marshmallow library to serialize data

Tutorial

The REST API

Provides access to a collection of people and to the individuals within that collection.

API Definition

Action HTTP Verb URL Path Description
Read GET /api/people Read information on people
Create POST /api/people Create a new person's data
Read GET /api/people/ Read information on a person
Update PUT /api/people/ Update a person's information
Delete DELETE /api/people/ Delete a person's inforrmation

Setting up CI/CD using GitHub Actions

  • There is a branch protection rule set for the main branch - you cannot push directly to the main branch.

  • In the CI pipeline, unless all tests run successfully, you cannot merge code into the main branch. Resources: Test Routes

Local setup

  • Activate virtual environment and install dependencies
$ python -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
  • Run project
$ python run.py
  • Dockerizing the Application
$ docker image build -t <image-name> .
$ docker push <repo>/<image-name> 
$ docker run -p port:port -d <image-name>
  • To pull the container from Dockerhub
$ docker pull mbaoma/note-app
$ docker run -p port:port -d <image-name>

Deploying the Application to Azure App Service

Manual Deployment

  • Push your container to Azure Registry

  • Create a web app under the Azure app services panel.

image

  • Select what container registry your container is hosted in

image

  • Create the web app and wait a while before checking the logs, to determine the state of your container.

  • Navigate to your app's URL to view the eployed application

https://brandnew7.azurewebsites.net/ image

-- /api/ui (swagger UI) https://brandnew7.azurewebsites.net/api/ui/ image

Kubernetes config

You need to have minikube running

$ minikube start

OR

  • Enable kubernetes on your Docker Desktop before running the next couple of commands.

  • Feed the config files to kubectl

$ kubectl apply -f simplek8s/file-name.yaml
$ kubectl apply -f simplek8s/file-name.yaml
  • Print the status of all running pods and services
$ kubectl get pods
$ kubectl get services
  • Navigate to

Helpful commands

$ minikube service <service-name> --url
$ kubectl describe svc <service-name>