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
@@ -67,12 +68,11 @@ def __init__(
6768
6869 self ._user_middleware = list (t .cast (list , self .config .MIDDLEWARE ))
6970
70- self ._static_app : t .Optional [ASGIApp ] = None
71-
7271 self .state = State ()
7372 self .config .DEFAULT_LIFESPAN_HANDLER = (
7473 lifespan or self .config .DEFAULT_LIFESPAN_HANDLER
7574 )
75+
7676 self .router = ApplicationRouter (
7777 routes = self ._get_module_routes (),
7878 redirect_slashes = self .config .REDIRECT_SLASHES ,
@@ -81,10 +81,20 @@ def __init__(
8181 self .config .DEFAULT_LIFESPAN_HANDLER # type: ignore[arg-type]
8282 ).lifespan ,
8383 )
84+ if (
85+ self .config .STATIC_MOUNT_PATH
86+ and self .config .STATIC_MOUNT_PATH not in self .router .routes
87+ ):
88+ self .router .append (AppStaticFileMount (self ))
89+
8490 self ._finalize_app_initialization ()
8591 self .middleware_stack = self .build_middleware_stack ()
8692 self ._config_logging ()
8793
94+ self .reload_event_manager += lambda app : self ._update_jinja_env_filters ( # type:ignore[misc]
95+ self .jinja_environment
96+ )
97+
8898 def _config_logging (self ) -> None :
8999 log_level = (
90100 self .config .LOG_LEVEL .value
@@ -106,26 +116,8 @@ def _config_logging(self) -> None:
106116 logging .getLogger ("ellar" ).setLevel (log_level )
107117 logging .getLogger ("ellar.request" ).setLevel (log_level )
108118
109- def _statics_wrapper (self ) -> t .Callable :
110- async def _statics_func_wrapper (
111- scope : TScope , receive : TReceive , send : TSend
112- ) -> t .Any :
113- assert self ._static_app , 'app static ASGIApp can not be "None"'
114- return await self ._static_app (scope , receive , send )
115-
116- return _statics_func_wrapper
117-
118119 def _get_module_routes (self ) -> t .List [BaseRoute ]:
119120 _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- )
129121
130122 for _ , module_ref in self ._injector .get_modules ().items ():
131123 _routes .extend (module_ref .routes )
@@ -157,9 +149,8 @@ def install_module(
157149 module_ref .run_module_register_services ()
158150 if isinstance (module_ref , ModuleTemplateRef ):
159151 self .router .extend (module_ref .routes )
160- self .reload_static_app ()
161152
162- self .rebuild_middleware_stack ()
153+ self .rebuild_stack ()
163154
164155 return t .cast (T , module_ref .get_module_instance ())
165156
@@ -189,12 +180,6 @@ def injector(self) -> EllarInjector:
189180 def versioning_scheme (self ) -> BaseAPIVersioning :
190181 return t .cast (BaseAPIVersioning , self ._config .VERSIONING_SCHEME )
191182
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-
198183 @property
199184 def config (self ) -> "Config" :
200185 return self ._config
@@ -298,7 +283,9 @@ def _finalize_app_initialization(self) -> None:
298283 self .injector .container .register_instance (self )
299284 self .injector .container .register_instance (self .config , Config )
300285 self .injector .container .register_instance (self .jinja_environment , Environment )
301- self .injector .container .register_instance (self .jinja_environment , Environment )
286+ self .injector .container .register_instance (
287+ self .jinja_environment , JinjaEnvironment
288+ )
302289
303290 def add_exception_handler (
304291 self ,
@@ -310,10 +297,11 @@ def add_exception_handler(
310297 self ._exception_handlers .append (exception_handler )
311298 _added_any = True
312299 if _added_any :
313- self .rebuild_middleware_stack ()
300+ self .rebuild_stack ()
314301
315- def rebuild_middleware_stack (self ) -> None :
302+ def rebuild_stack (self ) -> None :
316303 self .middleware_stack = self .build_middleware_stack ()
304+ self .reload_event_manager .run (self )
317305
318306 @property
319307 def reflector (self ) -> Reflector :
0 commit comments