Files
plantillas-proyectos/backend/main.py
acazares b68c4316ff Refactor backend and frontend code for improved structure and functionality
- Rearranged imports in multiple files for consistency and clarity.
- Updated logging middleware to exclude specific paths from logging.
- Enhanced security module by cleaning up token handling and improving tenant validation.
- Added tenant and company scoped mixins for better database model management.
- Implemented generic CRUD routes for tenant-scoped resources.
- Improved error handling and response management in API routes.
- Cleaned up login and logout processes to ensure proper session management.
- Introduced mechanisms to clear local storage and cookies on tenant change.
- Enhanced company store to detect tenant changes and clear data accordingly.
- Added new DTO mixins for currency and value affect flags.
2025-11-11 17:20:47 -06:00

82 lines
2.1 KiB
Python

"""
Anexo76 - Aplicación SaaS para gestión de comercio exterior
Backend API con FastAPI + Keycloak + SQLAlchemy
"""
import logging
from api.v1.router import router as api_v1_router
from core.config import settings
from core.database import init_db
from core.middleware import (
LicenseValidationMiddleware,
RequestLoggingMiddleware,
TenantMiddleware,
)
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# Configurar logging
logging.basicConfig(
level=logging.INFO if not settings.DEBUG else logging.DEBUG,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
# Crear aplicación FastAPI
app = FastAPI(
title="Anexo76 API",
version=settings.APP_VERSION,
description="Aplicación SaaS para gestión de comercio exterior conforme a Anexos 24, 30 y 22 del SAT",
docs_url="/api/docs" if settings.DEBUG else None,
redoc_url="/api/redoc" if settings.DEBUG else None,
openapi_url="/api/openapi.json" if settings.DEBUG else None,
)
# Inicializar la base de datos
@app.on_event("startup")
async def on_startup():
"""Evento de inicio de la aplicación"""
logger.info("Iniciando la aplicación Anexo76...")
init_db()
logger.info("Base de datos inicializada correctamente.")
# Configurar CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins_list,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Agregar middlewares personalizados
if settings.DEBUG:
app.add_middleware(RequestLoggingMiddleware)
app.add_middleware(LicenseValidationMiddleware)
app.add_middleware(TenantMiddleware)
# Registrar routers
app.include_router(api_v1_router, prefix="/api/v1")
@app.get("/api/")
async def root():
"""Root endpoint"""
return {
"name": "Anexo76 API",
"version": settings.APP_VERSION,
"status": "running",
"docs": "/api/docs" if settings.DEBUG else "disabled in production",
}
@app.get("/api/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "environment": settings.ENVIRONMENT}