Bottle Plugin Lifecycle Test
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import bottle | |
import logging | |
logger = logging.getLogger(__name__) | |
logger.addHandler(logging.StreamHandler()) | |
logger.setLevel(logging.DEBUG) | |
logger.info("module load") | |
class LifecycleTestPlugin(object): | |
name = "lifecycle_test" | |
api = 2 | |
def __init__(self, name): | |
self.name = name | |
logger.info("%s: plugin __init__", self.name) | |
def setup(self, app): | |
logger.info("%s: plugin setup", self.name) | |
def apply(self, callback, context): | |
logger.info("%s: plugin apply", self.name) | |
def _wrapper(*args, **kwargs): | |
logger.info("%s: plugin apply wrapper", self.name) | |
return callback(*args, **kwargs) | |
return _wrapper | |
def close(self): | |
logger.info("plugin close %s", self.name) | |
app = bottle.Bottle() | |
logger.info("installing plugins ...") | |
app.install(LifecycleTestPlugin("app_plugin1")) | |
app.install(LifecycleTestPlugin("app_plugin2")) | |
@app.get('/ping', | |
apply=[ | |
LifecycleTestPlugin("route_plugin1"), | |
LifecycleTestPlugin("route_plugin2"), | |
] | |
) | |
def ping(): | |
return 'pong' | |
if __name__ == "__main__": | |
logger.info("start app ...") | |
bottle.run(app, host="0.0.0.0", port="9000", reloader=True) |
module load | |
installing plugins ... | |
app_plugin1: plugin __init__ | |
app_plugin1: plugin setup | |
app_plugin2: plugin __init__ | |
app_plugin2: plugin setup | |
route_plugin1: plugin __init__ | |
route_plugin2: plugin __init__ | |
start app ... | |
module load | |
installing plugins ... | |
app_plugin1: plugin __init__ | |
app_plugin1: plugin setup | |
app_plugin2: plugin __init__ | |
app_plugin2: plugin setup | |
route_plugin1: plugin __init__ | |
route_plugin2: plugin __init__ | |
start app ... | |
Bottle v0.12.8 server starting up (using WSGIRefServer())... | |
Listening on http://0.0.0.0:9000/ | |
Hit Ctrl-C to quit. | |
route_plugin2: plugin apply | |
route_plugin1: plugin apply | |
app_plugin2: plugin apply | |
app_plugin1: plugin apply | |
app_plugin1: plugin apply wrapper | |
app_plugin2: plugin apply wrapper | |
route_plugin1: plugin apply wrapper | |
route_plugin2: plugin apply wrapper | |
127.0.0.1 - - [05/Jul/2015 14:07:25] "GET /ping HTTP/1.1" 200 4 | |
app_plugin1: plugin apply wrapper | |
app_plugin2: plugin apply wrapper | |
route_plugin1: plugin apply wrapper | |
route_plugin2: plugin apply wrapper | |
127.0.0.1 - - [05/Jul/2015 14:07:28] "GET /ping HTTP/1.1" 200 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment