2025-07-19 17:49:03 +02:00
|
|
|
# services/tenant/app/main.py
|
2025-07-17 13:09:24 +02:00
|
|
|
"""
|
2025-07-20 23:43:42 +02:00
|
|
|
Tenant Service FastAPI application - FIXED VERSION
|
2025-07-17 13:09:24 +02:00
|
|
|
"""
|
|
|
|
|
|
2025-07-18 14:41:39 +02:00
|
|
|
import structlog
|
2025-07-17 13:09:24 +02:00
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
from app.core.config import settings
|
2025-07-20 09:18:08 +02:00
|
|
|
from app.core.database import database_manager
|
2025-09-25 14:30:47 +02:00
|
|
|
from app.api import tenants, subscriptions, webhooks
|
2025-07-17 13:09:24 +02:00
|
|
|
from shared.monitoring.logging import setup_logging
|
|
|
|
|
from shared.monitoring.metrics import MetricsCollector
|
|
|
|
|
|
|
|
|
|
# Setup logging
|
2025-07-19 17:49:03 +02:00
|
|
|
setup_logging("tenant-service", settings.LOG_LEVEL)
|
2025-07-18 14:41:39 +02:00
|
|
|
logger = structlog.get_logger()
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
# Create FastAPI app
|
|
|
|
|
app = FastAPI(
|
2025-07-19 17:49:03 +02:00
|
|
|
title="Tenant Management Service",
|
|
|
|
|
description="Multi-tenant bakery management service",
|
|
|
|
|
version="1.0.0",
|
|
|
|
|
docs_url="/docs",
|
|
|
|
|
redoc_url="/redoc"
|
2025-07-17 13:09:24 +02:00
|
|
|
)
|
|
|
|
|
|
2025-07-19 17:49:03 +02:00
|
|
|
# Initialize metrics
|
|
|
|
|
metrics_collector = MetricsCollector("tenant_service")
|
|
|
|
|
app.state.metrics_collector = metrics_collector
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
# CORS middleware
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=["*"],
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-19 17:49:03 +02:00
|
|
|
# Include routers
|
|
|
|
|
app.include_router(tenants.router, prefix="/api/v1", tags=["tenants"])
|
2025-09-21 13:27:50 +02:00
|
|
|
app.include_router(subscriptions.router, prefix="/api/v1", tags=["subscriptions"])
|
2025-09-25 14:30:47 +02:00
|
|
|
app.include_router(webhooks.router, prefix="/api/v1", tags=["webhooks"])
|
2025-07-19 17:49:03 +02:00
|
|
|
|
2025-07-17 13:09:24 +02:00
|
|
|
@app.on_event("startup")
|
|
|
|
|
async def startup_event():
|
2025-07-19 17:49:03 +02:00
|
|
|
"""Initialize service on startup"""
|
|
|
|
|
logger.info("Starting Tenant Service...")
|
2025-07-17 13:09:24 +02:00
|
|
|
|
2025-07-20 23:43:42 +02:00
|
|
|
try:
|
|
|
|
|
# ✅ FIX: Import models to ensure they're registered with SQLAlchemy
|
|
|
|
|
from app.models.tenants import Tenant, TenantMember, Subscription
|
|
|
|
|
logger.info("Tenant models imported successfully")
|
|
|
|
|
|
|
|
|
|
# ✅ FIX: Create database tables on startup
|
|
|
|
|
await database_manager.create_tables()
|
|
|
|
|
logger.info("Tenant database tables created successfully")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Failed to initialize tenant service: {e}")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
logger.info("Tenant Service startup completed successfully")
|
|
|
|
|
|
2025-07-19 17:49:03 +02:00
|
|
|
@app.on_event("shutdown")
|
|
|
|
|
async def shutdown_event():
|
|
|
|
|
"""Cleanup on shutdown"""
|
|
|
|
|
logger.info("Shutting down Tenant Service...")
|
2025-07-20 23:43:42 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Close database connections properly
|
|
|
|
|
if hasattr(database_manager, 'engine') and database_manager.engine:
|
|
|
|
|
await database_manager.engine.dispose()
|
|
|
|
|
logger.info("Database connections closed")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error during shutdown: {e}")
|
|
|
|
|
|
|
|
|
|
logger.info("Tenant Service shutdown completed")
|
2025-07-17 13:09:24 +02:00
|
|
|
|
|
|
|
|
@app.get("/health")
|
|
|
|
|
async def health_check():
|
|
|
|
|
"""Health check endpoint"""
|
|
|
|
|
return {
|
|
|
|
|
"status": "healthy",
|
|
|
|
|
"service": "tenant-service",
|
|
|
|
|
"version": "1.0.0"
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 17:49:03 +02:00
|
|
|
@app.get("/metrics")
|
|
|
|
|
async def metrics():
|
|
|
|
|
"""Prometheus metrics endpoint"""
|
2025-07-26 18:46:52 +02:00
|
|
|
return metrics_collector.get_metrics()
|
2025-07-19 17:49:03 +02:00
|
|
|
|
2025-07-17 13:09:24 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import uvicorn
|
2025-09-25 14:30:47 +02:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|