简体   繁体   中英

Python flask rebar getting 404

I am getting 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)
  • Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead.
  • 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 -

code repo:

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

Use v1 tag:

 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

app.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

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

from marshmallow import fields, Schema


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

This has to do with how Python imports modules.

If the controller.py module never gets imported, the get_health endpoint will never get registered.

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.

Try changing your modules around:

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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