112 lines
2.7 KiB
Python
112 lines
2.7 KiB
Python
"""
|
|
Main FastAPI application for Diagram PH API
|
|
"""
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.middleware.gzip import GZipMiddleware
|
|
from app.config import settings
|
|
from app.api.v1.endpoints import refrigerants, properties, diagrams, cycles
|
|
import logging
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=getattr(logging, settings.LOG_LEVEL),
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create FastAPI application
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
description="API REST pour génération de diagrammes PH et calculs frigorifiques",
|
|
version=settings.VERSION,
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
)
|
|
|
|
# Add CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Add Gzip compression
|
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
|
|
|
# Include routers
|
|
app.include_router(
|
|
refrigerants.router,
|
|
prefix="/api/v1/refrigerants",
|
|
tags=["Refrigerants"]
|
|
)
|
|
|
|
app.include_router(
|
|
properties.router,
|
|
prefix="/api/v1/properties",
|
|
tags=["Properties"]
|
|
)
|
|
|
|
app.include_router(
|
|
diagrams.router,
|
|
prefix="/api/v1",
|
|
tags=["Diagrams"]
|
|
)
|
|
|
|
app.include_router(
|
|
cycles.router,
|
|
prefix="/api/v1",
|
|
tags=["Cycles"]
|
|
)
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
"""Actions to perform on application startup"""
|
|
logger.info(f"🚀 Starting {settings.APP_NAME} v{settings.VERSION}")
|
|
logger.info(f"📝 Environment: {settings.ENV}")
|
|
logger.info(f"📊 Log level: {settings.LOG_LEVEL}")
|
|
logger.info(f"🌐 CORS origins: {settings.CORS_ORIGINS}")
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown_event():
|
|
"""Actions to perform on application shutdown"""
|
|
logger.info(f"🛑 Shutting down {settings.APP_NAME}")
|
|
|
|
|
|
@app.get("/", tags=["Root"])
|
|
def root():
|
|
"""Root endpoint - API information"""
|
|
return {
|
|
"message": f"Welcome to {settings.APP_NAME}",
|
|
"version": settings.VERSION,
|
|
"status": "running",
|
|
"docs": "/docs",
|
|
"redoc": "/redoc",
|
|
"health": "/api/v1/health"
|
|
}
|
|
|
|
|
|
@app.get("/api/v1/health", tags=["Health"])
|
|
def health_check():
|
|
"""Health check endpoint"""
|
|
return {
|
|
"status": "healthy",
|
|
"service": settings.APP_NAME,
|
|
"version": settings.VERSION,
|
|
"environment": settings.ENV
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"app.main:app",
|
|
host=settings.HOST,
|
|
port=settings.PORT,
|
|
reload=(settings.ENV == "development"),
|
|
log_level=settings.LOG_LEVEL.lower()
|
|
) |