Skip to content

Commit

Permalink
application: factor out middleware installation (#2732)
Browse files Browse the repository at this point in the history
Summary:
Currently, we apply the `_handle_errors` middleware as a decorator
directly on `__call__`. We plan to add middleware that is parameterized
on instance variables, so this pattern will no longer suffice. This
commit simply refactors the existing code to apply middleware at
initialization time.

Test Plan:
It suffices to run `bazel test //tensorboard/backend:application_test`.

wchargin-branch: app-middleware
  • Loading branch information
wchargin authored Oct 7, 2019
1 parent 11d342c commit b17f8b9
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions tensorboard/backend/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ def __init__(self, plugins, path_prefix=''):
key=lambda x: len(x[0]),
reverse=True))

self._app = self._create_wsgi_app()

def _create_wsgi_app(self):
"""Apply middleware to create the final WSGI app."""
app = self._route_request
app = _handling_errors(app)
return app

@wrappers.Request.application
def _serve_plugins_listing(self, request):
Expand Down Expand Up @@ -392,23 +399,31 @@ def _serve_plugins_listing(self, request):
response[plugin.plugin_name] = output_metadata
return http_util.Respond(request, response, 'application/json')

@_handling_errors
def __call__(self, environ, start_response): # pylint: disable=invalid-name
def __call__(self, environ, start_response):
"""Central entry point for the TensorBoard application.
This method handles routing to sub-applications. It does simple routing
using strict string matching. Regular expressions are not supported.
Wildcard routes such as `/foo/*` are supported as a special case.
This __call__ method conforms to the WSGI spec, so that instances of this
class are WSGI applications.
Args:
environ: See WSGI spec.
start_response: See WSGI spec.
environ: See WSGI spec (PEP 3333).
start_response: See WSGI spec (PEP 3333).
"""
return self._app(environ, start_response)

Returns:
A werkzeug Response.
def _route_request(self, environ, start_response):
"""Delegate an incoming request to sub-applications.
This method supports strict string matching and wildcard routes of a
single path component, such as `/foo/*`. Other routing patterns,
like regular expressions, are not supported.
This is the main TensorBoard entry point before middleware is
applied. (See `_create_wsgi_app`.)
Args:
environ: See WSGI spec (PEP 3333).
start_response: See WSGI spec (PEP 3333).
"""
request = wrappers.Request(environ)
parsed_url = urlparse.urlparse(request.path)
Expand Down

0 comments on commit b17f8b9

Please sign in to comment.