Search code examples
pythonconfigurationvirtualhostcherrypy

Cherrypy virtual hosts - Separate application configuration


I'd like to place two cherrypy applications on different domains on the same IP. How to provide separate configuration files for these sites using cherrypy virtualhost dispatcher, not hiding cherrypy web server behind Apache?

import cherrypy
from cherrypy import expose

class Root(object):
    @expose
    def index(self):
        return "The Main Page" + \
          '<br /><a href="/rs/file.txt">File from the Main app</a>'

class SecondApp(object):
    @expose
    def index(self):
        return "Page on Subdomain" + \
          '<br /><a href="/rs/file.txt">Another file from the subdomain app</a>'

def main():
    cherrypy.config.update('global.conf')

    conf = {"/": {"request.dispatch": cherrypy.dispatch.VirtualHost(
                    **{ "sub.domain.local": "/secondapp" } )
                 }
           }

    root = Root()
    root.secondapp = SecondApp()
    app=cherrypy.tree.mount(root, "/", conf)
    app.merge('some.additional.configuration')
    cherrypy.engine.start()
    cherrypy.engine.block()

if __name__ == "__main__": main()

Configuration defined by 'conf' vocabulary has effect on both my domains 'domain.local' and 'sub.domain.local'. How can I insert here a separate configuration for the app "secondapp"?


Solution

  • You will need to change your main slightly. You have to add another configuration section, and then you use cherrypy.tree.mount to add it with it's conf object.

    def main():
        conf = {"/": {"request.dispatch": cherrypy.dispatch.VirtualHost(
                        **{ "sub.domain.local": "/secondapp" } )
                     }
               }
        secondapp_conf = {
            ... Config Items for Second App ....
            }
        }
    
        root = Root()
        root.secondapp = SecondApp()
        cherrypy.tree.mount(root, "/", conf)
        cherrypy.tree.mount(root.secondapp, "/secondapp", secondapp_conf)
        cherrypy.engine.start()
        cherrypy.engine.block()