Primera version estable de microservicios
This commit is contained in:
78
main.py
Normal file
78
main.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import logging
|
||||
from fastapi import FastAPI
|
||||
from core.config import settings
|
||||
from api.api_v1.api import api_router
|
||||
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
# Configuración inicial del logging (debe estar al inicio del archivo)
|
||||
logging.config.dictConfig({
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"default": {
|
||||
"()": "uvicorn.logging.DefaultFormatter",
|
||||
"fmt": "%(levelprefix)s %(asctime)s | %(name)s | %(message)s",
|
||||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||||
"use_colors": True,
|
||||
},
|
||||
"access": {
|
||||
"()": "uvicorn.logging.AccessFormatter",
|
||||
"fmt": '%(levelprefix)s %(asctime)s | %(client_addr)s | "%(request_line)s" %(status_code)s',
|
||||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||||
},
|
||||
},
|
||||
"handlers": {
|
||||
"default": {
|
||||
"formatter": "default",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stdout",
|
||||
},
|
||||
"access": {
|
||||
"formatter": "access",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stdout",
|
||||
},
|
||||
},
|
||||
"loggers": {
|
||||
"": {"handlers": ["default"], "level": "DEBUG"},
|
||||
"uvicorn.error": {"level": "DEBUG"},
|
||||
"uvicorn.access": {"handlers": ["access"], "level": "DEBUG", "propagate": False},
|
||||
},
|
||||
})
|
||||
|
||||
def create_application() -> FastAPI:
|
||||
"""Función factory para crear la aplicación FastAPI"""
|
||||
application = FastAPI(
|
||||
title=settings.APP_NAME,
|
||||
description="EFC Microservice - Un microservicio profesional por AduanaSoft",
|
||||
version=settings.APP_VERSION,
|
||||
debug=settings.DEBUG,
|
||||
)
|
||||
|
||||
# Configuración adicional para loggers específicos
|
||||
app_logger = logging.getLogger("app")
|
||||
app_logger.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)
|
||||
|
||||
# Incluir el router principal de la API
|
||||
application.include_router(api_router, prefix="/api/v1")
|
||||
|
||||
return application
|
||||
|
||||
app = create_application()
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # O especifica ["http://localhost:5173"]
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=settings.HOST,
|
||||
port=settings.PORT,
|
||||
log_config=None, # Usamos nuestra configuración
|
||||
log_level="debug" if settings.DEBUG else "info",
|
||||
reload=settings.DEBUG
|
||||
)
|
||||
Reference in New Issue
Block a user