| 1 |
<?xml version="1.0" encoding="UTF-8"?> |
|---|
| 2 |
<section xmlns:db="http://docbook.org/docbook-ng" xmlns:xi="http://www.w3.org/2001/XInclude" |
|---|
| 3 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xml:id="configureapplication"> |
|---|
| 4 |
<title>Mounting applications</title> |
|---|
| 5 |
<para>So far, we have talked about applications as if they are always "mounted" at root; that is, |
|---|
| 6 |
that the URL "/" is the "base URL" for the application. However, this rarely happens in practice. |
|---|
| 7 |
Often, you not only have an application mounted at some other base URL, but it must coexist with |
|---|
| 8 |
other applications (perhaps in the same process). CherryPy has always allowed applications to be |
|---|
| 9 |
deployed simultaneously, but it was often a difficult process, and required a lot of manual |
|---|
| 10 |
manipulation of the <code>cherrypy.root</code> tree, and of config file paths.</para> |
|---|
| 11 |
<para> Beginning in version 2.2, CherryPy provides a tool to make mounting applications easier: |
|---|
| 12 |
<code>cherrypy.tree.mount(app_root, baseurl=None, conf=None)</code>. You pass it a handler tree, |
|---|
| 13 |
the base URL for the app, and a config dict or filename, and it does all of the "hard work" for |
|---|
| 14 |
you. For example, instead of writing this:</para> |
|---|
| 15 |
<example> |
|---|
| 16 |
<programlisting>import cherrypy |
|---|
| 17 |
|
|---|
| 18 |
class Root: |
|---|
| 19 |
def index(self): |
|---|
| 20 |
return "Hello world! This is %s" % cherrypy.request.path |
|---|
| 21 |
index.exposed = True |
|---|
| 22 |
|
|---|
| 23 |
cherrypy.root.path.to.approot = Root() |
|---|
| 24 |
cherrypy.config.update({'/path/to/approot/': |
|---|
| 25 |
{'server.log_file': '/var/log/myapp.log'} |
|---|
| 26 |
})</programlisting> |
|---|
| 27 |
</example> |
|---|
| 28 |
<para>...you can now write the last two lines like this:</para> |
|---|
| 29 |
<example> |
|---|
| 30 |
<programlisting>cherrypy.tree.mount(Root(), "/path/to/approot", |
|---|
| 31 |
{'/': {'server.log_file': '/var/log/myapp.log'}})</programlisting> |
|---|
| 32 |
</example> |
|---|
| 33 |
<para>The call to mount() will prefix all of the config-section paths with your mount point path. |
|---|
| 34 |
If you use a config file instead of a Python dict, it becomes even cleaner.</para> |
|---|
| 35 |
<para>You can read more about the <code>cherrypy.tree</code> object in the API Reference later in |
|---|
| 36 |
this book.</para> |
|---|
| 37 |
</section> |
|---|