Files
plantillas-proyectos/backend/main.py
acazares 2a10d7d267 feat: Add frontend and backend initialization scripts, implement Keycloak and PostgreSQL setup
- Implemented SvelteKit frontend with authentication callback handling.
- Created demo routes and paraglide localization functionality.
- Added health check and entrypoint scripts for backend services.
- Established PostgreSQL and Keycloak initialization scripts with health checks.
- Introduced models for database schema using SQLAlchemy.
- Configured Vite and SvelteKit for development and testing environments.
- Added health check script to verify service statuses and resource usage.
- Created Docker entrypoint scripts for seamless service startup.
2025-10-19 00:14:06 -05:00

91 lines
2.2 KiB
Python

"""
Anexo76 - Aplicación SaaS para gestión de comercio exterior
Backend API con FastAPI + Keycloak + SQLAlchemy
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import logging
from core.config import settings
from core.database import init_db
from core.middleware import (
TenantMiddleware,
LicenseValidationMiddleware,
RequestLoggingMiddleware
)
from api.v1.router import router as api_v1_router
# 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__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifecycle events"""
# Startup
logger.info("Starting Anexo76 API...")
try:
init_db()
logger.info("Database initialized successfully")
except Exception as e:
logger.error(f"Error initializing database: {str(e)}")
yield
# Shutdown
logger.info("Shutting down Anexo76 API...")
# 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, 31 y 22 del SAT",
lifespan=lifespan,
docs_url="/docs" if settings.DEBUG else None,
redoc_url="/redoc" if settings.DEBUG else None
)
# Configurar CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins_list,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Agregar middlewares personalizados
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("/")
async def root():
"""Root endpoint"""
return {
"name": "Anexo76 API",
"version": settings.APP_VERSION,
"status": "running",
"docs": "/docs" if settings.DEBUG else "disabled in production"
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"environment": settings.ENVIRONMENT
}