Files
bakery-ia/services/auth/app/main.py

120 lines
4.0 KiB
Python
Raw Normal View History

"""
2025-09-29 13:13:12 +02:00
Authentication Service Main Application
"""
2025-09-29 13:13:12 +02:00
from fastapi import FastAPI
from app.core.config import settings
2025-09-29 13:13:12 +02:00
from app.core.database import database_manager
2025-08-11 07:01:08 +02:00
from app.api import auth, users, onboarding
2025-07-17 21:25:27 +02:00
from app.services.messaging import setup_messaging, cleanup_messaging
2025-09-29 13:13:12 +02:00
from shared.service_base import StandardFastAPIService
2025-09-29 13:13:12 +02:00
class AuthService(StandardFastAPIService):
"""Authentication Service with standardized setup"""
2025-07-18 12:34:28 +02:00
2025-09-29 13:13:12 +02:00
def __init__(self):
# Define expected database tables for health checks
auth_expected_tables = [
'users', 'refresh_tokens', 'user_onboarding_progress',
'user_onboarding_summary', 'login_attempts'
]
2025-07-18 12:34:28 +02:00
2025-09-29 13:13:12 +02:00
# Define custom metrics for auth service
auth_custom_metrics = {
"registration_total": {
"type": "counter",
"description": "Total user registrations by status",
"labels": ["status"]
},
"login_success_total": {
"type": "counter",
"description": "Total successful user logins"
},
"login_failure_total": {
"type": "counter",
"description": "Total failed user logins by reason",
"labels": ["reason"]
},
"token_refresh_total": {
"type": "counter",
"description": "Total token refreshes by status",
"labels": ["status"]
},
"token_verify_total": {
"type": "counter",
"description": "Total token verifications by status",
"labels": ["status"]
},
"logout_total": {
"type": "counter",
"description": "Total user logouts by status",
"labels": ["status"]
},
"registration_duration_seconds": {
"type": "histogram",
"description": "Registration request duration"
},
"login_duration_seconds": {
"type": "histogram",
"description": "Login request duration"
},
"token_refresh_duration_seconds": {
"type": "histogram",
"description": "Token refresh duration"
}
}
2025-07-17 21:25:27 +02:00
2025-09-29 13:13:12 +02:00
super().__init__(
service_name="auth-service",
app_name="Authentication Service",
description="Handles user authentication and authorization for bakery forecasting platform",
version="1.0.0",
log_level=settings.LOG_LEVEL,
api_prefix="/api/v1",
database_manager=database_manager,
expected_tables=auth_expected_tables,
enable_messaging=True,
custom_metrics=auth_custom_metrics
2025-07-18 13:39:40 +02:00
)
2025-09-29 13:13:12 +02:00
async def _setup_messaging(self):
"""Setup messaging for auth service"""
await setup_messaging()
self.logger.info("Messaging setup complete")
async def _cleanup_messaging(self):
"""Cleanup messaging for auth service"""
2025-07-18 12:34:28 +02:00
await cleanup_messaging()
2025-07-17 21:25:27 +02:00
2025-09-29 13:13:12 +02:00
async def on_shutdown(self, app: FastAPI):
"""Custom shutdown logic for auth service"""
self.logger.info("Authentication Service shutdown complete")
2025-09-29 13:13:12 +02:00
def get_service_features(self):
"""Return auth-specific features"""
return [
"user_authentication",
"token_management",
"user_onboarding",
"role_based_access",
"messaging_integration"
]
2025-09-29 13:13:12 +02:00
# Create service instance
service = AuthService()
# Create FastAPI app with standardized setup
app = service.create_app(
docs_url="/docs",
redoc_url="/redoc"
)
# Setup standard endpoints
service.setup_standard_endpoints()
2025-07-17 21:25:27 +02:00
2025-09-29 13:13:12 +02:00
# Include routers with specific configurations
service.add_router(auth.router, prefix="/api/v1/auth", tags=["authentication"])
service.add_router(users.router, prefix="/api/v1/users", tags=["users"])
service.add_router(onboarding.router, prefix="/api/v1/users", tags=["onboarding"])