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中。

 

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐