feat: add ProxyHeadersMiddleware for HTTPS redirects in production
This commit is contained in:
160
backend/main.py
160
backend/main.py
@@ -17,10 +17,12 @@ from core.middleware import (
|
||||
)
|
||||
from fastapi import FastAPI, Request, status, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.middleware.trustedhost import TrustedHostMiddleware
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pathlib import Path
|
||||
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
|
||||
|
||||
# Importar modelos para registrar con SQLAlchemy
|
||||
from api.v1.modules.a76.items.models import Item
|
||||
@@ -29,7 +31,9 @@ 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
|
||||
from api.v1.modules.a76.manifests.manifest.models import Manifest
|
||||
from api.v1.modules.a76.manifests.concept_manifestation.models import ConceptManifestation
|
||||
from api.v1.modules.a76.manifests.concept_manifestation.models import (
|
||||
ConceptManifestation,
|
||||
)
|
||||
from api.v1.modules.a76.manifests.value_manifestation.models import ValueManifestation
|
||||
|
||||
# Configurar logging
|
||||
@@ -94,6 +98,10 @@ async def on_startup():
|
||||
logger.info("Base de datos inicializada correctamente.")
|
||||
|
||||
|
||||
# Configurar middleware para headers de proxy (HTTPS en producción)
|
||||
# Esto asegura que los redirects 307 usen https:// en producción
|
||||
app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
|
||||
|
||||
# Configurar CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
@@ -112,6 +120,7 @@ app.add_middleware(TenantMiddleware)
|
||||
|
||||
# Middleware de Contexto de Usuario (Audit Log)
|
||||
from api.v1.modules.a76.audit_log.middleware import UserContextMiddleware
|
||||
|
||||
app.add_middleware(UserContextMiddleware)
|
||||
|
||||
# Importar modelos para Audit Log
|
||||
@@ -130,100 +139,121 @@ from api.v1.modules.a76.general_catalogs.company.models import Company
|
||||
from api.v1.modules.public.reference_data.countries.models import Country
|
||||
from api.v1.modules.public.reference_data.currency_types.models import CurrencyType
|
||||
from api.v1.modules.public.reference_data.customs_sections.models import CustomsSection
|
||||
from api.v1.modules.public.reference_data.customs_warehouses.models import CustomsWarehouse
|
||||
from api.v1.modules.public.reference_data.customs_warehouses.models import (
|
||||
CustomsWarehouse,
|
||||
)
|
||||
from api.v1.modules.public.reference_data.incoterms.models import Incoterm
|
||||
from api.v1.modules.public.reference_data.invoice_types.models import InvoiceType
|
||||
from api.v1.modules.public.reference_data.material_types.models import MaterialType
|
||||
from api.v1.modules.public.reference_data.payment_methods.models import PaymentMethod
|
||||
from api.v1.modules.public.reference_data.pedimento_codes.models import PedimentoCode
|
||||
from api.v1.modules.public.reference_data.pedimento_regimens.models import RegimenPedimento
|
||||
from api.v1.modules.public.reference_data.pedimento_regimens.models import (
|
||||
RegimenPedimento,
|
||||
)
|
||||
from api.v1.modules.public.reference_data.sectors.models import Sector
|
||||
from api.v1.modules.public.reference_data.states.models import State
|
||||
from api.v1.modules.public.reference_data.transport_modes.models import TransportMode
|
||||
from api.v1.modules.public.reference_data.transport_types.models import TransportType
|
||||
from api.v1.modules.public.reference_data.valuation_methods.models import ValuationMethod
|
||||
from api.v1.modules.public.reference_data.valuation_methods.models import (
|
||||
ValuationMethod,
|
||||
)
|
||||
from api.v1.modules.a76.general_catalogs.units_of_measure.models import UnitOfMeasure
|
||||
from api.v1.modules.a76.general_catalogs.exchange_rate.models import ExchangeRate
|
||||
from api.v1.modules.a76.general_catalogs.identifiers.models import Identifier
|
||||
from api.v1.modules.a76.classes.models import Class
|
||||
from api.v1.modules.a76.general_catalogs.classification_concepts.models import ClassificationConcept
|
||||
from api.v1.modules.a76.general_catalogs.classification_concepts.models import (
|
||||
ClassificationConcept,
|
||||
)
|
||||
from api.v1.modules.a76.general_catalogs.concepts.models import Concept
|
||||
from api.v1.modules.a76.general_catalogs.customs_broker_concepts.models import CustomsBrokerConcept
|
||||
from api.v1.modules.a76.general_catalogs.depreciation_catalog.models import DepreciationCatalog
|
||||
from api.v1.modules.a76.general_catalogs.customs_broker_concepts.models import (
|
||||
CustomsBrokerConcept,
|
||||
)
|
||||
from api.v1.modules.a76.general_catalogs.depreciation_catalog.models import (
|
||||
DepreciationCatalog,
|
||||
)
|
||||
from api.v1.modules.a76.general_catalogs.doda.models import Doda
|
||||
from api.v1.modules.a76.general_catalogs.electronic_notices.models import ElectronicNotice
|
||||
from api.v1.modules.a76.general_catalogs.electronic_notices.models import (
|
||||
ElectronicNotice,
|
||||
)
|
||||
from api.v1.modules.a76.general_catalogs.equivalencies.models import Equivalency
|
||||
from api.v1.modules.a76.general_catalogs.error_catalogs.models import ErrorCatalog
|
||||
from api.v1.modules.a76.general_catalogs.fda_catalog.models import FDACatalog
|
||||
from api.v1.modules.a76.general_catalogs.inpc.models import INPC
|
||||
from api.v1.modules.a76.general_catalogs.legends.models import Legend
|
||||
from api.v1.modules.a76.general_catalogs.multi_currency_types.models import MultiCurrencyType
|
||||
from api.v1.modules.a76.general_catalogs.multi_currency_types.models import (
|
||||
MultiCurrencyType,
|
||||
)
|
||||
from api.v1.modules.a76.general_catalogs.packages.models import Package
|
||||
from api.v1.modules.a76.general_catalogs.ports.models import Port
|
||||
from api.v1.modules.a76.general_catalogs.prevalidators.models import Prevalidator
|
||||
from api.v1.modules.a76.general_catalogs.seal.models import Seal
|
||||
from api.v1.modules.a76.general_catalogs.signatures.models import Signature
|
||||
from api.v1.modules.a76.general_catalogs.fractions.tariff_fractions.models import TariffFraction
|
||||
from api.v1.modules.a76.general_catalogs.fractions.tariff_fractions.models import (
|
||||
TariffFraction,
|
||||
)
|
||||
from api.v1.modules.a76.general_catalogs.unit_conversions.models import UnitConversion
|
||||
from api.v1.modules.a76.general_catalogs.fractions.us_tariff_fractions.models import USTariffFraction
|
||||
from api.v1.modules.a76.general_catalogs.fractions.us_tariff_fractions.models import (
|
||||
USTariffFraction,
|
||||
)
|
||||
|
||||
|
||||
# Registrar Listeners de Auditoría
|
||||
@app.on_event("startup")
|
||||
def register_audit():
|
||||
register_audit_listeners([
|
||||
# Core Transactions
|
||||
Pedimentos,
|
||||
InvoiceHeader,
|
||||
InvoiceSalesDetails,
|
||||
Item,
|
||||
|
||||
# Sidebar Core Modules
|
||||
ClientProvider,
|
||||
CustomsBroker,
|
||||
Part,
|
||||
Company,
|
||||
|
||||
# Reference Data
|
||||
Country,
|
||||
CurrencyType,
|
||||
CustomsSection,
|
||||
CustomsWarehouse,
|
||||
Incoterm,
|
||||
InvoiceType,
|
||||
MaterialType,
|
||||
PaymentMethod,
|
||||
PedimentoCode,
|
||||
RegimenPedimento,
|
||||
Sector,
|
||||
State,
|
||||
TransportMode,
|
||||
TransportType,
|
||||
ValuationMethod,
|
||||
UnitOfMeasure,
|
||||
ExchangeRate,
|
||||
Identifier,
|
||||
Class,
|
||||
ClassificationConcept,
|
||||
Concept,
|
||||
CustomsBrokerConcept,
|
||||
DepreciationCatalog,
|
||||
Doda,
|
||||
ElectronicNotice,
|
||||
Equivalency,
|
||||
ErrorCatalog,
|
||||
FDACatalog,
|
||||
INPC,
|
||||
Legend,
|
||||
MultiCurrencyType,
|
||||
Package,
|
||||
Port,
|
||||
Prevalidator,
|
||||
Seal,
|
||||
Signature,
|
||||
TariffFraction,
|
||||
UnitConversion,
|
||||
USTariffFraction
|
||||
])
|
||||
register_audit_listeners(
|
||||
[
|
||||
# Core Transactions
|
||||
Pedimentos,
|
||||
InvoiceHeader,
|
||||
InvoiceSalesDetails,
|
||||
Item,
|
||||
# Sidebar Core Modules
|
||||
ClientProvider,
|
||||
CustomsBroker,
|
||||
Part,
|
||||
Company,
|
||||
# Reference Data
|
||||
Country,
|
||||
CurrencyType,
|
||||
CustomsSection,
|
||||
CustomsWarehouse,
|
||||
Incoterm,
|
||||
InvoiceType,
|
||||
MaterialType,
|
||||
PaymentMethod,
|
||||
PedimentoCode,
|
||||
RegimenPedimento,
|
||||
Sector,
|
||||
State,
|
||||
TransportMode,
|
||||
TransportType,
|
||||
ValuationMethod,
|
||||
UnitOfMeasure,
|
||||
ExchangeRate,
|
||||
Identifier,
|
||||
Class,
|
||||
ClassificationConcept,
|
||||
Concept,
|
||||
CustomsBrokerConcept,
|
||||
DepreciationCatalog,
|
||||
Doda,
|
||||
ElectronicNotice,
|
||||
Equivalency,
|
||||
ErrorCatalog,
|
||||
FDACatalog,
|
||||
INPC,
|
||||
Legend,
|
||||
MultiCurrencyType,
|
||||
Package,
|
||||
Port,
|
||||
Prevalidator,
|
||||
Seal,
|
||||
Signature,
|
||||
TariffFraction,
|
||||
UnitConversion,
|
||||
USTariffFraction,
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
# Crear directorio de uploads si no existe y montar archivos estáticos
|
||||
|
||||
Reference in New Issue
Block a user