This template project can be used to jumpstart a REST API postgres backed project. It has stubs for testing and some usfull middleware.
This middleware adds a X-Context-Id header to the response, if such a header present in the request it will send it back if not a new one will be generated.
This id can be used a dependancy in the endpoints and used to achieve tracabilty in loggs.
i.e.
# Inject as Dependency
@app.post('/foo', tags=['Example'])
async def foo(context_id: str = Depends(ContextIdMiddleware.get_context)):
...# Use with logg
logger = logging.getLogger("api")
bt.add_task(logger.info, f'Informative log message.', extra={"context_id": context_id})This middleware adds a X-Process-Time header to the response indicating server execution time, usfull for performance visabilty.
DB_HOST: Host nameDB_PORT: PortDB_USER: Username nameDB_PASSWORD: PasswordDB_NAME: Database nameDB_NULL_POOL: If set will set to not use pool (Useful for unit testing to avoiddifferent looperror. )DB_ECHO: If set will echo db messages
API_ALGORITHM: JWT token algorithmdefault = HS256API_ACCESS_TOKEN_EXPIRE_MINUTES: Access JWT token expiration time (minutes)default = 15API_REFRESH_TOKEN_EXPIRE_DAYS: Refresh JWT token expiration time (minutes)default = 30API_SECRET_KEY: Access secret keydefault = randomAPI_REFRESH_SECRET_KEY: Refresh secret keydefault = random
Start all services
docker compose upStart DB only
docker compose up dbShutdown all services
docker compose down- Start DB service with compose
- Execute src/api.py in debug mode (the service will start on localhost:8001)
pylint $(git ls-files '*.py')python -m coverage run && python -m coverage html -i --skip-empty && open htmlcov/index.html