Skip to content

Instantly share code, notes, and snippets.

@earthday
Last active November 23, 2019 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earthday/1519786f57677388a7fd3aaa2480251b to your computer and use it in GitHub Desktop.
Save earthday/1519786f57677388a7fd3aaa2480251b to your computer and use it in GitHub Desktop.
import logging
import cherrypy
from cherrypy.process import plugins
class ADPHttpFacetPlugin(plugins.SimplePlugin):
def __init__(self, bus):
plugins.SimplePlugin.__init__(self, bus)
self.logger = logging.getLogger(__name__)
def start(self):
self.bus.log('Starting up ADP HTTP facet')
self.bus.subscribe("before_request", self.before_request)
self.bus.subscribe("after_request", self.after_request)
def stop(self):
self.bus.log('Stopping down DB access')
self.bus.unsubscribe("before_request", self.before_request)
self.bus.unsubscribe("after_request", self.after_request)
def before_request(self):
self.logger.info("before_request")
print("before_request")
def after_request(self):
self.logger.info("after_request")
print("after_request")
class ModelController(object):
@cherrypy.expose
def get_model(self, model_id):
return "Your model is {}".format(model_id)
@cherrypy.expose
def post_model(self, model_id, model_name):
return "{} : {}".format(model_id, model_name)
if __name__ == "__main__":
model_controller = ModelController()
d = cherrypy.dispatch.RoutesDispatcher()
d.connect("model", '/model/:model_id', controller=model_controller,
action='get_model',
conditions=dict(
method=["GET", "DELETE"]
))
d.connect("model", '/model/:model_id/name/:model_name', controller=model_controller,
action='post_model',
conditions=dict(
method=["POST"]
))
conf = {'/': {'request.dispatch': d}}
ADPHttpFacetPlugin(cherrypy.engine).subscribe()
cherrypy.quickstart(None, '/', config=conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment