text_processor/main.py
m.dabbagh 70f5b1478c init
2026-01-07 19:15:46 +03:30

119 lines
2.5 KiB
Python

"""
Main Application Entry Point.
This module creates and runs the FastAPI application.
"""
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from src.bootstrap import create_application
from src.shared.constants import (
API_DESCRIPTION,
API_DOCS_URL,
API_PREFIX,
API_REDOC_URL,
API_TITLE,
APP_VERSION,
)
logger = logging.getLogger(__name__)
# Application container (created on startup)
app_container = None
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Application lifespan manager.
Handles startup and shutdown events.
"""
# Startup
global app_container
logger.info("Starting up application...")
# Create application container with dependency injection
app_container = create_application(log_level="INFO")
logger.info("Application started successfully")
yield
# Shutdown
logger.info("Shutting down application...")
app_container = None
logger.info("Application shut down")
# Create FastAPI application
app = FastAPI(
title=API_TITLE,
description=API_DESCRIPTION,
version=APP_VERSION,
docs_url=API_DOCS_URL,
redoc_url=API_REDOC_URL,
lifespan=lifespan,
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure appropriately for production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
async def setup_routes():
"""Setup API routes on startup."""
if app_container:
# Include the API routes from the incoming adapter
app.include_router(
app_container.api.router,
prefix=API_PREFIX,
tags=["Text Processing"],
)
logger.info(f"API routes registered at {API_PREFIX}")
@app.get("/")
async def root():
"""Root endpoint with API information."""
return {
"name": API_TITLE,
"version": APP_VERSION,
"description": API_DESCRIPTION,
"docs_url": API_DOCS_URL,
"api_prefix": API_PREFIX,
}
@app.get("/health")
async def health_check():
"""Basic health check endpoint."""
return {
"status": "healthy",
"version": APP_VERSION,
}
if __name__ == "__main__":
import uvicorn
# Run the application
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=True, # Set to False in production
log_level="info",
)