This repository contains code for asynchronous example api using the Fast Api framework ,Uvicorn server and Postgres Database to perform crud operations on notes.
Read the full tutorial here
-
Clone this Repo
git clone (https://github.com/KenMwaura1/Fast-Api-example)
-
Cd into the Fast-Api folder
cd Fast-Api-example
-
Create a virtual environment
python3 -m venv venv
-
Activate virtualenv
source venv/bin/activate
For zsh users
source venv/bin/activate.zsh
For bash users
source venv/bin/activate.bash
For fish users
source venv/bin/activate.fish
-
Cd into the src folder
cd src
-
Install the required packages
python -m pip install -r requirements.txt
-
Start the app
python main.py
7b. Start the app using Uvicorn
uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8002
-
Ensure you have a Postgres Database running locally. Additionally create a
fast_api_dev
database with user**fast_api**
having required privileges. OR Change the DATABASE_URL variable in the .env file inside thenapp
folder to reflect database settings (user:password/db) -
Check the app on notes Open your browser and navigate to docs to view the swagger documentation for the api.
The is a simple Vue frontend using vite that was added. However it is an optional step in running the application.
Ensure you have Node.js installed. any version above 16 should work.
While inside the root folder Fast-Api-example
-
Cd into the
vue-client
folder.cd vue-client
-
Install the required dependencies. for NPM:
npm install
for Yarn:
yarn install
-
Start the Vue app for NPM:
npm run serve
for Yarn:
yarn serve
-
Open your browser and navigate to notes
-
Ensure Docker is installed.
-
Ensure Docker Compose is installed.
-
Clone this Repo
git clone (https://github.com/KenMwaura1/Fast-Api-example)
-
Change into the directory
cd Fast-Api-example
-
Use Docker-Compose to spin up containers
docker-compose up -d --build
-
If everything completes should be available on notes
-
Docs are generated on docs
Tests are available using pytest
Run them using pytest .
while in the root directory (/Fast-Api-example)
Open API Documentation is provided by Redoc
Contributions are welcome, please open an issue or submit a PR.
Github actions are used to run tests and build the docker image. The docker image is pushed to Docker Hub. Inorder to effectively use the actions you will need to add the following secrets to your repository settings. DOCKER_USERNAME
and DOCKER_PASSWORD
for the docker hub account.
This is to enable the docker login step in the workflow and push the image to the docker hub repository. Alternatively you can remove the step from the workflow by commenting it out.
It is also possible to use Github Packages to store the docker image. In this case you will need to add the following secrets to your repository settings. CR_PAT
and CR_USERNAME
for the github packages account. In our case the username is the github username. The CR_PAT
is a personal access token with the write:packages
scope. This is to enable the docker login step in the workflow and push the image to the github packages repository. Alternatively you can remove the step from the workflow by commenting it out.
The docker image is also tagged with the commit sha and pushed to the docker hub repository. This is to enable the image to be pulled by the docker-compose file in the root directory. The docker-compose file is used to spin up the containers locally. It is available on Github Packages as well.
The docker image is available on Docker Hub