Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bottle doesn't call the before_request hook in subapp, ver: latest dev #1140

Closed
academyofzhuang opened this issue Apr 4, 2019 · 0 comments
Closed

Comments

@academyofzhuang
Copy link

In dev version, bottle doesn't call the before_request hook in subapp.
In version 0.12, bottle calls the hook.

Here is the code for testing.

'''
rootapp.py

'''
from bottle import Bottle, hook, run, request
from subapp import subapp

app = Bottle()

app.mount('/subapp/', subapp)

# add hook in rootapp, works in 0.12 and 0.13
# @app.hook('before_request')
# def downcase():
#     path_info = request.environ['PATH_INFO']
#     request.environ['PATH_INFO'] = path_info.lower()

run(
    app=app,
    port='9999',
    reloader=True)


'''

subapp.py

'''

import bottle

subapp = bottle.Bottle()


# add hook in subapp, works only in 0.12, not in 0.13
@subapp.hook('before_request')
def downcase():
    path_info = bottle.request.environ['PATH_INFO']
    bottle.request.environ['PATH_INFO'] = path_info.lower()


@subapp.get('/a')
def a():
    return 'a'

@subapp.get('/bac')
def a():
    return f'{bottle.request.path}'

But a class based decorator works both in 0.12 and 0.13 for subapp

I prefer the hook way, though.

class downcase_path(object):
  def __init__(self, app):
    self.app = app
  def __call__(self, environ, h):
    environ['PATH_INFO'] = environ['PATH_INFO'].lower()
    return self.app(environ,h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant