1010from ellar .common .models import ControllerBase , ControllerType
1111from ellar .di import RequestORTransientScope , injectable
1212from ellar .reflect import REFLECT_TYPE , reflect
13- from ellar .utils import get_type_of_base
1413from injector import Scope
14+ from starlette .middleware import Middleware
15+
16+ if t .TYPE_CHECKING :
17+ from ellar .core .middleware .middleware import EllarMiddleware
1518
1619
1720@t .no_type_check
@@ -20,6 +23,7 @@ def Controller(
2023 * ,
2124 name : t .Optional [str ] = None ,
2225 include_in_schema : bool = True ,
26+ middleware : t .Optional [t .Sequence [t .Union [Middleware , "EllarMiddleware" ]]] = None ,
2327 scope : t .Optional [t .Union [t .Type [Scope ], Scope ]] = RequestORTransientScope ,
2428) -> t .Union [t .Type [ControllerBase ], t .Callable [..., t .Any ], t .Any ]:
2529 """
@@ -30,6 +34,7 @@ def Controller(
3034 :param name: route name prefix for url reversing, eg name:route_name default=''
3135 :param include_in_schema: include controller in OPENAPI schema
3236 :param scope: Controller Instance Lifetime scope
37+ :param middleware: Controller Middlewares
3338 :return: t.Type[ControllerBase]
3439 """
3540 _prefix : t .Optional [t .Any ] = prefix if prefix is not None else NOT_SET
@@ -42,7 +47,11 @@ def Controller(
4247 ), "Controller Prefix must start with '/'"
4348 # TODO: replace with a ControllerTypeDict and OpenAPITypeDict
4449 kwargs = AttributeDict (
45- path = _prefix , name = name , include_in_schema = include_in_schema , processed = False
50+ path = _prefix ,
51+ name = name ,
52+ include_in_schema = include_in_schema ,
53+ processed = False ,
54+ middleware = middleware ,
4655 )
4756
4857 def _decorator (cls : t .Type ) -> t .Type [ControllerBase ]:
@@ -73,25 +82,26 @@ def _decorator(cls: t.Type) -> t.Type[ControllerBase]:
7382 .replace ("controller" , "" )
7483 )
7584
76- for base in get_type_of_base (ControllerBase , _controller_type ):
77- if reflect .has_metadata (CONTROLLER_WATERMARK , base ) and hasattr (
78- cls , "__CONTROLLER_WATERMARK__"
79- ):
80- raise ImproperConfiguration (
81- f"`@Controller` decorated classes does not support inheritance. \n "
82- f"{ _controller_type } "
83- )
84-
85- if not reflect .has_metadata (
86- CONTROLLER_WATERMARK , _controller_type
87- ) and not hasattr (cls , "__CONTROLLER_WATERMARK__" ):
88- reflect .define_metadata (CONTROLLER_WATERMARK , True , _controller_type )
89- # reflect_all_controller_type_routes(_controller_type)
90-
91- injectable (scope or RequestORTransientScope )(cls )
92-
93- for key in CONTROLLER_METADATA .keys :
94- reflect .define_metadata (key , kwargs [key ], _controller_type )
85+ # for base in get_type_of_base(ControllerBase, _controller_type):
86+ # if reflect.has_metadata(CONTROLLER_WATERMARK, base) and hasattr(
87+ # cls, "__CONTROLLER_WATERMARK__"
88+ # ):
89+ # raise ImproperConfiguration(
90+ # f"`@Controller` decorated classes does not support inheritance. \n"
91+ # f"{_controller_type}"
92+ # )
93+
94+ # if not reflect.has_metadata(
95+ # CONTROLLER_WATERMARK, _controller_type
96+ # ) and not hasattr(cls, "__CONTROLLER_WATERMARK__"):
97+
98+ reflect .define_metadata (CONTROLLER_WATERMARK , True , _controller_type )
99+ # reflect_all_controller_type_routes(_controller_type)
100+
101+ injectable (scope or RequestORTransientScope )(cls )
102+
103+ for key in CONTROLLER_METADATA .keys :
104+ reflect .define_metadata (key , kwargs [key ], _controller_type )
95105
96106 if new_cls :
97107 # if we forced cls to inherit from ControllerBase, we need to block it from been processed
0 commit comments