3.6 KiB
3.6 KiB
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
- FreeleapsAuthMiddleware: API Key validation and path skipping
- 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:
# 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:
{
"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:
[
{
"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)
# 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
# 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: