Skip to main content
BackAnt’s 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 via flask-cors:
This allows all origins by default. To restrict to specific origins, pass an 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:
See Error Handling for how to raise and use 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’s before_request and after_request hooks inside create_app():