You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+85-39Lines changed: 85 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,56 +90,102 @@ if __name__ == "__main__":
90
90
asyncio.run(main())
91
91
```
92
92
93
-
## Middleware Example
93
+
## Middleware
94
94
95
-
OxAPY's middleware system is designed to be flexible and powerful. Middleware is applied to all routes within the same **scope**. You can use the `.scope()` method to create new scopes, allowing you to group routes with different middleware. This allows for building complex routing structures where different sets of middleware apply to different groups of routes.
95
+
OxAPY offers two paradigms for organizing middleware. You can use one or combine both.
96
96
97
-
### Best Practices
97
+
### 1. Sequence Paradigm (same router)
98
98
99
-
-**Order Matters**: Middleware is executed in the order it is defined within a scope.
100
-
-**Scoping**: Use `.scope()` to create logical separation between groups of routes and their middleware. For example, you can have one scope for public endpoints and another for authenticated endpoints.
101
-
-**Clarity**: Be mindful that middleware applies to all routes defined in the current scope, both before and after the middleware is added.
99
+
Middleware only applies to routes registered **after** it within the same router. Routes before it get no middleware.
102
100
103
101
```python
104
-
from oxapy import Status, Router, get, HttpServer
102
+
# Simple: one middleware layer
103
+
Router()
104
+
.route(get("/health", lambda_: "OK")) # no middleware
105
+
.middleware(auth)
106
+
.route(get("/dashboard", dashboard)) # auth only
107
+
.route(get("/account", account)) # auth only
108
+
```
105
109
106
-
deflog_middleware(request, next, **kwargs):
107
-
print(f"Request: {request.method}{request.uri}")
108
-
returnnext(request, **kwargs)
110
+
```python
111
+
# Multiple layers: each middleware applies to everything after it
112
+
Router()
113
+
.route(get("/health", lambda_: "OK")) # no middleware
0 commit comments