简体   繁体   English

无法运行由 swagger codegen 生成的服务器

[英]failed to run the server that generated by swagger codegen

I am learning to develop restful api for machine learning service.我正在学习为机器学习服务开发宁静的 api。 I used openapi spec to define my api specification, and I tried to use flask rest for this.我使用 openapi 规范来定义我的 api 规范,并为此尝试使用 flask rest。 However, I find this medium post is relevant to my intention, so I downloaded source code and tried to run the server locally, but server endpoint failed.但是,我发现这篇中等帖子与我的意图相关,所以我下载了源代码并尝试在本地运行服务器,但服务器端点失败。 I could able to run my other flask app on the same endpoint, but this project is not working even I followed its instruction.我可以在同一个端点上运行我的其他 flask 应用程序,但是即使我按照它的说明进行操作,这个项目也无法正常工作。 I created my open api spec file so I want to learn and understand this medium post by run server locally.我创建了我的open api spec文件,所以我想通过在本地运行服务器来学习和理解这篇中型文章 Can anyone point me out what's going on this medium post source code ?谁能指出我这个中等帖子源代码发生了什么? any quick solution to figure out how to run server locally?任何快速解决方案来弄清楚如何在本地运行服务器? thanks谢谢

for me, a simple flask app works and can run it on server endpoint.对我来说,一个简单的 flask 应用程序可以工作并且可以在服务器端点上运行它。

from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.run()

I want to run the server from this APIs with OpenAPI , because I want to use same structure for developing my api.我想使用 OpenAPI 从这个 API运行服务器,因为我想使用相同的结构来开发我的 api。 Can anyone point me out why I can't run a server?谁能指出我为什么不能运行服务器? any idea?任何想法?

update更新

in this medium post , all artifacts generated by openapi code generator, and structured out by openapi spec file, so able to run the server from its source code will help me understand how to proceed my workflow.这篇中等文章中,所有由 openapi 代码生成器生成的工件,并由 openapi 规范文件结构化,因此能够从其源代码运行服务器将帮助我了解如何继续我的工作流程。

The examples in the repository you mention come with docker files.您提到的存储库中的示例带有 docker 文件。 You could use Docker to quickly spin up the server.您可以使用 Docker 来快速启动服务器。

cd api_tutorial/openapi/photo_album/codegen_server
docker build . -t codegen_server
docker run codegen_server

However, this does not work.但是,这不起作用。

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/src/app/openapi_server/__main__.py", line 3, in <module>
    import connexion
  File "/usr/local/lib/python3.8/site-packages/connexion/__init__.py", line 3, in <module>
    from .apis import AbstractAPI  # NOQA
  File "/usr/local/lib/python3.8/site-packages/connexion/apis/__init__.py", line 1, in <module>
    from .abstract import AbstractAPI  # NOQA
  File "/usr/local/lib/python3.8/site-packages/connexion/apis/abstract.py", line 16, in <module>
    from ..operations import OpenAPIOperation, Swagger2Operation
  File "/usr/local/lib/python3.8/site-packages/connexion/operations/__init__.py", line 1, in <module>
    from .abstract import AbstractOperation  # noqa
  File "/usr/local/lib/python3.8/site-packages/connexion/operations/abstract.py", line 11, in <module>
    from ..decorators.response import ResponseValidator
  File "/usr/local/lib/python3.8/site-packages/connexion/decorators/response.py", line 12, in <module>
    from .validation import ResponseBodyValidator
  File "/usr/local/lib/python3.8/site-packages/connexion/decorators/validation.py", line 9, in <module>
    from werkzeug import FileStorage
ImportError: cannot import name 'FileStorage' from 'werkzeug' (/usr/local/lib/python3.8/site-packages/werkzeug/__init__.py)

As pointed out on this Github issue, this issue is related to connexion and does not happen with version 2.6.0 .正如在connexion问题中所指出的,此问题与连接有关,并且不会在2.6.0版本中发生。 So, let's update requirements.txt to use this version instead.所以,让我们更新requirements.txt以使用这个版本。

connexion == 2.6.0

Build and run again with docker and voilá, it should work.使用 docker 再次构建并运行,瞧,它应该可以工作了。

docker run codegen_server
 * Serving Flask app "__main__" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

You could also run the server without Docker by cd-ing into the root directory and doing.你也可以在没有 Docker 的情况下通过 cd-ing 进入根目录并执行操作来运行服务器。

python -m openapi_server

Which in fact, is exactly what docker does, as we can see in the Dockerfile.事实上,这正是 docker 所做的,正如我们在 Dockerfile 中看到的那样。

...

ENTRYPOINT ["python3"]
CMD ["-m", "openapi_server"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM