72 lines
1.8 KiB
Python
72 lines
1.8 KiB
Python
# services/tenant/app/main.py
|
|
"""
|
|
Tenant Service FastAPI application
|
|
"""
|
|
|
|
import structlog
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.core.config import settings
|
|
from app.core.database import database_manager
|
|
from app.api import tenants
|
|
from shared.monitoring.logging import setup_logging
|
|
from shared.monitoring.metrics import MetricsCollector
|
|
|
|
# Setup logging
|
|
setup_logging("tenant-service", settings.LOG_LEVEL)
|
|
logger = structlog.get_logger()
|
|
|
|
# Create FastAPI app
|
|
app = FastAPI(
|
|
title="Tenant Management Service",
|
|
description="Multi-tenant bakery management service",
|
|
version="1.0.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc"
|
|
)
|
|
|
|
# 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=["*"],
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(tenants.router, prefix="/api/v1", tags=["tenants"])
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
"""Initialize service on startup"""
|
|
logger.info("Starting Tenant Service...")
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown_event():
|
|
"""Cleanup on shutdown"""
|
|
logger.info("Shutting down Tenant Service...")
|
|
await database_manager.engine.dispose()
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""Health check endpoint"""
|
|
return {
|
|
"status": "healthy",
|
|
"service": "tenant-service",
|
|
"version": "1.0.0"
|
|
}
|
|
|
|
@app.get("/metrics")
|
|
async def metrics():
|
|
"""Prometheus metrics endpoint"""
|
|
return metrics_collector.generate_latest()
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8000) |