108 lines
3.3 KiB
Python
108 lines
3.3 KiB
Python
# services/recipes/app/main.py
|
|
"""
|
|
Recipe Service - FastAPI application
|
|
Handles recipe management, production planning, and inventory consumption tracking
|
|
"""
|
|
|
|
import time
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.gzip import GZipMiddleware
|
|
|
|
from .core.config import settings
|
|
from .core.database import db_manager
|
|
from .api import recipes
|
|
from shared.service_base import StandardFastAPIService
|
|
# Import models to register them with SQLAlchemy metadata
|
|
from .models import recipes as recipe_models
|
|
|
|
|
|
class RecipesService(StandardFastAPIService):
|
|
"""Recipes Service with standardized setup"""
|
|
|
|
def __init__(self):
|
|
# Define expected database tables for health checks
|
|
recipes_expected_tables = [
|
|
'recipes', 'recipe_ingredients', 'production_batches',
|
|
'production_ingredient_consumption', 'production_schedules'
|
|
]
|
|
|
|
super().__init__(
|
|
service_name="recipes-service",
|
|
app_name="Recipe Management Service",
|
|
description="Comprehensive recipe management, production planning, and inventory consumption tracking for bakery operations",
|
|
version=settings.SERVICE_VERSION,
|
|
log_level=settings.LOG_LEVEL,
|
|
cors_origins=settings.ALLOWED_ORIGINS,
|
|
api_prefix=settings.API_V1_PREFIX,
|
|
database_manager=db_manager,
|
|
expected_tables=recipes_expected_tables
|
|
)
|
|
|
|
async def on_startup(self, app: FastAPI):
|
|
"""Custom startup logic for recipes service"""
|
|
# Custom startup completed
|
|
pass
|
|
|
|
async def on_shutdown(self, app: FastAPI):
|
|
"""Custom shutdown logic for recipes service"""
|
|
# Database cleanup is handled by the base class
|
|
pass
|
|
|
|
def get_service_features(self):
|
|
"""Return recipes-specific features"""
|
|
return [
|
|
"recipe_management",
|
|
"production_planning",
|
|
"inventory_consumption_tracking",
|
|
"batch_production",
|
|
"tenant_scoped_operations"
|
|
]
|
|
|
|
def setup_custom_middleware(self):
|
|
"""Setup custom middleware for recipes service"""
|
|
# Add GZip middleware
|
|
self.app.add_middleware(GZipMiddleware, minimum_size=1000)
|
|
|
|
# Request timing middleware
|
|
@self.app.middleware("http")
|
|
async def add_process_time_header(request: Request, call_next):
|
|
"""Add processing time header to responses"""
|
|
start_time = time.time()
|
|
response = await call_next(request)
|
|
process_time = time.time() - start_time
|
|
response.headers["X-Process-Time"] = str(process_time)
|
|
return response
|
|
|
|
|
|
# Create service instance
|
|
service = RecipesService()
|
|
|
|
# Create FastAPI app with standardized setup
|
|
app = service.create_app(
|
|
docs_url="/docs" if settings.DEBUG else None,
|
|
redoc_url="/redoc" if settings.DEBUG else None
|
|
)
|
|
|
|
# Setup standard endpoints
|
|
service.setup_standard_endpoints()
|
|
|
|
# Setup custom middleware
|
|
service.setup_custom_middleware()
|
|
|
|
# Include API routers with tenant-scoped paths
|
|
app.include_router(
|
|
recipes.router,
|
|
prefix=f"{settings.API_V1_PREFIX}/tenants",
|
|
tags=["recipes"]
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"main:app",
|
|
host="0.0.0.0",
|
|
port=8000,
|
|
reload=settings.DEBUG,
|
|
log_level=settings.LOG_LEVEL.lower()
|
|
) |