We will construct API with FastAPI and MongoDB
Create project com poetry:
poetry init
If you are using zsh do:
poetry add fastapi\[all\]
from bash:
poetry add fastapi[all]
poetry add dynaconf loguru requests
poetry add pymongo
Problems: if you installed uvicorn or jinja2 before fastapi[all] you should remove and then install fastapi.
To see instaled packages type: poetry show
Initialize Dynacon on your project.
poetry shell
dynaconf init -f toml
Configuring your Dynaconf environment
------------------------------------------
π The file `config.py` was generated.
on your code now use `from config import settings`.
(you must have `config` importable in your PYTHONPATH).
ποΈ settings.toml created to hold your settings.
π .secrets.toml created to hold your secrets.
π the .secrets.toml is also included in `.gitignore`
beware to not push your secrets to a public repo
or use dynaconf builtin support for Vault Servers.
π Dynaconf is configured! read more on https://dynaconf.com
Use `dynaconf -i config.settings list` to see your settings
This will create .secrets.toml
config.py
and settings.toml
Use dynaconf -i config.settings list
to see your settings:
DEFAULT<dict> {'APP_DESCRIPTION': 'API with MongoDB! π§π·',
'APP_NAME': 'API FastAPI! π',
'APP_VERSAO': '1.0.0',
'CURRENT_ENV': 'main',
'DEBUG': False,
'DIR_INPUT': 'Entrada',
'DIR_OUTPUT': 'Saida',
'HOME_DIR': '/home/mpi',
'URL_CHECK_PUBLIC_IP': 'https://ifconfig.me'}
DEVELOPMENT<dict> {'RUN_S00': False}
PRODUCTION<dict> {'RUN_S00': True}
Fill options in .secrets.toml
:
[default]
MONGODB_URI="@jinja mongodb://{{this.MONGO_ROOT_USER}}:{{this.MONGO_ROOT_PASS}}@{{this.MONGO_HOST}}:{{this.MONGO_PORT}}/{{this.MONGO_DB}}?authSource=admin"
# Change development or production on .env
[development]
aws_access_key_id = ''
aws_secret_access_key = ''
telegram_token = ""
telegram_chat_id = 0
SMTP_HOST = ""
SMTP_PORT = "587"
SMTP_USER = ""
SMTP_PASSWORD = ""
SMTP_MSG_FROM = ''
MONGO_ROOT_USER="admin"
MONGO_ROOT_PASS="changeme"
MONGO_HOST="localhost"
MONGO_PORT=27017
MONGO_DB="dev-db"
# RESULT MONGODB_URI = "mongodb://admin:changeme@localhost:27017/dev-db?authSource=admin"
# Change development or production on .env
[production]
aws_access_key_id = ''
aws_secret_access_key = ''
telegram_token = ""
telegram_chat_id = 0
SMTP_HOST = ""
SMTP_PORT = "587"
SMTP_USER = ""
SMTP_PASSWORD = ""
SMTP_MSG_FROM = ''
MONGO_ROOT_USER="admin_prod"
MONGO_ROOT_PASS="changeme"
MONGO_HOST="localhost"
MONGO_PORT=27017
MONGO_DB="dev-db"
# RESULT MONGODB_URI = "mongodb://admin_prod:changeme@localhost:27017/prod-db?authSource=admin"
Atention: Don't forget to add .secrets.toml on .gitignore.
https://towardsdatascience.com/the-nice-way-to-use-docker-with-vscode-f475c49aab1b
Ctrl + Shift + P
Create requirements.txt from poetry:
poetry export -f requirements.txt -o requirements.txt --without-hashes
docker build --rm --pull -f "/home/mpi/github.com/fastapi-mongodb/Dockerfile" --label "com.microsoft.created-by=visual-studio-code" -t "fastapimongodb:latest"
docker run --rm -d -p 8000:8000 fastapimongodb:latest
Create .env
file:
export ENV_FOR_DYNACONF=development # development ou production
export DYANCONF_CONFIG_BASICAUTH_USERNAME="mongo_user"
export DYANCONF_CONFIG_BASICAUTH_PASSWORD="pass123"
export DYANCONF_CONFIG_MONGODB_PORT=27017
export DYANCONF_CONFIG_MONGODB_ADMINUSERNAME="root"
export DYANCONF_CONFIG_MONGODB_ADMINPASSWORD="Mongodb2021"
In your real project put this values on .secrets.toml
Start Docker MongoDb and MongoExpress:
docker-compose -f docker-compose-mongo.yml up -d
You can access at http://localhost:8081
mongo-express.
Put your DYANCONF_CONFIG_BASICAUTH_USERNAME and DYANCONF_CONFIG_BASICAUTH_PASSWORD.
docker exec -it mongo /bin/bash
https://medium.com/fastapi-tutorials/integrating-fastapi-and-mongodb-8ef4f2ca68ad
https://towardsdatascience.com/the-nice-way-to-use-docker-with-vscode-f475c49aab1b
https://www.mongodb.com/developer/quickstart/python-quickstart-fastapi/
https://testdriven.io/blog/fastapi-mongo/
https://medium.com/rahasak/enable-mongodb-authentication-with-docker-1b9f7d405a94
https://medium.com/1mgofficial/how-to-override-uvicorn-logger-in-fastapi-using-loguru-124133cdcd4e
Tips: https://gdevops.gitlab.io/tuto_git/tools/pre-commit/pre-commit.html
https://www.architecture-performance.fr/ap_blog/some-pre-commit-git-hooks-for-python/
https://towardsdatascience.com/pre-commit-hooks-you-must-know-ff247f5feb7e