Detecting if a Flask application is processing a URL

I have an example of having a frontend Flash app before continuing with its main application.

I implemented it using the middleware pattern:

class MyMiddleware(object): def __init__(self, main_app, pre_app): self.main_app = main_app self.pre_app = pre_app def __call__(self, environ, start_response): # check whether pre_app has a rule for this URL with self.pre_app.request_context(environ) as ctx: if ctx.request.url_rule is None: return self.main_app(environ, start_response) return self.pre_app(environ, start_response) 

Is there a more idiomatic way to do this without creating a context to check if the application is being processed by the application? I want to maintain the flexibility of storing two applications.

+7
python flask werkzeug
source share
1 answer

Each flask.Flask app has the url_map property - this is werkzeug.routing.Map . You can simply run bind_to_environ and use the test method from the MapAdapter :

 if self.pre_app.url_map.bind_to_environ(environ).test(environ['PATH_INFO']): return self.pre_app(environ, start_response) return self.main_app(environ, start_response) 

I donโ€™t know that I would call it โ€œmore idiomatic,โ€ but this is a different way of handling your use case.

+3
source share

All Articles