A template to a fastapi'project with auth, middleware and dependency injection
To create a project, you need install cookicutter
pip install cookiecutter
Next, run the command
cookiecutter https://github.com/MarLops/template_fastapi_auth.git
The project offer two types of auth
- Basic
- JWT
The project using users.json as Users'database, but this can modify.
To add auth in some function/endpoint, you need add
user = Depends(get_current_user)
as one of argument. If user is not in database, the variable "user" will be None
To overrride the database, you need to acess main.py and complete the DatabaseUserPersonal class
After you complete, we will need change create_database_user in same file
def create_database_user(*arg,**kwargs):
DB_USER = DatabaseUserPersonal()
The arguments of create_database_user can be modify in app.py
There are tests
The project can providence a database
To overrride the Database, you need to acess main.py and complete the DB class
After you complete, you will need change create_database in same file
def create_database(*arg,**kwargs):
DB_app = DB(*arg,**kwargs)
The arguments of create_database can be modify in app.py
To add database in some function/endpoint, you need add
db = Depends(get_database)
as one of argument.
The test can be create in tests folders.
To create new endpoints, you can
- Create at app.py
- Create at {{cookiecutter.struct}}.py
- Create new file and mount in app.py
To overrride the Middleware, you need to acess main.py and complete the Middware_Personalite class
After you complete, you will need change create_middware in same file
def create_middware(*arg,**kwargs):
return Middware_Personalite()
The arguments of create_middware can be modify in app.py
The method middleware_before is call before the endpoint receive the request
The method middleware_after is call afther the endpoint receive the response
The project use configparse. To add new configuration, modify settings.ini file .