Refactor all main.py
This commit is contained in:
@@ -1,180 +1,120 @@
|
||||
"""
|
||||
Authentication Service Main Application - Fixed middleware issue
|
||||
Authentication Service Main Application
|
||||
"""
|
||||
|
||||
import structlog
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from app.core.config import settings
|
||||
from app.core.database import engine, create_tables
|
||||
from app.core.database import database_manager
|
||||
from app.api import auth, users, onboarding
|
||||
from app.services.messaging import setup_messaging, cleanup_messaging
|
||||
from shared.monitoring import setup_logging, HealthChecker
|
||||
from shared.monitoring.metrics import setup_metrics_early
|
||||
from shared.service_base import StandardFastAPIService
|
||||
|
||||
# Setup logging first
|
||||
setup_logging("auth-service", settings.LOG_LEVEL)
|
||||
logger = structlog.get_logger()
|
||||
|
||||
# Global variables for lifespan access
|
||||
metrics_collector = None
|
||||
health_checker = None
|
||||
class AuthService(StandardFastAPIService):
|
||||
"""Authentication Service with standardized setup"""
|
||||
|
||||
# Create FastAPI app FIRST
|
||||
app = FastAPI(
|
||||
title="Authentication Service",
|
||||
description="Handles user authentication and authorization for bakery forecasting platform",
|
||||
version="1.0.0",
|
||||
def __init__(self):
|
||||
# Define expected database tables for health checks
|
||||
auth_expected_tables = [
|
||||
'users', 'refresh_tokens', 'user_onboarding_progress',
|
||||
'user_onboarding_summary', 'login_attempts'
|
||||
]
|
||||
|
||||
# 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"
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
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"""
|
||||
await cleanup_messaging()
|
||||
|
||||
async def on_shutdown(self, app: FastAPI):
|
||||
"""Custom shutdown logic for auth service"""
|
||||
self.logger.info("Authentication Service shutdown complete")
|
||||
|
||||
def get_service_features(self):
|
||||
"""Return auth-specific features"""
|
||||
return [
|
||||
"user_authentication",
|
||||
"token_management",
|
||||
"user_onboarding",
|
||||
"role_based_access",
|
||||
"messaging_integration"
|
||||
]
|
||||
|
||||
|
||||
# Create service instance
|
||||
service = AuthService()
|
||||
|
||||
# Create FastAPI app with standardized setup
|
||||
app = service.create_app(
|
||||
docs_url="/docs",
|
||||
redoc_url="/redoc"
|
||||
)
|
||||
|
||||
# Setup metrics BEFORE any middleware and BEFORE lifespan
|
||||
# This must happen before the app starts
|
||||
metrics_collector = setup_metrics_early(app, "auth-service")
|
||||
# Setup standard endpoints
|
||||
service.setup_standard_endpoints()
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan events - NO MIDDLEWARE ADDED HERE"""
|
||||
global health_checker
|
||||
|
||||
# Startup
|
||||
logger.info("Starting Authentication Service...")
|
||||
|
||||
try:
|
||||
# Create database tables
|
||||
await create_tables()
|
||||
logger.info("Database tables created")
|
||||
|
||||
# Setup messaging
|
||||
await setup_messaging()
|
||||
logger.info("Messaging setup complete")
|
||||
|
||||
# Register custom metrics (metrics_collector already exists)
|
||||
metrics_collector.register_counter(
|
||||
"registration_total",
|
||||
"Total user registrations by status",
|
||||
labels=["status"] # Add this line
|
||||
)
|
||||
metrics_collector.register_counter(
|
||||
"login_success_total",
|
||||
"Total successful user logins"
|
||||
)
|
||||
metrics_collector.register_counter(
|
||||
"login_failure_total",
|
||||
"Total failed user logins by reason",
|
||||
labels=["reason"] # Add this line, based on auth.py usage
|
||||
)
|
||||
metrics_collector.register_counter(
|
||||
"token_refresh_total",
|
||||
"Total token refreshes by status",
|
||||
labels=["status"] # Add this line
|
||||
)
|
||||
metrics_collector.register_counter(
|
||||
"token_verify_total",
|
||||
"Total token verifications by status",
|
||||
labels=["status"] # Add this line
|
||||
)
|
||||
metrics_collector.register_counter(
|
||||
"logout_total",
|
||||
"Total user logouts by status",
|
||||
labels=["status"] # Add this line
|
||||
)
|
||||
metrics_collector.register_counter("errors_total", "Total errors", labels=["type"]) # Add this line
|
||||
metrics_collector.register_histogram("registration_duration_seconds", "Registration request duration")
|
||||
metrics_collector.register_histogram("login_duration_seconds", "Login request duration")
|
||||
metrics_collector.register_histogram("token_refresh_duration_seconds", "Token refresh duration")
|
||||
|
||||
# Setup health checker
|
||||
health_checker = HealthChecker("auth-service")
|
||||
|
||||
# Add database health check
|
||||
async def check_database():
|
||||
try:
|
||||
from app.core.database import get_db
|
||||
from sqlalchemy import text
|
||||
async for db in get_db():
|
||||
await db.execute(text("SELECT 1"))
|
||||
return True
|
||||
except Exception as e:
|
||||
return f"Database error: {e}"
|
||||
|
||||
health_checker.add_check("database", check_database, timeout=5.0, critical=True)
|
||||
|
||||
# Add messaging health check
|
||||
def check_messaging():
|
||||
try:
|
||||
# Add your messaging health check logic here
|
||||
return True
|
||||
except Exception as e:
|
||||
return f"Messaging error: {e}"
|
||||
|
||||
health_checker.add_check("messaging", check_messaging, timeout=3.0, critical=False)
|
||||
|
||||
# Store health checker in app state
|
||||
app.state.health_checker = health_checker
|
||||
|
||||
logger.info("Authentication Service started successfully")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to start Authentication Service: {e}")
|
||||
raise
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
logger.info("Shutting down Authentication Service...")
|
||||
try:
|
||||
await cleanup_messaging()
|
||||
await engine.dispose()
|
||||
logger.info("Authentication Service shutdown complete")
|
||||
except Exception as e:
|
||||
logger.error(f"Error during shutdown: {e}")
|
||||
|
||||
# Set lifespan AFTER metrics setup
|
||||
app.router.lifespan_context = lifespan
|
||||
|
||||
# CORS middleware (added after metrics setup)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # Configure properly for production
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# Include routers
|
||||
app.include_router(auth.router, prefix="/api/v1/auth", tags=["authentication"])
|
||||
app.include_router(users.router, prefix="/api/v1/users", tags=["users"])
|
||||
app.include_router(onboarding.router, prefix="/api/v1/users", tags=["onboarding"])
|
||||
|
||||
# Health check endpoint with comprehensive checks
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
"""Comprehensive health check endpoint"""
|
||||
if health_checker:
|
||||
return await health_checker.check_health()
|
||||
else:
|
||||
return {
|
||||
"service": "auth-service",
|
||||
"status": "healthy",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
|
||||
# Exception handlers
|
||||
@app.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
"""Global exception handler with metrics"""
|
||||
logger.error(f"Unhandled exception: {exc}", exc_info=True)
|
||||
|
||||
# Record error metric if available
|
||||
if metrics_collector:
|
||||
metrics_collector.increment_counter("errors_total", labels={"type": "unhandled"})
|
||||
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"detail": "Internal server error"}
|
||||
)
|
||||
# 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"])
|
||||
Reference in New Issue
Block a user