Spaces:
Sleeping
Sleeping
File size: 3,722 Bytes
f3ca15e aaa9a08 f3ca15e aaa9a08 1c3cab3 aaa9a08 2e108ec aaa9a08 2e108ec aaa9a08 2e108ec f3ca15e aaa9a08 1c3cab3 f3ca15e aaa9a08 f3ca15e aaa9a08 f3ca15e aaa9a08 bbd3a01 aaa9a08 bbd3a01 aaa9a08 2e108ec f3ca15e aaa9a08 f3ca15e aaa9a08 f3ca15e aaa9a08 f3ca15e aaa9a08 f3ca15e aaa9a08 f3ca15e aaa9a08 f3ca15e aaa9a08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 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 ---
@app.on_event("startup")
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)
@app.get("/")
def root():
"""Welcome message for the API."""
return {"message": "Welcome to Chathur API"}
# --- Cache Status and Refresh Endpoints ---
@app.get("/state_cache_status")
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
@app.get("/central_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)
}
@app.post("/schemes/refresh_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."}
@app.post("/central_schemes/refresh_cache")
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."}
|