我正在尝试运行一个简单的连接 REST-API。我已经实例化了 connexion on 的实例run.py
。
import os
import connexion
from flask import render_template
from users import read
app = connexion.App(__name__, specification_dir='./')
app.add_api('swagger.yaml')
@app.route('/')
def home():
return 'Welcome to users API'
if __name__ == 'main':
app.run(port='5000', host='0.0.0.0', debug=True)
服务器的 Swagger 规范swagger.yaml
swagger: "2.0"
info:
description: Auth users details
version: "1.0.0"
title: Swagger specification for the Auth contracts
consumes:
- "application/json"
produces:
- "application/json"
basePath: "/api"
paths:
/users:
get:
operationId: "users.read"
tags:
- "Users"
summary: "The users data structure supported by the server application"
description: "Read the list of users"
responses:
200:
description: "Successful read users list operation"
schema:
type: "array"
items:
properties:
fname:
type: "string"
lname:
type: "string"
timestamp:
type: "string"
应用程序的用户详细信息。users.py
from datetime import datetime
def get_timestamp():
return datetime.now().strftime(("%Y-%m-%d %H:%M:%S"))
users = {
'Tom': {
'fname': 'Tom',
'lname': 'Riddle',
'timestamp': get_timestamp()
},
'Harry': {
'fname': 'Harry',
'lname': 'Potter',
'timestamp': get_timestamp()
},
'Dumbledore': {
'fname': 'Brian',
'lname': 'Dumbledore',
'timestamp': get_timestamp()
},
}
def read():
return [users[key] for key in sorted(users.keys())]
为了延迟启动,我创建了 shell 文件server.sh
export FLASK_APP=run.py
export FLASK_DEBUG=1
flask run
应用程序的文件夹结构。
W/D
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── people.cpython-37.pyc
│ ├── run.cpython-37.pyc
│ └── users.cpython-37.pyc
├── run.py
├── server.sh
├── swagger.yaml
└── users.py
当我运行外壳时,它给出了这个错误..
flask.cli.NoAppException: Failed to find Flask application or factory in module "run". Use "FLASK_APP=run:name to specify one.
这里可能是什么问题?