API using FastAPI, Pydantic models, and MongoDB
OpenAPI documentation models for requests, responses and errors.
- Python >= 3.7
- Docker
- Requirements listed on requirements.txt
- MongoDB connection string
- Make (You can use a Mac, Linux or WSL in Windows) for build and deploy
this will run with reloading in uvicorn, don't worry the mounted volumes with compose are only in development!
make start
Visit http://localhost:5000/
make prod
It's important to set the following variables in order to connect to the production server. Why not a connection string? I find it easier to integrate the different parameters with greater control. It will be necessary to configure and deploy a CosmosDB instance with MongoDB API for instance.
// .env
API_PORT=
MONGO_URI=
MONGO_DB=
// if you are running the local instance of the client you will need this to add it to cors programatically, depending on the port
CLIENT_URI=http://localhost or CLIENT_URI=http://localhost:3000
Visit http://localhost:${API_PORT}
This will build and tag the production image locally for testing. If everything is working as it should then it can be published to Azure Container Registry
make build
This will run the production image locally and inject the necessary environment variables from .env to ensure its funcionality
make run
- Create a free Azure account at https://azure.microsoft.com/free
- Have Azure CLI installed in Linux environment either Mac or PC with WSL 2
- Login with Azure CLI
- Docker
Configure this app environment variables with your project-specific details for Make file:
// .env
REGISTRY_NAME=
RESOURCE_GROUP=
REGISTRY_IMAGE=
REGISTRY_TAG=
APP_SERVICE_PLAN=
APP_NAME=
If your infrastructure is already setup and you have this variables then, you can continue with the following steps
make acr-build
Since Azure Container Registry builds sometimes pop up some errors, it's also possible to build with Docker locally and publish to your registry using
make build
make publish
If you want to provision the whole infrastructure from here, it is possible using the Makefile, and the following environment variables are connected, which run all the necessary scripts, otherwise, you can set the environment variables from existing resources.
make appservice-create
make webapp-create
make webapp-appsettings
make app-permissions
make container-permission
make resource-identity
make acr-build
or make build
& make publish
make deploy
make container-settings
- FastAPI
- Uvicorn
- Gunicorn (For production)
- PyMongo
- Logging Middleware
- Testing
- Caching