Search code examples
pythonconfigurationcherrypystatic-files

Serving static files with CherryPy ignores mount point?


I'm trying to server static content for my CherryPy app, and everything works fine when the app is mounted at the root:

cherrypy.quickstart(root=Controller(), config="../app.conf")

with the following app.conf:

[/]
tools.staticdir.root: os.path.abspath("../public")
tools.encode.on: False
tools.gzip.on: True
tools.gzip.mime_types: ['text/html', 'text/plain', 'application/json',    'text/javascript', 'application/javascript']
tools.staticdir.debug: True

[/js]
tools.staticdir.on: True
tools.staticdir.dir: 'js'

[/css]
tools.staticdir.on: True
tools.staticdir.dir: 'css'

[/images]
tools.staticdir.on: True
tools.staticdir.dir: 'images'

However, when replacing the cherrypy.quickstart call with the following (note the script_name):

app1 = cherrypy.tree.mount(root=Controller(), config="../app.conf", script_name="/myapp")
cherrypy.engine.start()
cherrypy.engine.block()

the dynamic urls (i.e. the cherrypy "route" methods) are correctly redirected to /myapp/[method name] but static files are still server from the root URL. How can I get the static file serving to automatically use the new mount point? Can I use the new mount point while still referring to the relative static content folder filepaths?


Solution

  • This is how I solved it:

    [/]
    tools.staticdir.root: os.path.abspath("public/")
    tools.encode.on: False
    tools.gzip.on: True
    tools.gzip.mime_types: ['text/html', 'text/plain', 'application/json', 'text/javascript', 'application/javascript']
    
    [/static]
    tools.etags.on: True
    tools.staticdir.on: True
    tools.staticdir.dir: "public"
    
    [/js]
    tools.staticdir.on: True
    tools.staticdir.dir: 'js'
    
    [/css]
    tools.staticdir.on: True
    tools.staticdir.dir: 'css'
    
    [/images]
    tools.staticdir.on: True
    tools.staticdir.dir: 'images'