Forums

Setting up RestAPI in WSGI from CherryPy

I've been poking around on the forum, reading about using CherryPy on PythonAnywhere, but I'm still not sure how to convert my rest api endpoints set up in Cherry Py to be exposed through a wsgi interface.

https://help.pythonanywhere.com/pages/UsingCherryPy/ doesn't seem to show how multiple endpoints are exposed.

https://www.pythonanywhere.com/forums/topic/11058/ talks about exposing static content through cherry py

https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html doesn't show how to expose multiple endpoints.

Essentially, I'm looking for the WSGI equivalent/implementation of this CherryPy snippet

cherrypy.tree.mount(Root(), '/', "static.conf")
cherrypy.tree.mount(Totals(), '/totals', "app.conf")
cherrypy.tree.mount(Source(), '/transactions', "app.conf")

Any tips on pointing me in the right direction?

Thanks,Robert

I'm not a CherryPy expert, but that looks like a simple implementation of what you want (I added a simple config for the index view; it's assuming that /static/css/style.css exists in the staticdir.root and that static files mappings are set on the Web app page). I kept everyting from the example on our help page, but replaced the Root class and added config dict.

config = {"/": {"tools.staticdir.root": "/path/to/the/dir/with/static"}}


class Root(object):
    @cherrypy.expose
    def index(self):
        return """<html>
        <head>
            <link href="/static/css/style.css" rel="stylesheet"></head>
        <body>
            <h1>Hello</h1>
            World
        </body>
        </html>
        """

    @cherrypy.expose
    def totals(self):
        return "Hello from totals!"

    @cherrypy.expose
    def transactions(self):
        return "Hello from transactions!"

application = cherrypy.Application(Root(), "", config=config)