Merge pull request #406 from agoose77/master_pre_patches_1

Cleanup middleware decorator
This commit is contained in:
Eli Uriegas 2017-02-12 12:14:50 -06:00 committed by GitHub
commit d8c4c1525d

View File

@ -144,14 +144,12 @@ class Sanic:
return response return response
# Decorator # Decorator
def middleware(self, *args, **kwargs): def middleware(self, middleware_or_request):
""" """
Decorates and registers middleware to be called before a request Decorates and registers middleware to be called before a request
can either be called as @app.middleware or @app.middleware('request') can either be called as @app.middleware or @app.middleware('request')
""" """
attach_to = 'request' def register_middleware(middleware, attach_to='request'):
def register_middleware(middleware):
if attach_to == 'request': if attach_to == 'request':
self.request_middleware.append(middleware) self.request_middleware.append(middleware)
if attach_to == 'response': if attach_to == 'response':
@ -159,11 +157,12 @@ class Sanic:
return middleware return middleware
# Detect which way this was called, @middleware or @middleware('AT') # Detect which way this was called, @middleware or @middleware('AT')
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]): if callable(middleware_or_request):
return register_middleware(args[0]) return register_middleware(middleware_or_request)
else: else:
attach_to = args[0] return partial(register_middleware,
return register_middleware attach_to=middleware_or_request)
# Static Files # Static Files
def static(self, uri, file_or_directory, pattern='.+', def static(self, uri, file_or_directory, pattern='.+',