3131 ModuleSetup ,
3232 ModuleTemplateRef ,
3333)
34- from ellar .core .routing import ApplicationRouter
34+ from ellar .core .routing import ApplicationRouter , AppStaticFileMount
3535from ellar .core .services import Reflector
3636from ellar .core .versioning import BaseAPIVersioning , VersioningSchemes
3737from ellar .di import EllarInjector
38- from starlette .routing import BaseRoute , Mount
38+ from jinja2 import Environment as JinjaEnvironment
39+ from starlette .routing import BaseRoute
3940
4041from .lifespan import EllarApplicationLifespan
4142from .mixin import AppMixin
@@ -73,6 +74,7 @@ def __init__(
7374 self .config .DEFAULT_LIFESPAN_HANDLER = (
7475 lifespan or self .config .DEFAULT_LIFESPAN_HANDLER
7576 )
77+
7678 self .router = ApplicationRouter (
7779 routes = self ._get_module_routes (),
7880 redirect_slashes = self .config .REDIRECT_SLASHES ,
@@ -81,10 +83,20 @@ def __init__(
8183 self .config .DEFAULT_LIFESPAN_HANDLER # type: ignore[arg-type]
8284 ).lifespan ,
8385 )
86+ if (
87+ self .config .STATIC_MOUNT_PATH
88+ and self .config .STATIC_MOUNT_PATH not in self .router .routes
89+ ):
90+ self .router .append (AppStaticFileMount (self ))
91+
8492 self ._finalize_app_initialization ()
8593 self .middleware_stack = self .build_middleware_stack ()
8694 self ._config_logging ()
8795
96+ self .reload_event_manager += lambda app : self ._update_jinja_env_filters ( # type:ignore[misc]
97+ self .jinja_environment
98+ )
99+
88100 def _config_logging (self ) -> None :
89101 log_level = (
90102 self .config .LOG_LEVEL .value
@@ -117,15 +129,6 @@ async def _statics_func_wrapper(
117129
118130 def _get_module_routes (self ) -> t .List [BaseRoute ]:
119131 _routes : t .List [BaseRoute ] = []
120- if self .has_static_files :
121- self ._static_app = self .create_static_app ()
122- _routes .append (
123- Mount (
124- str (self .config .STATIC_MOUNT_PATH ),
125- app = self ._statics_wrapper (),
126- name = "static" ,
127- )
128- )
129132
130133 for _ , module_ref in self ._injector .get_modules ().items ():
131134 _routes .extend (module_ref .routes )
@@ -157,9 +160,8 @@ def install_module(
157160 module_ref .run_module_register_services ()
158161 if isinstance (module_ref , ModuleTemplateRef ):
159162 self .router .extend (module_ref .routes )
160- self .reload_static_app ()
161163
162- self .rebuild_middleware_stack ()
164+ self .rebuild_stack ()
163165
164166 return t .cast (T , module_ref .get_module_instance ())
165167
@@ -189,12 +191,6 @@ def injector(self) -> EllarInjector:
189191 def versioning_scheme (self ) -> BaseAPIVersioning :
190192 return t .cast (BaseAPIVersioning , self ._config .VERSIONING_SCHEME )
191193
192- @property
193- def has_static_files (self ) -> bool : # type: ignore
194- return (
195- True if self .static_files or self .config .STATIC_FOLDER_PACKAGES else False
196- )
197-
198194 @property
199195 def config (self ) -> "Config" :
200196 return self ._config
@@ -298,7 +294,9 @@ def _finalize_app_initialization(self) -> None:
298294 self .injector .container .register_instance (self )
299295 self .injector .container .register_instance (self .config , Config )
300296 self .injector .container .register_instance (self .jinja_environment , Environment )
301- self .injector .container .register_instance (self .jinja_environment , Environment )
297+ self .injector .container .register_instance (
298+ self .jinja_environment , JinjaEnvironment
299+ )
302300
303301 def add_exception_handler (
304302 self ,
@@ -310,10 +308,11 @@ def add_exception_handler(
310308 self ._exception_handlers .append (exception_handler )
311309 _added_any = True
312310 if _added_any :
313- self .rebuild_middleware_stack ()
311+ self .rebuild_stack ()
314312
315- def rebuild_middleware_stack (self ) -> None :
313+ def rebuild_stack (self ) -> None :
316314 self .middleware_stack = self .build_middleware_stack ()
315+ self .reload_event_manager .run (self )
317316
318317 @property
319318 def reflector (self ) -> Reflector :
0 commit comments