Oslo Middleware Library

8.0.0 · active · verified Thu Apr 16

Oslo.middleware is an OpenStack library that provides a collection of WSGI middleware components for web service development. It allows developers to inject functionality into WSGI pipelines to intercept and modify request/response flows, offering features like HTTP header manipulation, request body size limiting, CORS support, and error handling. It is an actively maintained project within the OpenStack ecosystem, with regular releases aligning with OpenStack development cycles.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to chain two `oslo_middleware` components, `RequestId` and `CatchErrors`, with a basic WSGI application. `RequestId` ensures a unique ID for each request, and `CatchErrors` provides high-level error handling, preventing raw tracebacks from being exposed to clients. This setup is typical in OpenStack services.

import webob.dec
import webob.exc
from oslo_middleware import catch_errors
from oslo_middleware import request_id

@webob.dec.wsgify
def simple_app(req):
    if req.path_info == '/hello':
        return f"Hello, Request ID: {req.environ.get('openstack.request_id', 'N/A')}"
    elif req.path_info == '/error':
        raise ValueError('Something went wrong!')
    return webob.exc.HTTPNotFound()

# Order matters: RequestId first to ensure ID is generated,
# then CatchErrors to handle subsequent exceptions.
app = request_id.RequestId(simple_app)
app = catch_errors.CatchErrors(app)

# To run this with a WSGI server (e.g., Gunicorn or uWSGI):
# Save as app.py and run 'gunicorn app:app'
# Test with curl:
# curl http://127.0.0.1:8000/hello
# curl http://127.0.0.1:8000/error

view raw JSON →