gunicorn + flask + swagger + connexion 进行python后端开发

gunicorn提供一个更稳健的WSGI的并发管理。

参考文章:https://realpython.com/flask-connexion-rest-api/

这几个组件的功能如下:

1. gunicorn负责WSGI管理,处理并发相应

2. flask和connexion生成app:

import connexion
from flask_cors import CORS

app = connexion.App(__name__, 
                    specification_dir=APP_DIR + '/swagger/',
                    options={"swagger_ui": True})                    

app.add_api('swagger.yaml')

CORS(app.app)

3. swagger提供api管理和生成文档:使用swagger-ui.yaml定义每个api的入参和出参。

4. 利用flask中的render_template渲染前端网页。

代码server.py, swagger-ui.yaml,model.py。

server.py中启动app,并加入一个网页渲染。

from flask import render_template
import connexion

# Create the application instance
app = connexion.App(__name__, specification_dir='./')

# Read the swagger.yml file to configure the endpoints
app.add_api('swagger.yml')

# Create a URL route in our application for "/"
@app.route('/')
def home():
    """
    This function just responds to the browser ULR
    localhost:5000/
    :return:        the rendered template 'home.html'
    """
    return render_template('home.html')

# If we're running in stand alone mode, run the application
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

所有swagger中定义的api的逻辑则放到model.py中。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值