CherryPy Recipes / deployment / multi_apps / __init__.py

# -*- coding: utf-8 -*-
""" CherryPy can hold several applications in one given instance
of the server.

To do so use ``cherrypy.tree.mount`` which associates
an instance of an application with a URL path segment.

That path is the root point at which the application
will be reachable.

So if your app is mounted at /blog, requests to
/forum will not reach anything. Only requests
starting with /blog. 

``cherrypy.tree.mount`` has the interesting
property of sandboxing your applications
one from the other.

So for instance:

    cherrypy.tree.mount(App1(), '/app1', {'/': {'tools.etags.on': True}})
    cherrypy.tree.mount(App2(), '/app2', {'/': {'tools.etags.on': False}})
    
Both applications won't be aware of the other.
This means that the provided configurations
are per application.

Note that configuration must be relative to
root point where the apps are mounted.
This is why we use the '/' configuration section
since the configuration is applied to
the application independantly from where
it was mounted.
"""
import cherrypy

class App1(object):
    @cherrypy.expose
    def index(self):
        return "Hello world from app1"
    
class App2(object):
    @cherrypy.expose
    def index(self):
        return "Hello world from app2"

if __name__ == '__main__':
    cherrypy.config.update({'server.socket_port': 8090})

    cherrypy.tree.mount(App1(), '/app1', {'/': {'tools.etags.on': True}})
    cherrypy.tree.mount(App2(), '/app2', {'/': {'tools.etags.on': False}})
    
    cherrypy.engine.start()
    cherrypy.engine.block()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.