removing legacy code on path_rewrite_middleware
This commit is contained in:
@@ -200,23 +200,31 @@ Middleware for request/response logging and performance monitoring.
|
||||
|
||||
---
|
||||
|
||||
## Path Rewriting
|
||||
## Path-Based Routing Solution
|
||||
|
||||
### path_rewrite_middleware
|
||||
**Modern Approach: Double Router Mounting**
|
||||
|
||||
Middleware function that rewrites request paths for path-based vendor routing.
|
||||
Instead of using middleware to rewrite paths, the application registers shop routes **twice** with different prefixes:
|
||||
|
||||
**Purpose:**
|
||||
Allows `/vendors/VENDORCODE/shop/products` (path-based development mode) to be internally routed as `/shop/products` for proper FastAPI route matching.
|
||||
```python
|
||||
# In main.py
|
||||
app.include_router(shop_pages.router, prefix="/shop")
|
||||
app.include_router(shop_pages.router, prefix="/vendors/{vendor_code}/shop")
|
||||
```
|
||||
|
||||
**Execution Order:**
|
||||
Must run AFTER VendorContextMiddleware and BEFORE ContextDetectionMiddleware.
|
||||
**How It Works:**
|
||||
- **Subdomain/Custom Domain Mode**: Routes match `/shop/*` prefix
|
||||
- **Path-Based Development Mode**: Routes match `/vendors/{vendor_code}/shop/*` prefix
|
||||
- FastAPI handles routing naturally without path manipulation
|
||||
- Vendor code is available as a path parameter when needed
|
||||
|
||||
::: middleware.path_rewrite_middleware.path_rewrite_middleware
|
||||
options:
|
||||
show_source: true
|
||||
heading_level: 4
|
||||
show_root_heading: false
|
||||
**Benefits:**
|
||||
- ✅ No middleware complexity
|
||||
- ✅ Explicit route definitions
|
||||
- ✅ FastAPI native routing
|
||||
- ✅ Vendor code accessible via path parameter
|
||||
|
||||
**Note:** Previous implementations used `path_rewrite_middleware` to rewrite paths at runtime. This approach has been deprecated in favor of double mounting, which is simpler and more maintainable.
|
||||
|
||||
---
|
||||
|
||||
@@ -228,18 +236,19 @@ The middleware stack must be configured in the correct order for proper function
|
||||
graph TD
|
||||
A[Request] --> B[LoggingMiddleware]
|
||||
B --> C[VendorContextMiddleware]
|
||||
C --> D[PathRewriteMiddleware]
|
||||
D --> E[ContextMiddleware]
|
||||
E --> F[ThemeContextMiddleware]
|
||||
F --> G[Application Routes]
|
||||
G --> H[Response]
|
||||
C --> D[ContextMiddleware]
|
||||
D --> E[ThemeContextMiddleware]
|
||||
E --> F[Application Routes]
|
||||
F --> G[Response]
|
||||
```
|
||||
|
||||
**Critical Dependencies:**
|
||||
1. **VendorContextMiddleware** must run first to detect vendor
|
||||
2. **PathRewriteMiddleware** needs `clean_path` from VendorContext
|
||||
3. **ContextMiddleware** needs rewritten path
|
||||
4. **ThemeContextMiddleware** needs context type
|
||||
1. **LoggingMiddleware** runs first for request timing
|
||||
2. **VendorContextMiddleware** detects vendor and sets clean_path
|
||||
3. **ContextMiddleware** detects context type (API/Admin/Vendor/Shop)
|
||||
4. **ThemeContextMiddleware** loads vendor theme based on context
|
||||
|
||||
**Note:** Path-based routing (e.g., `/vendors/{code}/shop/*`) is handled by double router mounting in `main.py`, not by middleware.
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user