#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
中间件模块初始化文件
提供统一的中间件注册方法
"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from middlewares.request_logger import register_middleware as register_request_logger
from middlewares.error_handlers import register_exception_handlers
from config.settings import settings

def register_middlewares(app: FastAPI):
    """
    注册所有中间件到FastAPI应用
    
    Args:
        app: FastAPI应用实例
    """
    # 添加CORS中间件
    app.add_middleware(
        CORSMiddleware,
        allow_origins=settings.CORS_ORIGINS,
        allow_credentials=settings.CORS_ALLOW_CREDENTIALS,
        allow_methods=settings.CORS_ALLOW_METHODS,
        allow_headers=settings.CORS_ALLOW_HEADERS,
    )
    
    # 注册请求日志中间件
    register_request_logger(app)
    
    # 注册异常处理器
    register_exception_handlers(app) 