- Updated `TimestampMixin` to use `server_default` for `created_at` and `updated_at` fields. - Modified various models in the `fa_classes`, `doc_types_dig`, `ports`, `units_of_measure`, `invoices`, `parts`, `trailers`, `licenses`, `tenants`, and `user_tenant` modules to set `server_default` for boolean fields and other relevant fields. - Adjusted the `trailer_types` model to change the schema from `a76` to `public`. - Implemented database migration execution during application startup in `main.py`. - Removed old Alembic migration execution logic from the entrypoint script. - Updated seed data for units of measure and removed unused seed files.
135 lines
4.0 KiB
Python
135 lines
4.0 KiB
Python
"""
|
|
Anexo76 - Aplicación SaaS para gestión de comercio exterior
|
|
Backend API con FastAPI + Keycloak + SQLAlchemy
|
|
"""
|
|
|
|
import logging
|
|
import subprocess
|
|
|
|
from api.v1.router import router as api_v1_router
|
|
from core.config import settings
|
|
from core.database import init_db
|
|
from core.error_handlers import register_exception_handlers
|
|
from core.middleware import (
|
|
LicenseValidationMiddleware,
|
|
RequestLoggingMiddleware,
|
|
TenantMiddleware,
|
|
)
|
|
from fastapi import FastAPI, Request, status, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from pathlib import Path
|
|
|
|
# Importar modelos para registrar con SQLAlchemy
|
|
from api.v1.modules.a76.items.models import Item
|
|
from api.v1.modules.a76.items.series.models import Serie
|
|
from api.v1.modules.a76.parts.models import Part
|
|
from api.v1.modules.a24.fa.fa_parts.models import FaPart
|
|
from api.v1.modules.a24.inv.inv_parts.models import InvPart
|
|
|
|
# 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,
|
|
)
|
|
|
|
# Registrar manejadores de excepciones
|
|
register_exception_handlers(app)
|
|
|
|
|
|
# Add validation error handler
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
|
logger.error(
|
|
f"Validation error for {request.method} {request.url.path}: {exc.errors()}"
|
|
)
|
|
logger.error(f"Request body: {await request.body()}")
|
|
return JSONResponse(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
content={"detail": exc.errors(), "body": exc.body},
|
|
)
|
|
|
|
|
|
# Add HTTP exception handler
|
|
@app.exception_handler(HTTPException)
|
|
async def http_exception_handler(request: Request, exc: HTTPException):
|
|
logger.error(
|
|
f"HTTP {exc.status_code} for {request.method} {request.url.path}: {exc.detail}"
|
|
)
|
|
return JSONResponse(
|
|
status_code=exc.status_code,
|
|
content={"detail": exc.detail},
|
|
)
|
|
|
|
|
|
def run_migrations():
|
|
subprocess.run(
|
|
["alembic", "upgrade", "head"],
|
|
check=True
|
|
)
|
|
|
|
# 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()
|
|
run_migrations()
|
|
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)
|
|
|
|
# Crear directorio de uploads si no existe y montar archivos estáticos
|
|
uploads_dir = Path("/app/uploads")
|
|
uploads_dir.mkdir(parents=True, exist_ok=True)
|
|
app.mount("/api/uploads", StaticFiles(directory=str(uploads_dir)), name="uploads")
|
|
|
|
# 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}
|