a
    ©ˆc²$  ã                   @   s’   d Z ddlZddlZddlZddlmZ ddlmZ dadd„ Z	dej
dfdd	„Zd
d„ Zdd„ Zddd„Zej
dddfdd„ZedkrŽeƒ  dS )a  server is a module that enables using python through a web-server.

This module can be used as the entry point to the application. In that case, it
sets up a web-server.
web-pages are determines by the command line arguments passed in.
Use "--help" to list the supported arguments.

é    N)Ú	websocket)Úbackendsc                 C   sæ   | j ddddd� | j ddddd� | j d	d
tddd� | j ddtddd� | j ddtddd� | j ddddd� | j ddddd� | j dd td!d!d"d#� | j d$dd%d&d'� | j d(dd)d*d+� | j d,d-d.d/� | j d0tdd1d2d#� | S )3zb
    Add arguments known to this module. parser must be
    argparse.ArgumentParser instance.
    z-dz--debugz log debugging messages to stdoutÚ
store_true)ÚhelpÚactionz-sz--nosignalhandlerszQPrevent installation of signal handlers so server can be started inside a thread.z-iz--hostÚ	localhostz@the interface for the web-server to listen on (default: 0.0.0.0))ÚtypeÚdefaultr   z-pz--porti�  z;port number for the web-server to listen on (default: 8080)z-tz	--timeouti,  zLtimeout for reaping process on idle in seconds (default: 300s, 0 to disable)z-cz	--contentÚ z+root for web-pages to serve (default: none))r	   r   z-az	--authKeyzwslink-secretz;Authentication key for clients to connect to the WebSocket.z-wsz--ws-endpointÚwsz:Specify WebSocket endpoint. (e.g. foo/bar/ws, Default: ws))r   r	   Údestr   z--no-ws-endpointÚnowsz,If provided, disables the websocket endpoint)r   r   r   z--fs-endpointsÚfsEndpointsziadd another fs location to a specific endpoint (i.e: data=/Users/seb/Download|images=/Users/seb/Pictures))r	   r   r   z--reverse-urlÚreverse_urlz8Make the server act as a client to connect to a ws relay)r   r   z--sslÚsslz‰add a tuple file [certificate, key] (i.e: --ssl 'certificate,key') or adhoc string to generate temporary certificate (i.e: --ssl 'adhoc'))Úadd_argumentÚstrÚint)Úparser© r   úP/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/wslink/server.pyÚadd_arguments   s–    ÿüûûûüüúüüýûr   zwslink web-serverc                 C   sP   t j|d�}t|ƒ | | ¡}z| |¡ W n ty>   Y n0 t||d� dS )zÂ
    Sets up the web-server using with __name__ == '__main__'. This can also be
    called directly. Pass the optional protocol to override the protocol used.
    Default is ServerProtocol.
    )Údescription)ÚoptionsÚprotocolN)ÚargparseÚArgumentParserr   Ú
parse_argsÚ	configureÚAttributeErrorÚstart_webserver)Úargvr   r   r   Úargsr   r   r   Ústartx   s    
r#   c                  C   s   t rt ¡ } |  t  ¡ ¡S d S ©N)Ú	ws_serverÚasyncioÚget_event_loopÚcreate_taskÚstop)Úloopr   r   r   Ústop_webserver�   s    r+   c                   C   s   t rt  ¡ S dS )Néÿÿÿÿ)r%   Úget_portr   r   r   r   r-   –   s    r-   Úaiohttpc                 C   s   t j| |d�S )N©Úbackend)r   Úcreate_webserver)Úserver_configr0   r   r   r   r1   ¬   s    r1   FÚmainc                    s  |ƒ }|rd}n| j rtj}ntj}| jr<| j||dœ}�n<| j| j| j|dœ}| jsli |d< ||d | j	< t
| jƒdkr�i |d< | j|d d< t
| jƒdkrÞd|vr®i |d< | j d	¡D ]"}	|	 d
¡}
|
d |d |
d < qºt
| jƒdk�rlddlm}m} | jdk�r||d ƒ| _n(| j d¡}t
|ƒdk�r<tdƒ‚|| _| j\}}| |j¡}| ||¡ ||d< | j |d< t||d�at ¡ ‰d‰t|dƒ�r¢|j‰t|dƒ�r¸| t¡ ‡fdd„‰ ‡ ‡fdd„}‡ ‡fdd„}||ˆ dœ}||v�rtd|› �ƒ‚|| ƒ S )az  
    Starts the web-server with the given protocol. Options must be an object
    with the following members:
        options.host : the interface for the web-server to listen on
        options.port : port number for the web-server to listen on
        options.timeout : timeout for reaping process on idle in seconds
        options.content : root for web-pages to serve.
    N)r   Zws_protocolÚlogging_level)ÚhostÚportÚtimeoutr4   r   r   Zstaticú/é   ú|ú=é   )Úgenerate_ssl_pairr   Zadhocr5   ú,é   z<ssl configure must be "adhoc" or a tuple of files "cert,key"r   Zhandle_signalsr/   Úport_callbackÚ
set_serverc                      s
   t  ˆ ¡S r$   )r%   r#   r   )r@   r   r   Úcreate_coroutine  s    z)start_webserver.<locals>.create_coroutinec                      s(   zˆ  ˆ ƒ ¡ W ˆ ¡  n
ˆ ¡  0 d S r$   )Zrun_until_completeÚcloser   ©rB   r*   r   r   Ú	main_exec  s    z"start_webserver.<locals>.main_execc                      s   ˆ  ˆ ƒ ¡S r$   )r(   r   rD   r   r   Ú	task_exec   s    z"start_webserver.<locals>.task_exec)r3   ZtaskÚ	coroutinezUnknown exec_mode: )ÚdebugÚloggingÚDEBUGÚERRORr   r5   r6   r7   r   r   ÚlenÚcontentr   Úsplitr   Ússl_contextr=   Ú	ExceptionÚ
SSLContextÚPROTOCOL_TLS_SERVERÚload_cert_chainZnosignalhandlersr1   r%   r&   r'   Úhasattrr@   rA   )r   r   ZdisableLoggingr0   Z	exec_modeÚkwargsZwslinkServerr4   r2   ZfsResourceInfoZ	infoSplitr=   r   ÚtokensÚcertÚkeyÚcontextrE   rF   Z
exec_modesr   )rB   r*   r@   r   r    ´   sx    ý
ü
ÿ

ý
r    Ú__main__)r.   )Ú__doc__r   r&   rI   Zwslinkr   Zwslr   r%   r   ZServerProtocolr#   r+   r-   r1   r    Ú__name__r   r   r   r   Ú<module>   s$   Y	

û
{