Flask实现红图

红图流程图:
redprint.png

代码实现:
root.app.py

from flask import Flask


def register_blueprints(app):
    from api.v1 import create_blueprint_v1

    app.register_blueprint(create_blueprint_v1(), url_prefix="/v1")


app = Flask(__name__)

register_blueprints(app)

if __name__ == '__main__':
    app.run()

root.redprint.py

class Readprint(object):
    def __init__(self, name):
        self.name = name
        self.mound = []

    def route(self, rule, **options):
        def decorator(f):
            self.mound.append((f, rule, options))
            return f

        return decorator

    def register(self, bp, url_prefix=None):
        if url_prefix is None:
            url_prefix = "/" + self.name

        for f, rule, options in self.mound:
            endpoint = options.pop("endpoint", f.__name__)
            bp.add_url_rule(url_prefix + rule, endpoint, f, **options)

root.api.v1.__init__.py

from flask import Blueprint

from api.v1 import book


def create_blueprint_v1():
    bp_v1 = Blueprint("v1", __name__)

    book.api.register(bp_v1)

    return bp_v1

root.api.v1.book.py

from redprint import Readprint

api = Readprint("book")


@api.route("/get")
def get():
    return "get book"
Last modification:September 9th, 2019 at 10:51 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment