Detecting whether a Flask app handles a URL
I have a case for having a preliminary frontend Flask app before continuing to my main app.
I've implemented it using a "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 of doing this, without creating a context just to check whether the URL is handled by the app? I want to maintain the flexibility of keeping two apps.
Each flask.Flask
app
has a url_map
property - which is a werkzeug.routing.Map
. You could just run bind_to_environ
and use the test
method from 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'd call it "more idiomatic" but it is another way of handling your use case.
链接地址: http://www.djcxy.com/p/33518.html上一篇: axios库中的超时功能不起作用
下一篇: 检测Flask应用程序是否处理URL