70 lines
1.9 KiB
Python
70 lines
1.9 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from prometheus_fastapi_instrumentator import Instrumentator
|
|
from common.config.app_settings import site_settings
|
|
from loguru import logger
|
|
import os
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
"""
|
|
Create and configure the FastAPI application
|
|
"""
|
|
app = FastAPI(
|
|
title="Metrics Service API",
|
|
description="Metrics Service for Freeleaps Platform",
|
|
version="1.0.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc"
|
|
)
|
|
|
|
# Add CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Setup logging
|
|
setup_logging()
|
|
|
|
# Setup Prometheus metrics
|
|
Instrumentator().instrument(app).expose(app)
|
|
|
|
# Include routers
|
|
# from webapi.routes import health, api
|
|
# app.include_router(health.router, prefix="/health", tags=["health"])
|
|
# app.include_router(api.router, prefix="/api/metrics", tags=["metrics"])
|
|
# Note: Registration router is included in main.py
|
|
|
|
return app
|
|
|
|
|
|
def setup_logging():
|
|
"""
|
|
Setup logging configuration
|
|
"""
|
|
# Create log directory if it doesn't exist
|
|
log_dir = site_settings.LOG_BASE_PATH
|
|
os.makedirs(log_dir, exist_ok=True)
|
|
|
|
# Configure loguru
|
|
logger.add(
|
|
f"{log_dir}/{site_settings.BACKEND_LOG_FILE_NAME}.log",
|
|
rotation="1 day",
|
|
retention="30 days",
|
|
level="INFO",
|
|
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {name}:{function}:{line} | {message}"
|
|
)
|
|
|
|
logger.add(
|
|
f"{log_dir}/{site_settings.APPLICATION_ACTIVITY_LOG}.log",
|
|
rotation="1 day",
|
|
retention="30 days",
|
|
level="INFO",
|
|
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {name}:{function}:{line} | {message}",
|
|
filter=lambda record: record["level"].name == "INFO"
|
|
)
|