0

I am a little struggling with getting CherryPy to both host static files and my REST api. I want the API to be on http://127.0.0.1:8080/api, and any static files on http://127.0.0.1:8080/. When a user just GETs the /api URL, I want it to serve a static HTML with a manual on how to use the API. The static html on the / URL is a HTML5 app; a base implementation of the REST API.

What I have for now is this:

cherrypy.config.update({"tools.staticdir.on": True })
cherrypy.config.update({"tools.staticdir.dir": "/home/bart/html" })
#cherrypy.config.update({"tools.staticdir.index": "index.html"})

if __name__ == '__main__':  
    cherrypy.tree.mount(myApp(), "/api")

    cherrypy.engine.start()
    cherrypy.engine.block()

And it is almost there, except when I GET http://127.0.0.1:8080/api, it serves the same as on /. The index of the myApp() class is as follows:

@cherrypy.expose
def index(self):
    # TODO: place API docs here
    if cherrypy.request.method == "GET":    
        return "REST API v{v}".format(v=VERSION)
    else:
        return result(405)
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • have a look at this http://stackoverflow.com/a/15789415/5476782 I think your definition needs another page handler for the static files you want in `/` – Evhz May 07 '17 at 13:18

1 Answers1

0

I got it working by creating a separate configuration, for an empty application:

cherrypy.tree.mount(myApp(), "/api")    
cherrypy.tree.mount(None, "/", {'/':
    {"tools.staticdir.on": True ,
    "tools.staticdir.dir": "/home/bart/html" ,
    "tools.staticdir.index": "index.html"
    } } )
cherrypy.engine.start()
cherrypy.engine.block()
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195