|
| 1 | +# Framework Middleware Configuration Guide |
| 2 | + |
| 3 | +This guide explains how to configure middleware in your framework using the provided `MiddlewareConfig` class or via keyword arguments. |
| 4 | + |
| 5 | +## Default Middleware |
| 6 | + |
| 7 | +The framework includes the following middleware by default: |
| 8 | + |
| 9 | +- **CORS**: Allows cross-origin requests (default: all origins, all methods, all headers). |
| 10 | +- **Logging**: Basic request/response logging (default: INFO level). |
| 11 | +- **Error Handling**: Basic error handling to prevent leaking internal details. |
| 12 | + |
| 13 | +Optional middleware you can enable/configure: |
| 14 | +- **Compression**: GZip compression for responses. |
| 15 | +- **Custom Middleware**: Add your own FastAPI-compatible middleware. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Configuration Methods |
| 20 | + |
| 21 | +You can configure middleware in two ways: |
| 22 | + |
| 23 | +### 1. Using `MiddlewareConfig` |
| 24 | + |
| 25 | +```python |
| 26 | +from pymcp.server import create_app, MiddlewareConfig |
| 27 | + |
| 28 | +config = MiddlewareConfig( |
| 29 | + cors={ |
| 30 | + "allow_origins": ["https://myapp.com"], |
| 31 | + "allow_methods": ["GET", "POST"], |
| 32 | + "allow_headers": ["*"], |
| 33 | + "allow_credentials": True, |
| 34 | + }, |
| 35 | + logging={ |
| 36 | + "level": "DEBUG", |
| 37 | + "format": "%(asctime)s %(levelname)s %(message)s", |
| 38 | + }, |
| 39 | + error_handling={ |
| 40 | + # Add custom error handlers if needed |
| 41 | + }, |
| 42 | + compression={ |
| 43 | + "enabled": True |
| 44 | + }, |
| 45 | + custom=[ |
| 46 | + # List of custom middleware classes |
| 47 | + ] |
| 48 | +) |
| 49 | + |
| 50 | +app = create_app(middleware_config=config) |
| 51 | +``` |
| 52 | + |
| 53 | +### 2. Using Keyword Arguments |
| 54 | + |
| 55 | +```python |
| 56 | +from pymcp.server import create_app |
| 57 | + |
| 58 | +app = create_app( |
| 59 | + cors={ |
| 60 | + "allow_origins": ["https://myapp.com"], |
| 61 | + "allow_methods": ["GET", "POST"], |
| 62 | + "allow_headers": ["*"], |
| 63 | + "allow_credentials": True, |
| 64 | + }, |
| 65 | + logging={ |
| 66 | + "level": "DEBUG", |
| 67 | + "format": "%(asctime)s %(levelname)s %(message)s", |
| 68 | + }, |
| 69 | + compression={ |
| 70 | + "enabled": True |
| 71 | + }, |
| 72 | + custom=[ |
| 73 | + # List of custom middleware classes |
| 74 | + ] |
| 75 | +) |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Middleware Options |
| 81 | + |
| 82 | +### CORS |
| 83 | +- `allow_origins`: List of allowed origins (default: `["*"]`) |
| 84 | +- `allow_methods`: List of allowed HTTP methods (default: `["*"]`) |
| 85 | +- `allow_headers`: List of allowed headers (default: `["*"]`) |
| 86 | +- `allow_credentials`: Allow credentials (default: `True`) |
| 87 | + |
| 88 | +### Logging |
| 89 | +- `level`: Log level (default: `"INFO"`) |
| 90 | +- `format`: Log format string |
| 91 | + |
| 92 | +### Error Handling |
| 93 | +- `custom_handler`: (Optional) Your custom error handler function |
| 94 | + |
| 95 | +### Compression |
| 96 | +- `enabled`: Enable GZip compression (default: `False`) |
| 97 | + |
| 98 | +### Custom Middleware |
| 99 | +- `custom`: List of FastAPI-compatible middleware classes to add |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Example: Adding Custom Middleware |
| 104 | + |
| 105 | +```python |
| 106 | +from starlette.middleware.base import BaseHTTPMiddleware |
| 107 | + |
| 108 | +class MyCustomMiddleware(BaseHTTPMiddleware): |
| 109 | + async def dispatch(self, request, call_next): |
| 110 | + # Custom logic here |
| 111 | + response = await call_next(request) |
| 112 | + return response |
| 113 | + |
| 114 | +config = MiddlewareConfig(custom=[MyCustomMiddleware]) |
| 115 | +app = create_app(middleware_config=config) |
| 116 | +``` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## Best Practices |
| 121 | +- Restrict CORS origins in production. |
| 122 | +- Use appropriate log levels for your environment. |
| 123 | +- Add custom error handlers for user-friendly error messages. |
| 124 | +- Enable compression for large responses or slow networks. |
| 125 | +- Add custom middleware for authentication, metrics, etc. |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## Recommended Usage: config.py |
| 130 | + |
| 131 | +For best practice, create a `config.py` file in your project root to define your middleware configuration. Then, import this config in your server entry point (e.g., `run_server.py`). |
| 132 | + |
| 133 | +### Example: config.py |
| 134 | + |
| 135 | +```python |
| 136 | + |
| 137 | +from pymcp.applications import create_app |
| 138 | +from pymcp.middleware import MiddlewareConfig |
| 139 | + |
| 140 | +middleware_config = MiddlewareConfig( |
| 141 | + cors={ |
| 142 | + "allow_origins": ["https://myapp.com"], |
| 143 | + "allow_methods": ["GET", "POST"], |
| 144 | + "allow_headers": ["*"], |
| 145 | + "allow_credentials": True, |
| 146 | + }, |
| 147 | + logging={ |
| 148 | + "level": "DEBUG", |
| 149 | + "format": "%(asctime)s %(levelname)s %(message)s", |
| 150 | + }, |
| 151 | + compression={ |
| 152 | + "enabled": True |
| 153 | + }, |
| 154 | + custom=[ |
| 155 | + # Add custom middleware classes here if needed |
| 156 | + ] |
| 157 | +) |
| 158 | +``` |
| 159 | + |
| 160 | +### Example: run_server.py |
| 161 | + |
| 162 | +```python |
| 163 | +from config import middleware_config |
| 164 | +from pymcp.server import create_app |
| 165 | + |
| 166 | +app = create_app(middleware_config=middleware_config) |
| 167 | +``` |
| 168 | + |
| 169 | +This approach keeps your configuration clean and separated from your application logic. |
| 170 | + |
| 171 | +For more details, see the framework documentation or contact the maintainers. |
0 commit comments