@@ -45,65 +45,72 @@ def setup_logger(settings: DynamicSidecarSettings):
4545    logging .root .setLevel (settings .log_level )
4646
4747
48- def  assemble_application () ->  FastAPI :
49-     """ 
50-     Creates the application from using the env vars as a context 
51-     Also stores inside the state all instances of classes 
52-     needed in other requests and used to share data. 
53-     """ 
48+ def  create_basic_app () ->  FastAPI :
49+     # settings 
5450    settings  =  DynamicSidecarSettings .create_from_envs ()
5551    setup_logger (settings )
5652    logger .debug (settings .json (indent = 2 ))
5753
58-     application  =  FastAPI (
54+     # minimal 
55+     app  =  FastAPI (
5956        debug = settings .DEBUG ,
6057        openapi_url = f"/api/{ API_VTAG }  /openapi.json" ,
6158        docs_url = "/dev/doc" ,
6259    )
63-     override_fastapi_openapi_method (application )
60+     override_fastapi_openapi_method (app )
61+     app .state .settings  =  settings 
6462
65-     application .state .settings  =  settings 
66-     application .state .shared_store  =  SharedStore ()
67-     application .state .application_health  =  ApplicationHealth ()
63+     app .include_router (main_router )
64+     return  app 
6865
69-     # ROUTES  -------------------- 
70-     application .include_router (main_router )
7166
72-     # ERROR HANDLERS  ------------ 
73-     application .add_exception_handler (NodeNotFound , node_not_found_error_handler )
74-     application .add_exception_handler (BaseDynamicSidecarError , http_error_handler )
67+ def  create_app ():
68+     """ 
69+     Creates the application from using the env vars as a context 
70+     Also stores inside the state all instances of classes 
71+     needed in other requests and used to share data. 
72+     """ 
73+ 
74+     app  =  create_basic_app ()
7575
7676    # MODULES SETUP -------------- 
7777
78-     if   settings . is_development_mode : 
79-          remote_debug_setup ( application )
78+     app . state . shared_store   =   SharedStore () 
79+     app . state . application_health   =   ApplicationHealth ( )
8080
81-     if  settings .RABBIT_SETTINGS :
82-         setup_rabbitmq (application )
83-         setup_background_log_fetcher (application )
81+     if  app .state .settings .is_development_mode :
82+         remote_debug_setup (app )
83+ 
84+     if  app .state .settings .RABBIT_SETTINGS :
85+         setup_rabbitmq (app )
86+         setup_background_log_fetcher (app )
8487
8588    # also sets up mounted_volumes 
86-     setup_mounted_fs (application )
87-     setup_directory_watcher (application )
89+     setup_mounted_fs (app )
90+     setup_directory_watcher (app )
91+ 
92+     # ERROR HANDLERS  ------------ 
93+     app .add_exception_handler (NodeNotFound , node_not_found_error_handler )
94+     app .add_exception_handler (BaseDynamicSidecarError , http_error_handler )
8895
8996    # EVENTS --------------------- 
9097    async  def  _on_startup () ->  None :
91-         await  login_registry (settings .REGISTRY_SETTINGS )
92-         await  volumes_fix_permissions (application .state .mounted_volumes )
98+         await  login_registry (app . state . settings .REGISTRY_SETTINGS )
99+         await  volumes_fix_permissions (app .state .mounted_volumes )
93100        print (WELCOME_MSG , flush = True )
94101
95102    async  def  _on_shutdown () ->  None :
96103        logger .info ("Going to remove spawned containers" )
97104        result  =  await  remove_the_compose_spec (
98-             shared_store = application .state .shared_store ,
99-             settings = settings ,
100-             command_timeout = settings .DYNAMIC_SIDECAR_DOCKER_COMPOSE_DOWN_TIMEOUT ,
105+             shared_store = app .state .shared_store ,
106+             settings = app . state . settings ,
107+             command_timeout = app . state . settings .DYNAMIC_SIDECAR_DOCKER_COMPOSE_DOWN_TIMEOUT ,
101108        )
102109        logger .info ("Container removal did_succeed=%s\n %s" , result [0 ], result [1 ])
103110
104111        logger .info ("shutdown cleanup completed" )
105112
106-     application .add_event_handler ("startup" , _on_startup )
107-     application .add_event_handler ("shutdown" , _on_shutdown )
113+     app .add_event_handler ("startup" , _on_startup )
114+     app .add_event_handler ("shutdown" , _on_shutdown )
108115
109-     return  application 
116+     return  app 
0 commit comments