Flask-fakeStatic: url rule 伪静态扩展

2018-04-01  本文已影响0人  LonelyT_T

Source Code:flask-fakeStatic.
Yo...Star the repo if it does help you

from flask import Flask, current_app, jsonify


class FakeStatic(object):
    skip_endpoints = {"static", }

    def __init__(self, app=None, skip_endpoints=None):
        if skip_endpoints is not None:
            self.skip_endpoints |= set(skip_endpoints)
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        for rule in app.url_map.iter_rules():
            if rule.endpoint in self.skip_endpoints:
                continue
            rule.rule += ".html"
            rule.refresh()


app = Flask(__name__)


@app.route("/index")
def index():
    return "index"


@app.route('/routes')
def routes():

    arr = [rule.endpoint for rule in current_app.url_map.iter_rules()]
    return jsonify({"total": len(arr), "data": sorted(arr)})

# stay here, after all rules registered
FakeStatic(app, skip_endpoints=["routes"])
if __name__ == "__main__":
    app.run(debug=True)
上一篇下一篇

猜你喜欢

热点阅读