简体   繁体   English

Python flask 钢筋得到 404

[英]Python flask rebar getting 404

I am getting 404我得到 404

Error:错误:

 curl localhost:5000/api/health
 {"message":"The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."}

application logs:应用日志:

  • Serving Flask app "app" (lazy loading)服务 Flask 应用程序“应用程序”(延迟加载)
  • Environment: production WARNING: Do not use the development server in a production environment.环境:生产警告:不要在生产环境中使用开发服务器。 Use a production WSGI server instead.请改用生产 WSGI 服务器。
  • Debug mode: off调试模式:关闭
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [25/Jan/2022 23:24:05] "GET /api/health HTTP/1.1" 404 -http://127.0.0.1:5000/上运行(按 CTRL+C 退出)127.0.0.1 - - [25/Jan/2022 23:24:05] "GET /api/health HTTP/1.1" 404 -

code repo:代码仓库:

https://github.com/Sytten/flask-rebar-example/tree/v1/app

Use v1 tag:使用 v1 标签:

 git checkout v1

Steps to repro复制步骤

 git clone https://github.com/Sytten/flask-rebar-example.git
 cd flask-rebar-example/
 git checkout v1
 pip install -r requirements.txt 
 cd app
 python app.py
 
 # open new terminal and run below command 
 curl localhost:5000/api/health

Expected results:预期成绩:

{"status": "OK"}

python 3.9.5 version and Mac os python 3.9.5版本和Mac os

app.py应用程序.py

from flask import Flask
from flask_rebar import Rebar


rebar = Rebar()
registry = rebar.create_handler_registry(prefix='/api')


def create_app() -> Flask:
    app = Flask(__name__)
    rebar.init_app(app)
    return app


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

Controller.py Controller.py

from flask_rebar import errors

from .app import registry
from .schemas import HealthSchema


@registry.handles(rule="/health", method="GET", marshal_schema=HealthSchema())
def get_health():
    return {"status": "OK"}

schemas.py架构.py

from marshmallow import fields, Schema


class HealthSchema(Schema):
    status = fields.String()

This has to do with how Python imports modules.这与 Python 如何导入模块有关。

If the controller.py module never gets imported, the get_health endpoint will never get registered.如果controller.py模块永远不会被导入, get_health端点永远不会被注册。

I'm not exactly sure how that example is supposed to be started (here's the associated blog post ), but what you've done ( cd app && python app.py ) won't work.我不确定该示例应该如何开始(这是相关的博客文章),但是您所做的( cd app && python app.py )将不起作用。

Try changing your modules around:尝试改变你的模块:

controller.py controller.py

from rebar import HandlerRegistry
from .schemas import HealthSchema

registry = HandlerRegistry(prefix='/api')

@registry.handles(rule="/health", method="GET", marshal_schema=HealthSchema())
def get_health():
    return {"status": "OK"}

app.py应用程序.py

from rebar import Rebar
from controller import registry

def create_app() -> Flask:
    rebar = Rebar()
    rebar.add_handler_registry(registry)
    app = Flask(__name__)
    rebar.init_app(app)
    return app

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

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

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