def middleware(self, middleware_or_request):
def register_middleware(middleware, attach_to='<STR_LIT>'):<EOL><INDENT>if attach_to == '<STR_LIT>':<EOL><INDENT>self.request_middleware.append(middleware)<EOL><DEDENT>if attach_to == '<STR_LIT>':<EOL><INDENT>self.response_middleware.appendleft(middleware)<EOL><DEDENT>return middleware<EOL><DEDENT>if callable(middleware_or_request):<EOL><INDENT>return register_middleware(middleware_or_request)<EOL><DEDENT>else:<EOL><INDENT>return partial(register_middleware,<EOL>attach_to=middleware_or_request)<EOL><DEDENT>
Decorate and register middleware to be called before a request.
        Can either be called as @app.middleware or @app.middleware('request')