A simple note taking app built with FastAPI and Beanie ODM
This simple app provides a basic note API on top of a MongoDB store with the following features:
- Registration
- JWT auth login
- Note model CRUD
- List Note by Tag
- Mock logout
The libraries are used to build the features:
- FastAPI - Python async micro framework built on Starlette and PyDantic
- Beanie ODM - Async MongoDB object-document mapper built on PyDantic
The components below are applied in the application architectures:
- Python 3.9
- MongoDB Atlas
- Nginx
- Docker
- Minikube
- Setup MongoDB Atlas database
- Setup custom role with all collection actions on
notes
andusers
collection, then assign to anew database user. - Configure IP whitelisting at
Network Access
from left panel.
Create .env
in root directory with format below for running with Docker:
# APP Settings
HOST=<replace with host name, default 0.0.0.0 for running in local>
PORT=<replace with target port, default 8000 for running in local>
DEBUG_MODE=<indicate if the app running in development or debug mode, default True>
LOG_LEVEL=INFO
#Database Settings
DB_HOST=<replace with mongodb host name>
DB_USER=<replace with mongodb database username>
DB_PASSWORD=<replace with mongodb database password>
DB_NAME=<replace with mongodb database name>
DB_SOCKET_TIMEOUT=<input timeput in miliseconds>
DB_CONNECT_TIMEOUT=<input timeput in miliseconds>
# Auth
JWT_SECRET=<Replace with random hash values>
JWT_ALGO=HS256
JWT_EXPIRY=<input timeput in seconds, default to 600>
# REDIS
REDIS_HOST=<replace with redis host name or ip. Defaulto to redis if running in Docker>
REDIS_PORT=6379
REDIS_PASSWORD=<replace with redis password. The password will configure into redis server>
The app is built with container in mind.
To build and run the app, simpliy run command below from root directory:
docker-compose up --build -d
Once the containers are running, access app via http://localhost/docs
- Enable minikube-ingress
- Create a new namespace
sandbox
in minikube. - Create Kubernetes secret manifest to store sensitive credentials for running with
minikube
:
apiVersion: v1
kind: Secret
metadata:
name: note-secret
namespace: sandbox
type: Opaque
data:
DB_HOST: <replace with mongodb host name>
DB_USER: <replace with mongodb database username>
DB_PASSWORD: <replace with mongodb database password>
DB_NAME: <replace with mongodb database name>
JWT_SECRET: <Replace with random hash values>
- Run command
kubectl apply -f secret.yaml
to create secret/ - Run command
eval $(minikube docker-env)
to active minikube docker environment. - Build the image using
minikube image
to build and load the image locally:
minikube image build -t josecw/note-api note-api/
- Deploy manifests:
kubectl apply -f release/deployment.yaml
kubectl apply -f release/ingress.yaml
- Edit
/etc/hosts/
or host file to include localhost or 127.0.0.1 asmy-note-app.com
- Open another terminal and run
minikube tunnel
- Now test the ingress is working
curl my-note-app.com
should see{"message":"Note-App is live"}
- The app is officially running. Go to
http://my-note-app.com/docs
for documentation
As pipeline is not available in the repo, please follow the steps in Setup
to configure kubernetes deployment
Run the following command for test: (Setup python environment before run test)
cd note-api
pytest -c pytest.ini