无法启动 flask +连接器+ Swagger

sauutmhj  于 2024-01-08  发布在  其他
关注(0)|答案(1)|浏览(315)

问题

我启动了一个Flask应用程序(+ Connexion和Swagger UI),并试图打开http://127.0.0.1:5000/api/ui。浏览器显示starlette.exceptions.HTTPException: 404: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

设置

  1. % pip install "connexion[flask, swagger-ui]"
  2. % export FLASK_APP="app"
  3. (Prepare files)
  4. % flask run --debug
  5. (Access http://127.0.0.1:5000/api/ui)

字符串
结果

  • Python 3.12.0
  • 连接器3.0.2
  • Flask 3.0.0
  • swagger_ui_bundle 1.1.0
  • Werkzeug 3.0.1

文件

目录结构

  1. app/
  2. __init__.py
  3. openapi.yaml
  4. hello.py


__init__.py

  1. from connexion import FlaskApp
  2. from flask.app import Flask
  3. from pathlib import Path
  4. BASE_DIR = Path(__file__).parent.resolve()
  5. def create_app() -> Flask:
  6. flask_app: FlaskApp = FlaskApp(__name__)
  7. app: Flask = flask_app.app
  8. flask_app.add_api("openapi.yaml")
  9. return app


openapi.yaml

  1. openapi: 3.0.3
  2. info:
  3. title: "test"
  4. description: "test"
  5. version: "1.0.0"
  6. servers:
  7. - url: "/api"
  8. paths:
  9. /hello:
  10. get:
  11. summary: "hello"
  12. description: "hello"
  13. operationId: "hello.say_hello"
  14. responses:
  15. 200:
  16. description: "OK"
  17. content:
  18. text/plain:
  19. schema:
  20. type: string
  21. example: "hello"


hello.py

  1. def say_hello() -> str:
  2. return 'Hello, world!'

错误信息

基于这些设置,我相信我可以在http://127.0.0.1:5000/api/ui上看到Swagger UI。然而,我遇到了下面的错误消息。

  1. Traceback (most recent call last):
  2. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/app.py", line 867, in full_dispatch_request
  3. rv = self.dispatch_request()
  4. ^^^^^^^^^^^^^^^^^^^^^^^
  5. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/app.py", line 841, in dispatch_request
  6. self.raise_routing_exception(req)
  7. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/app.py", line 450, in raise_routing_exception
  8. raise request.routing_exception # type: ignore
  9. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/ctx.py", line 353, in match_request
  10. result = self.url_adapter.match(return_rule=True) # type: ignore
  11. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  12. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/werkzeug/routing/map.py", line 624, in match
  13. raise NotFound() from None
  14. werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
  15. During handling of the above exception, another exception occurred:
  16. Traceback (most recent call last):
  17. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/app.py", line 1478, in __call__
  18. return self.wsgi_app(environ, start_response)
  19. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/app.py", line 1458, in wsgi_app
  21. response = self.handle_exception(e)
  22. ^^^^^^^^^^^^^^^^^^^^^^^^
  23. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/app.py", line 1455, in wsgi_app
  24. response = self.full_dispatch_request()
  25. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  26. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/app.py", line 869, in full_dispatch_request
  27. rv = self.handle_user_exception(e)
  28. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  29. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/flask/app.py", line 759, in handle_user_exception
  30. return self.ensure_sync(handler)(e)
  31. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  32. File "/Users/myusername/tmp/.venv/lib/python3.12/site-packages/connexion/apps/flask.py", line 245, in _http_exception
  33. raise starlette.exceptions.HTTPException(exc.code, detail=exc.description)
  34. starlette.exceptions.HTTPException: 404: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

jv4diomz

jv4diomz1#

TL:DR:您需要一个ASGI服务器来运行应用程序。
在connectorx 1 e0f1x上也有类似的问题。
这只会引导你到运行你的应用程序的文档,可以在这里找到。
考虑到上述情况,我设法创建了自己的解决方案:
__init__.py

  1. from connexion import FlaskApp
  2. def create_app():
  3. app = FlaskApp(__name__)
  4. app.add_api("openapi.yaml", validate_responses=True)
  5. return app
  6. app = create_app()

字符串
openapi.yaml

  1. openapi: 3.0.3
  2. info:
  3. title: test
  4. description: test
  5. version: 1.0.0
  6. paths:
  7. /hello:
  8. get:
  9. summary: hello
  10. description: hello
  11. operationId: app.hello.say_hello
  12. responses:
  13. 200:
  14. description: OK
  15. content:
  16. text/plain:
  17. schema:
  18. type: string
  19. example: hello


有关其他帮助,请参阅下面我使用的Docker设置:
docker-compose.yaml

  1. version: '3.8'
  2. services:
  3. test-api:
  4. build:
  5. context: .
  6. dockerfile: Dockerfile
  7. restart: unless-stopped
  8. env_file:
  9. - .env
  10. ports:
  11. - '8000:8000'
  12. volumes:
  13. - ./app:/usr/src/app/app


Dockerfile

  1. FROM public.ecr.aws/lambda/python:3.10
  2. RUN mkdir -p /usr/src/app
  3. WORKDIR /usr/src/app
  4. COPY requirements.txt /usr/src/app/
  5. RUN pip3 install --no-cache-dir -r requirements.txt
  6. ENV FLASK_RUN_HOST=0.0.0.0
  7. ENV FLASK_RUN_PORT=8000
  8. EXPOSE 8000
  9. COPY entrypoint.sh /usr/src/app/entrypoint.sh
  10. RUN chmod +x /usr/src/app/entrypoint.sh
  11. ENTRYPOINT ["/usr/src/app/entrypoint.sh"]


entrypoint.sh

  1. #!/bin/sh
  2. gunicorn -w 1 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 app:app --reload
  3. # Keep the script running to keep the container alive
  4. tail -f /dev/null


requirements.txt

  1. connexion[flask, swagger-ui, uvicorn]==3.0.2
  2. gunicorn==21.2.0
  3. Werkzeug==3.0.1
  4. Flask==3.0.0
  5. setuptools >= 21.0.0
  6. swagger-ui-bundle==1.1.0


项目结构:

  1. project-root/
  2. |-- app/
  3. | |-- __init__.py
  4. | |-- hello.py
  5. | |-- openapi.yaml
  6. |-- Dockerfile
  7. |-- docker-compose.yml
  8. |-- entrypoint.sh
  9. |-- requirements.txt
  10. |-- .env


希望这对你有帮助!我建议从这个工作点开始使用特定的设置,例如CORS,数据库设置,迁移,修改Docker设置等。

展开查看全部

相关问题