feat(guide): guide to use tenant middleware
This commit is contained in:
parent
9dc8811886
commit
065c082aa7
130
apps/notification/webapi/middleware/README.md
Normal file
130
apps/notification/webapi/middleware/README.md
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
# Notification Service Middleware Guide
|
||||||
|
|
||||||
|
This guide explains how to use and test the middleware system of the notification service.
|
||||||
|
|
||||||
|
## Middleware Architecture
|
||||||
|
|
||||||
|
### Middleware Execution Order
|
||||||
|
```
|
||||||
|
Client Request → FreeleapsAuthMiddleware → TenantDBConnectionMiddleware → Business Logic
|
||||||
|
```
|
||||||
|
|
||||||
|
1. **FreeleapsAuthMiddleware**: API Key validation and path skipping
|
||||||
|
2. **TenantDBConnectionMiddleware**: Tenant database switching (based on product_id)
|
||||||
|
|
||||||
|
## 1. Setup API Key
|
||||||
|
|
||||||
|
### 1.1 Register API Key via freeleaps-auth service
|
||||||
|
|
||||||
|
Ensure the freeleaps-auth service is running on port 9000, then execute the following commands:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Register API KEY for magicleaps tenant
|
||||||
|
curl -X POST "http://localhost:9000/api/v1/keys/register_api_key" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{
|
||||||
|
"tenant_name": "magicleaps",
|
||||||
|
"product_id": "68a3f19119cfaf36316f6d14",
|
||||||
|
"scopes": ["notify.send_notification"],
|
||||||
|
"expires_at": "2099-12-31T23:59:59Z"
|
||||||
|
}'
|
||||||
|
|
||||||
|
# Register API KEY for test-a tenant
|
||||||
|
curl -X POST "http://localhost:9000/api/v1/keys/register_api_key" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{
|
||||||
|
"tenant_name": "test-a",
|
||||||
|
"product_id": "68a3f19119cfaf36316f6d15",
|
||||||
|
"scopes": ["notify.send_notification"],
|
||||||
|
"expires_at": "2099-12-31T23:59:59Z"
|
||||||
|
}'
|
||||||
|
|
||||||
|
# Register API KEY for test-b tenant
|
||||||
|
curl -X POST "http://localhost:9000/api/v1/keys/register_api_key" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{
|
||||||
|
"tenant_name": "test-b",
|
||||||
|
"product_id": "68a3f19119cfaf36316f6d16",
|
||||||
|
"scopes": ["notify.send_notification"],
|
||||||
|
"expires_at": "2099-12-31T23:59:59Z"
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
### 1.2 Record the returned API KEY
|
||||||
|
|
||||||
|
Example successful response:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"data": {
|
||||||
|
"tenant_name": "magicleaps",
|
||||||
|
"product_id": "68a3f19119cfaf36316f6d14",
|
||||||
|
"api_key": {
|
||||||
|
"key_id": "ak_live_UkIcMxwBXIw",
|
||||||
|
"api_key": "ak_live_UkIcMxwBXIw.J7qWirjL0IJkmvqktjEh3ViveP8dgiturxyy0KJ5sKk",
|
||||||
|
"status": "active"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Configure Tenant Database
|
||||||
|
|
||||||
|
Create tenant documents in the `tenant_doc` collection of the main database:
|
||||||
|
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"tenant_name": "magicleaps",
|
||||||
|
"product_id": "68a3f19119cfaf36316f6d14",
|
||||||
|
"mongodb_uri": "mongodb://localhost:27017/interview",
|
||||||
|
"status": "active"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"tenant_name": "test-a",
|
||||||
|
"product_id": "68a3f19119cfaf36316f6d15",
|
||||||
|
"mongodb_uri": "mongodb://localhost:27017/test-a",
|
||||||
|
"status": "active"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"tenant_name": "test-b",
|
||||||
|
"product_id": "68a3f19119cfaf36316f6d16",
|
||||||
|
"mongodb_uri": "mongodb://localhost:27017/test-b",
|
||||||
|
"status": "active"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. Test Middleware Functionality
|
||||||
|
|
||||||
|
### 3.1 Test System Endpoints (Skip Validation)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Health check endpoints - should return 200, skip all validation
|
||||||
|
curl -v "http://localhost:8104/api/_/healthz"
|
||||||
|
curl -v "http://localhost:8104/api/_/readyz"
|
||||||
|
curl -v "http://localhost:8104/api/_/livez"
|
||||||
|
|
||||||
|
# Documentation and monitoring endpoints - should return 200, skip all validation
|
||||||
|
curl -v "http://localhost:8104/docs"
|
||||||
|
curl -v "http://localhost:8104/metrics"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.2 Test API Key Validation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# No API Key - should return 200 (compatibility mode)
|
||||||
|
curl -v "http://localhost:8104/api/notification/global_templates/list?region=1"
|
||||||
|
|
||||||
|
# Invalid API Key - should return 400/401
|
||||||
|
curl -v "http://localhost:8104/api/notification/global_templates/list?region=1" \
|
||||||
|
-H "X-API-KEY: invalid_key"
|
||||||
|
|
||||||
|
# Valid API Key - should return 200 and switch to tenant database
|
||||||
|
curl -v "http://localhost:8104/api/notification/global_templates/list?region=1" \
|
||||||
|
-H "X-API-KEY: ak_live_UkIcMxwBXIw.J7qWirjL0IJkmvqktjEh3ViveP8dgiturxyy0KJ5sKk"
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.3 Check Log Output
|
||||||
|
|
||||||
|
View logs in `/apps/notification/log/notification-activity.log`:
|
||||||
Loading…
Reference in New Issue
Block a user