app.py wires up several pieces of middleware and lifecycle hooks at application creation time inside create_app().
CORS
Cross-Origin Resource Sharing is enabled globally viaflask-cors:
origins argument:
Database session teardown
SQLAlchemy uses a scoped session tied to the request context. After each request completes (with or without an exception), the session is removed to prevent connection leaks:db_session is the scoped_session created in startup/Alchemy.py. The remove() call closes the connection and returns it to the pool.
Error handler
APIException is registered as a Flask error handler so any raised APIException anywhere in the request lifecycle is automatically caught and returned as a JSON response:
APIException.
Graceful shutdown (SIGTERM)
The app registers a SIGTERM signal handler so it can finish in-flight requests before exiting — important when running inside Docker or Kubernetes:Adding custom middleware
To add your own middleware (e.g. request logging, rate limiting), use Flask’sbefore_request and after_request hooks inside create_app():