92 lines
2.6 KiB
Python
92 lines
2.6 KiB
Python
"""
|
|
FastAPI Main Application
|
|
"""
|
|
import logging
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.exceptions import RequestValidationError
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
from app.core.config import settings
|
|
from app.core.logging_config import logger
|
|
from app.api.v1 import api_router
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
"""Create application instance"""
|
|
logger.info("Creating FastAPI application instance...")
|
|
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
version=settings.APP_VERSION,
|
|
description="""
|
|
## Hospital Performance Management System API
|
|
|
|
### Function Modules
|
|
- **Basic Data Management**: Department, Staff, Indicators
|
|
- **Performance Assessment**: Assessment records, review workflow
|
|
- **Data Analysis Reports**: Department statistics, trends, rankings
|
|
- **Salary Calculation**: Performance-based payroll
|
|
|
|
### Tech Stack
|
|
- FastAPI + SQLAlchemy 2.0
|
|
- PostgreSQL
|
|
- Async IO support
|
|
""",
|
|
openapi_url=f"{settings.API_PREFIX}/openapi.json",
|
|
docs_url=f"{settings.API_PREFIX}/docs",
|
|
redoc_url=f"{settings.API_PREFIX}/redoc",
|
|
)
|
|
|
|
# CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Register router
|
|
app.include_router(api_router, prefix=settings.API_PREFIX)
|
|
|
|
# Health check
|
|
@app.get("/health", tags=["System"])
|
|
async def health_check():
|
|
return {"status": "healthy", "version": settings.APP_VERSION}
|
|
|
|
# HTTP exception handler
|
|
@app.exception_handler(StarletteHTTPException)
|
|
async def http_exception_handler(request, exc):
|
|
logger.warning(f"HTTP Exception: {request.method} {request.url} - {exc.status_code} - {exc.detail}")
|
|
raise exc
|
|
|
|
# Request validation exception handler
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_exception_handler(request, exc):
|
|
logger.error(f"Validation Error: {request.method} {request.url} - {exc}")
|
|
raise exc
|
|
|
|
# Global exception handler
|
|
@app.exception_handler(Exception)
|
|
async def global_exception_handler(request, exc):
|
|
logger.error(f"Global Exception: {request.method} {request.url} - {exc}", exc_info=True)
|
|
raise exc
|
|
|
|
logger.info("FastAPI application instance created successfully")
|
|
return app
|
|
|
|
|
|
app = create_app()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"app.main:app",
|
|
host="0.0.0.0",
|
|
port=8000,
|
|
reload=True,
|
|
log_level="info"
|
|
)
|