Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException, status | |
| import asyncio | |
| import logging | |
| # State scheme imports | |
| from api.routes import endpoints | |
| from api.services.scheme_service import load_all_schemes_into_cache, is_cache_loading, cached_all_schemes | |
| from api.routes import recommend_route | |
| from api.routes import central_endpoints | |
| from api.services.central_services import ( | |
| load_central_schemes_into_cache, | |
| get_central_cache_loading_status, | |
| is_central_cache_loading, | |
| _central_schemes_cache | |
| ) | |
| from api.core.firebase_utils import db, initialize_firebase | |
| from fastapi.middleware.cors import CORSMiddleware | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # --- Application Startup Event --- | |
| async def startup_event(): | |
| """ | |
| Called when the FastAPI application starts. | |
| Initializes Firebase and initiates the loading of all schemes into the cache. | |
| """ | |
| initialize_firebase() | |
| # Start cache loading in the background for both state and central schemes | |
| asyncio.create_task(load_all_schemes_into_cache()) | |
| asyncio.create_task(load_central_schemes_into_cache()) | |
| logger.info("Application startup: Initiated cache loading for state and central schemes.") | |
| # --- API Endpoints (include routers) --- | |
| app.include_router(endpoints.router) | |
| app.include_router( | |
| central_endpoints.central_router, | |
| prefix="/central", # <-- MODIFIED: Removed "/{lang}" from here | |
| tags=["Central Schemes"] | |
| ) | |
| app.include_router(recommend_route.router) | |
| def root(): | |
| """Welcome message for the API.""" | |
| return {"message": "Welcome to Chathur API"} | |
| # --- Cache Status and Refresh Endpoints --- | |
| def get_state_cache_status(): | |
| """Returns the current status of the state scheme cache.""" | |
| return { | |
| "cache_loaded": bool(cached_all_schemes), | |
| "cache_loading": is_cache_loading, | |
| "states_in_cache": len(cached_all_schemes) | |
| } | |
| # NEW: Separate endpoint for central scheme cache status | |
| def get_central_cache_status(): | |
| """Returns the current status of the central scheme cache.""" | |
| # Use 'en' as the default key to check for loaded ministries | |
| default_lang_cache = _central_schemes_cache.get("en", {}) | |
| return { | |
| "cache_loaded": bool(default_lang_cache), | |
| "cache_loading": get_central_cache_loading_status(), | |
| "ministries_in_cache": len(default_lang_cache) | |
| } | |
| async def refresh_schemes_cache(): | |
| """ | |
| Manually triggers a refresh of the in-memory STATE schemes cache. | |
| """ | |
| if is_cache_loading: | |
| raise HTTPException( | |
| status_code=status.HTTP_409_CONFLICT, | |
| detail="State schemes cache refresh already in progress." | |
| ) | |
| asyncio.create_task(load_all_schemes_into_cache()) # Trigger in background | |
| return {"message": "State schemes cache refresh initiated."} | |
| async def refresh_central_schemes_cache(): | |
| """ | |
| Manually triggers a refresh of the in-memory CENTRAL schemes cache. | |
| """ | |
| if get_central_cache_loading_status(): # Using the function from central_services | |
| raise HTTPException( | |
| status_code=status.HTTP_409_CONFLICT, | |
| detail="Central schemes cache refresh already in progress." | |
| ) | |
| asyncio.create_task(load_central_schemes_into_cache()) # Trigger in background | |
| return {"message": "Central schemes cache refresh initiated."} | |