
    i[                     z   d Z ddlZddlZddlZddlZddlZddlmZ  ee	      j                  Zej                  j                  d ee             ddlmZmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ  ed	d
d      Zej9                  edgddgdg       ej;                  de        ee	      j                  dz  Zej?                         r!ej;                  d e ee            d       ejA                  de      d        Z!ejA                  d      d        Z" ejF                         Z$da%d Z&d Z'ejA                  d      d        Z(e)d k(  rf e*d!        e*d"        e*d!        e*d#        e*d$        e*d%        e*d&        e*d'        e*d!        e'         ejV                  d(d)d*d+       yy),a  
Unified Server for Dr. Robert Young Semantic Search

This server combines both the frontend (HTML/CSS/JS) and backend (API) 
into a single FastAPI application running on one port, eliminating the need
for separate servers and CORS configuration.

Key Features:
- Serves frontend HTML/CSS/JS files directly
- Mounts backend API under /api prefix
- Handles CORS automatically
- Single deployment URL for entire application
- Health check endpoint for monitoring
    N)Path)FastAPIRequest)StaticFiles)CORSMiddleware)HTMLResponse)appz&Dr. Robert O . Young  - Unified Serverz$Combined frontend and backend serverz1.0.0)titledescriptionversion*T)allow_originsallow_credentialsallow_methodsallow_headersz/apifrontendz/static)	directorystatic)name/)response_classc                     K   t         dz  } | j                         r)t        | dd      5 }|j                         }ddd       |S t	        dd      S # 1 sw Y   S xY ww)	zServe the main frontend page
    
    This endpoint reads and serves the index.html file from the frontend directory,
    providing the complete chat interface to users.
    
    Returns:
        HTMLResponse: Complete frontend HTML page
    z
index.htmlrzutf-8)encodingNzF<h1>Frontend not found</h1><p>Please check the frontend directory.</p>i  )contentstatus_code)frontend_direxistsopenreadr   )frontend_pathfr   s      /var/www/html/unified_server.pyrootr$   @   se      !</M-w7 	1ffhG	 \
 	
	s   (AAAAAz/healthc                     K   dddS w)a!  Health check endpoint for monitoring and deployment verification
    
    This endpoint provides a simple way to verify that the server is running
    and responding correctly, useful for uptime monitoring and CI/CD pipelines.
    
    Returns:
        dict: Health status information
    healthyzunified-server)statusservice r)       r#   health_checkr+   V   s       ,<==s   Fc                     t        t        dz  dz        } t        j                  }t	        j
                  d       	 t        j                         rt        d       n1t        5  da	t        dd        t        dt	        j                  d	              t        d        	 t        j                  |d
| gt        t              t        j                  t        j                  d      }|j                  D ]#  }|j!                         }|st        d|        % |j#                  d       |j$                  dk(  rt        d       nt        d|j$                   d       t        d       ddlm}  |        t        d       da		 d d d        t	        j
                  d       i# t        j*                  $ r j-                          t        d       Y Rt.        $ r}t        d|        Y d }~md }~ww xY w# da	w xY w# 1 sw Y   zxY w)Nscraperzscrape_and_embed.pyi  Tz5[AUTO-SCRAPE] Previous scrape still running, skipping
<============================================================z![AUTO-SCRAPE] Starting scrape at z%Y-%m-%d %H:%M:%Sz-u)cwdstdoutstderrtextz[AUTO-SCRAPE] i  )timeoutr   z+[AUTO-SCRAPE] Scrape completed successfullyz"[AUTO-SCRAPE] Scrape failed (code )z)[AUTO-SCRAPE] Refreshing article cache...)load_article_cachez[AUTO-SCRAPE] Cache refreshedz-[AUTO-SCRAPE] Scrape timed out (30 min limit)z[AUTO-SCRAPE] Error: F)strproject_rootsys
executable_timesleep_scrape_locklockedprint_scrape_runningstrftime
subprocessPopenPIPESTDOUTr1   rstripwait
returncodebackend.mainr6   TimeoutExpiredkill	Exception)scraper_pathpython_pathprocessliner6   es         r#   _run_scheduled_scraperR   f   s   |i/2GGHL..K	KK
 IJ &,"&6(m$9%..I\:]9^_`"!,(..$dL9-))00!G !( ;#{{}!N4&"9:; LLL.))Q.KL B7CUCUBVVWXY EF?&(9: ',OM&,P 	DY H "00 KLLNIJ  71!5667 ',OM&, &,sV   #>G?"A'F%
A9F%G?%.G5G8G5G0+G80G55G88G<<G??Hc                  p    t        j                  t        d      } | j                          t	        d       y )NT)targetdaemonz.[AUTO-SCRAPE] Scheduler started (every 1 hour))	threadingThreadrR   startr?   )threads    r#   start_auto_scraperrZ      s'    %:4HF
LLN	
:;r*   z/health/scraperc                     K   t         ddS w)N<   )scraper_runninginterval_minutes)r@   r)   r*   r#   scraper_statusr_      s     .BGGs   
__main__r/   zUNIFIED SERVER STARTINGz2Single URL for everything: http://192.168.1.4:8000z Frontend: http://127.0.0.1:8000/z'Backend API: http://127.0.0.1:8000/api/z(API Docs: http://127.0.0.1:8000/api/docszAuto-scrape: Every 1 hourzunified_server:appz0.0.0.0i@  )hostportreload),__doc__osr9   timer;   rV   rB   pathlibr   __file__parentr8   pathinsertr7   fastapir   r   fastapi.staticfilesr   fastapi.middleware.corsr   fastapi.responsesr   uvicornrI   r	   backend_appadd_middlewaremountr   r   getr$   r+   Lockr=   r@   rR   rZ   r_   __name__r?   runr)   r*   r#   <module>rx      s   
 
     H~$$ 3|$ % % + 2 *  , 
26   %%%   		&+  H~$$z1IIis</@AIQ\*
 +
* 	> 	> y~~3j<
 	H H z	(O	
#$	(O	
>?	
,-	
34	
45	
%&	(O  GKK	! r*   