o
    hb                     @   sV  d Z ddlZddlmZ ddlmZ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 dd
lmZmZ ejejdd eeZe ZdaedefddZedddedZejeejddgdgd ej ej!dd ej ej!dd e"ddd Z#e"ddd Z$e"edd  Z%e&d!d"d# Z'e&d$d%d& Z(dS )'zQ
Data Service - Web scraping service for collecting articles from search engines
    N)asynccontextmanager)FastAPIHTTPException)CORSMiddlewareJSONResponse)connect_to_mongoclose_mongo_connection)get_settings)KafkaService)scraperarticlesz4%(asctime)s - %(name)s - %(levelname)s - %(message)s)levelformatappc              
   C  s   t d zt I dH  t d t at d W n ty5 } zt d|  W Y d}~nd}~ww dV  t d zt I dH  t d tr[t I dH  t d W dS W dS  tyx } zt d	|  W Y d}~dS d}~ww )
zApplication lifespan managerzStarting Data Service...NzConnected to MongoDBzKafka service initializedzFailed to initialize services: zShutting down Data Service...zMongoDB connection closedzKafka service closedzError during shutdown: )	loggerinfor   r   kafka_service	Exceptionerrorr	   close)r   e r   JC:\Users\vibe-look\OneDrive\Desktop\Pige-main\backend\data-service\main.pylifespan   s0   



r   zData Servicez@Web scraping service for collecting articles from search engines1.0.0)titledescriptionversionr   T*)Zallow_originsZallow_credentialsZallow_methodsZallow_headersz/api/v1)prefix  c                    s   t dddidS )zHandle 404 errorsr!   detailzResource not foundstatus_codecontentNr   requestexcr   r   r   not_found_handler\   s
   r)     c                    s"   t d|  tdddidS )zHandle 500 errorszInternal server error: r*   r"   zInternal server errorr#   N)r   r   r   r&   r   r   r   internal_error_handlere   s   r+   c                    s   t |jd|jidS )zHandle HTTP exceptionsr"   r#   N)r   r$   r"   r&   r   r   r   http_exception_handlero   s
   r,   /healthc                      s   ddddS )zHealth check endpointZhealthyzdata-servicer   )statusZservicer   Nr   r   r   r   r   health_checkx   s
   r/   /c                      s   dddddS )zRoot endpointzData Service APIr   z/docsr-   )messager   ZdocsZhealthNr   r   r   r   r   root   s   r2   ))__doc__logging
contextlibr   Zfastapir   r   Zfastapi.middleware.corsr   Zfastapi.responsesr   Zapp.config.databaser   r	   Zapp.config.settingsr
   Zapp.services.kafka_servicer   Zapp.apir   r   basicConfigINFO	getLogger__name__r   Zsettingsr   r   r   Zadd_middlewareZCORS_ORIGINSZinclude_routerZrouterZexception_handlerr)   r+   r,   getr/   r2   r   r   r   r   <module>   sX    
(	

	

	