swagger+Sanic

from sanic import Sanic
from sanic.response import json,file
# from sanic_openapi import swagger_blueprint, doc
from sanic_ext import openapi
from sanic import Blueprint
import os

app = Sanic(__name__)
app.config.API_VERSION = '1'
hello_bp = Blueprint('hello_bp', url_prefix='/hello')

def build_json_schema(properties: dict) -> dict:
    """
    生成openapi请求体的类型提示
    :param properties: {属性名:类型}
    :return:
    """
    return {"application/json": {
        "schema": {"type": "object", "properties": {k: {"type": v} for k, v in properties.items()}}}}

def get_root():
    # 获取当前脚本所在目录的上级目录路径
    current_dir = os.path.dirname(os.path.abspath(__file__))
    root_dir = os.path.dirname(current_dir)
    return root_dir
app.extend(
    config={"oas_ui_default": "swagger",
            "oas_path_to_swagger_html": get_root() + "/docs/swagger/swagger.html",
            "swagger_ui_configuration": {"defaultModelsExpandDepth": -1}})
@hello_bp.route("/",methods=['POST'])
@openapi.body(build_json_schema({"pageId": "integer", "content": "string"}))
async def hello(request):
    print(request.json)
    a=request.json.get("content","")
    return json({'hello': a})

# 配置静态文件路由
app.static('/static', './static')
@app.route("/swagger-src/<file_name>", methods=['GET'])
async def swagger_template_file(request, file_name):
    print(file_name)
    file_path = get_root() + "/docs/swagger/" + file_name
    return await file(file_path)

# app.blueprint(swagger_blueprint)
app.blueprint(hello_bp)
if __name__ == '__main__':
    app.config.API_TITLE="API文档"
    app.run(host='0.0.0.0', port=2658)

swagger.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="/swagger-src/swagger-ui.min.css">
        <title>__HTML_TITLE__</title>
        <style>
            body {
                margin: 0;
                padding: 0;
            }
            __HTML_CUSTOM_CSS__
        </style>
    </head>
    <body>
        <div id="openapi"></div>
        <script src="/swagger-src/swagger-ui-bundle.min.js"></script>
        <script src="/swagger-src/swagger-ui-standalone-preset.min.js"></script>
        <script>
            window.onload = function () {
                const ui = SwaggerUIBundle({
                    url: "__URL_PREFIX__/openapi.json",
                    dom_id: "#openapi",
                    configUrl: "__URL_PREFIX__/swagger-config",
                    deepLinking: true,
                    presets: [
                        SwaggerUIBundle.presets.apis,
                        SwaggerUIStandalonePreset
                    ],
                    plugins: [
                        SwaggerUIBundle.plugins.DownloadUrl
                    ],
                    layout: "StandaloneLayout"
                })
            }
        </script>
    </body>
</html>

swagger-ui.min.css

swagger-ui-bundle.min.js

swagger-ui-standalone-preset.min.js

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值