o
    h                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZ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 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$m%Z%m&Z&m'Z' d dl(m)Z) e  Z*edefddZ+eej,ej-ej.e+ddZ/e/0dde	dede
fddZ1e/j2edgddgdgdgd e/j3ed d!gd" e/j3ed d#gd" e/j3ed d$gd" e/j3ed d%gd" e/j3ed d&gd" e/j3ed d'gd" e/4ede	d(edefd)d*Z4e/4ede	d(edefd+d,Z5e/4e6de	d(e6defd-d.Z7e/4e!de	d(e!defd/d0Z8e/4e9de	d(e9defd1d2Z:e/j;d3dd4d5e<de)fd6d7Z=dS )8    N)Callable)asynccontextmanager)FastAPIHTTPExceptionRequestResponse)CORSMiddleware)JSONResponse)ProgrammingError)auth_router)admin_router)company_list_router)vessel_router)seafarer_router)tiket_router)settings)MongoDBSingleton)OperationalError) handle_generic_exception_handlerhandle_programming_errorhttp_exception_error_handler handel_operational_error_handlerhandel_attribute_error_handler)CustomResponse_appc                 C  s   t }dV  dS )zInitialize the application.N)mongodb_instance)r   _ r   $/var/www/html/beacon-api/app/main.pylifespan!   s   
r   T)titleversiondescriptionr   debughttprequest	call_nextreturnc                    s6   t   }|| I dH }t   | }t||jd< |S )a  
    Add process time header to the response.

    Args:
        request (Request): The incoming request.
        call_next (Callable): The next callable in the middleware chain.

    Returns:
        Response: The response with added process time header.
    NzX-Process-Time)timestrheaders)r%   r&   
start_timeresponseprocess_timer   r   r   add_process_time_header3   s   r.   *)allow_originsallow_credentialsallow_methodsallow_headersexpose_headersz/FASTAPIAUTH)prefixtagsADMINzCOMPANY LISTzVESSEL LISTzSEAFARERS LISTTIKETexcc                    s   t | |S )zHandle HTTP exceptions.)r   r%   r:   r   r   r   exception_handlerX   s   
r<   c                       t | |I dH S )z)Handle MySQL ProgrammingError exceptions.N)r   r;   r   r   r   programming_error_handler^      r>   c                    r=   )zHandle generic exceptions.N)r   r;   r   r   r   generic_exception_handlerd   r?   r@   c                    s   t | |I d H N)r   r;   r   r   r   operational_error_handlerj      rB   c                    s   t | |I d H S rA   )r   r;   r   r   r   attribute_error_handlero   rC   rD   z/migrate)response_model_exclude_nonemessagec              
      s   zt ddddd| g t g d tddd	W S  t jy1 } ztd
t|dd }~w tyD } ztd
t|dd }~ww )Nalembicrevisionz--autogeneratez-mz"{}")rG   upgradehead1zMigration successful)statusrF   i  )status_codedetail)
subprocessrunformatr   CalledProcessErrorr   r)   	Exception)rF   er   r   r   run_migrationst   s   rU   )>rO   r(   collections.abcr   
contextlibr   fastapir   r   r   r   fastapi.middleware.corsr   fastapi.responsesr	   sqlalchemy.excr
   app.api.auth.routerr   app.api.admin.routerr   app.api.company_list.routerr   app.api.vessel_list.routerr   app.api.seafarer.routerr   app.api.tiket.routerr   
app.configr   app.database.main.mongor   r   app.exceptionsr   r   r   r   r   app.utils.schemas_utilsr   r   r   PROJECT_NAMEAPI_VERSIONDESCRIPTIONapp
middlewarer.   add_middlewareinclude_routerr<   r>   rS   r@   rB   AttributeErrorrD   getr)   rU   r   r   r   r   <module>   sr    

