Files
bakery-ia/services/tenant/app/main.py

72 lines
1.7 KiB
Python
Raw Normal View History

2025-07-19 17:49:03 +02:00
# services/tenant/app/main.py
"""
2025-07-19 17:49:03 +02:00
Tenant Service FastAPI application
"""
2025-07-18 14:41:39 +02:00
import structlog
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
2025-07-19 17:49:03 +02:00
from app.core.database import engine
from app.api import tenants
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()
# 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-19 17:49:03 +02:00
# Initialize metrics
metrics_collector = MetricsCollector("tenant_service")
app.state.metrics_collector = metrics_collector
# 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"])
@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-19 17:49:03 +02:00
@app.on_event("shutdown")
async def shutdown_event():
"""Cleanup on shutdown"""
logger.info("Shutting down Tenant Service...")
await engine.dispose()
@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"""
return metrics_collector.generate_latest()
if __name__ == "__main__":
import uvicorn
2025-07-19 17:49:03 +02:00
uvicorn.run(app, host="0.0.0.0", port=8000)