o
    5)iO                     @   s   d dl Z d dl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mZmZmZmZ dd	lmZ dd
dZe ZdS )    N)datetime)Flaskgrequest)CORS)setup_error_handling)CacheService)
get_loggerlog_exceptionlog_request_infolog_response_infosetup_logger   )configc                    s  t t}d|j_| ptjdd} |jt|   |jddg|jdg d|jdd	d
g|jdd|jdddd}t	|j t
|fi | t| zddlm} td  d W n	 tyl   Y nw t| td t| tjd|jdd |jdd }|j fdd}ddlm} || zddlm} ||  d W n ty    d Y nw  d|   |S ) NF	FLASK_ENVdefaultCORS_ORIGINS*CORS_METHODS)GETPOSTPUTDELETEOPTIONSCORS_ALLOW_HEADERSzContent-TypeAuthorizationCORS_SUPPORTS_CREDENTIALSCORS_MAX_AGEiX  T)originsmethodsallow_headerssupports_credentialsmax_ageautomatic_optionsr   )CompressappzResponse compression enabledPYPANDOC_PANDOCPANDOC_LOCATIONz/usr/bin/pandocc                   S   sF   t jrnt jdddkrdt jd< tt t_	t
 t_t  dS )zTThis helps Flask correctly interpret HTTPS requests from Cloudflare proxied traffic.zX-Forwarded-Proto httpszwsgi.url_schemeN)r   	is_secureheadersgetenvironstruuiduuid4r   
request_idr   now
start_timer    r4   r4   */var/www/html/oms-prod-mcp/app/__init__.pybefore_requestE   s   


z"create_app.<locals>.before_requestc                    sR   t |  | jdd  ttdr't tj  } 	dtj
 d|dd | S )NzContent-Encodingr3   zRequest z completed in z.4fz seconds)r   r+   pophasattrr   r   r2   r3   total_secondsinfor1   )responsedurationloggerr4   r5   after_requestV   s   
z!create_app.<locals>.after_request)api_bp)
example_bpz$Example validation routes registeredz6Example controller not found - skipping example routesz+Flask API application created with config: )r   __name__json	sort_keysosr-   r,   r   from_objectprintr   r   init_appflask_compressr$   r	   r:   ImportErrorr   r   
setdefaultr6   r?   app.routes.apir@   register_blueprint"app.controllers.example_controllerrA   warning)config_namer%   cors_configr$   r6   r?   r@   rA   r4   r=   r5   
create_app   sX   




rR   )N)rE   r/   r   flaskr   r   r   
flask_corsr   app.middleware.error_handlerr   app.services.cache_servicer   app.utils.helpersr	   r
   r   r   r   config.settingsr   rR   r%   r4   r4   r4   r5   <module>   s    

b