Sanic官翻-版本控制

版本控制

您可以将version关键字传递给路由装饰器或蓝图初始化器。这将导致v {version} url前缀,其中{version}是版本号。

每个路由

您可以将版本号直接传递给路由。

from sanic import response


@app.route('/text', version=1)
def handle_request(request):
    return response.text('Hello world! Version 1')

@app.route('/text', version=2)
def handle_request(request):
    return response.text('Hello world! Version 2')

app.run(port=80)

使用curl测试

curl localhost/v1/text
curl localhost/v2/text

全局路由版本

您还可以将版本号传递给蓝图,该版本号将应用于所有路由。

from sanic import response
from sanic.blueprints import Blueprint

bp = Blueprint('test', version=1)

@bp.route('/html')
def handle_request(request):
    return response.html('<p>Hello world!</p>')

使用curl测试

curl localhost/v1/html
posted @ 2021-05-13 11:11  fhkankan  阅读(54)  评论(0编辑  收藏  举报