
    KCj=                     T   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZmZmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'  e       Z( e         ed      Z)edefd       Z* ee(jV                  e(jX                  de*e(jZ                  sdnde(jZ                  sdnd      Z.e.j_                  ee(j`                  dddgdg        e.j_                  e       e(jb                  r(e.j_                  e       e.je                  d! e
              e.jg                  ee       e.jg                  ee       e.jg                  e4e       e.jk                  ee(jl                  "       e.jo                  d#      d$e8fd%       Z9y)&u  
Application entry point.

`uvicorn app.main:app` (dev) or `gunicorn -k uvicorn.workers.UvicornWorker
app.main:app` (production, see deployment/systemd/) both load this file.

The lifespan context manager is where the production-fail-fast principle
is enforced: if reference data, the feature builder, or any model fails
to load, the application refuses to start at all rather than starting
in a broken state. This is intentional and correct — see
app/models_loader/registry.py for the reasoning.
    )asynccontextmanager)FastAPI)RequestValidationError)CORSMiddleware)make_asgi_app)
api_router)get_settings)AppException)
get_loggersetup_logging)dispose_engine)app_exception_handlerunhandled_exception_handlervalidation_exception_handler)PrometheusMiddleware)RequestContextMiddleware)model_registry)cache_service)feature_builder)llm_service)reference_datazapp.mainappc           	     V  K   t         j                  dt        j                   dt        j                   dt        j
                   d       t        j                  rt        j                  st        d      t        j                          t        j                          t        j                          t        j                          t!        j                          d {    t         j                  d       d  t         j                  d       t#                d {    y 7 F7 w)Nz	Starting z vz ()zANTHROPIC_API_KEY is not set. Refusing to start in production without it. Set the key or explicitly accept fallback-only mode by changing ENVIRONMENT for a controlled rollout.u7   Startup complete — service is ready to accept trafficu.   Shutting down — closing database connections)loggerinfosettingsAPP_NAMEAPP_VERSIONENVIRONMENTis_productionANTHROPIC_API_KEYRuntimeErrorr   loadr   r   load_allr   initr   r   )r   s    5C:\Crop_Prediction\Backend\crop-ai-system\app\main.pylifespanr(   -   s     
KK)H--.b1E1E0FbI]I]H^^_`ah&@&@ @
 	
 




KKIJ	
KK@A

  s%   CD)D%A D)D' D)'D)zProduction API for the AI Crop Decision Support System. Evaluates crop suitability for a given location and month using 6 ML models, with LLM-generated explanations./docsNz/redoc)titleversiondescriptionr(   docs_url	redoc_urlTGETPOST*)allow_originsallow_credentialsallow_methodsallow_headersz/metrics)prefix/returnc                  x   K   t         j                  t         j                  dt         j                  sddS ddS w)Nrunningr)   zdisabled in production)servicer+   statusdocs)r   r   r   r!        r'   rootr@   p   sB      $$'''55	  <T	 s   8:):__doc__
contextlibr   fastapir   fastapi.exceptionsr   fastapi.middleware.corsr   prometheus_clientr   app.api.v1.routerr   app.core.configr	   app.core.exceptionsr
   app.core.logging_configr   r   app.db.sessionr   !app.middleware.exception_handlersr   r   r   app.middleware.metricsr   app.middleware.request_contextr   app.models_loader.registryr   app.services.cache_servicer   app.services.feature_builderr   app.services.llm_servicer   app.services.reference_datar   r   r   r(   r   r   r!   r   add_middlewarecors_origins_listENABLE_METRICSmountadd_exception_handler	Exceptioninclude_routerAPI_V1_PREFIXgetdictr@   r>   r?   r'   <module>r^      s   +  5 2 + ( ( , = ) 
 8 C 5 4 8 0 6> 	J	   < 


  	8 $22W&44h$"   ,,&/%     + ,+,IIj-/*   ,(= >   02N O   )%@ A   :h&<&<  = D  r?   