Injecting a Flask Request into another Flask App

后端 未结 1 1547
滥情空心
滥情空心 2021-01-15 03:26

Is there a way to inject a Flask request object into a different Flask app. This is what I\'m trying to do:

app = flask.Flask(__name__)

@app.route(\'/foo/&l         


        
1条回答
  •  礼貌的吻别
    2021-01-15 03:47

    I wouldn't recommend this method, but this is technically possible by abusing the request stack and rewriting the current request and re-dispatching it.

    However, you'll still need to do some type of custom "routing" to properly set the url_rule, as the incoming request from GCF won't have it (unless you explicitly provide it via the request):

    from flask import Flask, _request_ctx_stack
    from werkzeug.routing import Rule
    
    app = Flask(__name__)
    
    @app.route('/hi')
    def hi(*args, **kwargs):
        return 'Hi!'
    
    def say_hello(request):
        ctx = _request_ctx_stack.top
        request = ctx.request
        request.url_rule = Rule('/hi', endpoint='hi')
        ctx.request = request
        _request_ctx_stack.push(ctx)
        return app.dispatch_request()
    

    0 讨论(0)
提交回复
热议问题