Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.x/docs/book/xml/configsystemoverview.xml

Revision 857 (checked in by rdelon, 3 years ago)

Updated book to reflect [856]

Line 
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="configsystemoverview">
4     <title>Configuration system</title>
5     <para>The CherryPy configuration system provides fine-grained control over how each part of the
6     application should react. You will use it for two reasons:</para>
7     <itemizedlist>
8         <listitem>
9             <para>Web server settings</para>
10         </listitem>
11         <listitem>
12             <para>Enabling filters per path</para>
13         </listitem>
14     </itemizedlist>
15     <para>You will be able to declare the configuration settings either from a file or from a Python
16     dictionary.</para>
17     <para>First of all, let's see how a typical configuration file is defined.</para>
18     <example>
19         <title>Configuration file</title>
20         <programlisting linenumbering="numbered">
21             # The configuration file called myconfigfile.conf
22             [global]
23             server.socket_port=8080
24             server.socket_host=""
25             server.socket_file=""
26             server.socket_queue_size=5
27             server.protocol_version="HTTP/1.0"
28             server.log_to_screen=True
29             server.log_file=""
30             server.reverse_dns=False
31             server.thread_pool=10
32             server.environment="development"
33            
34             [/service/xmlrpc]
35             xmlrpc_filter.on = True
36            
37             [/admin]
38             session_authenticate_filter.on=True
39            
40             [/css/default.css]
41             static_filter.on = True
42             static_filter.file = "data/css/default.css"
43            
44             # From your script...
45             cherrypy.config.update(file="myconfigfile.conf")
46         </programlisting>
47     </example>
48     <para>The settings can also be defined using a python dictionary instead of a file as
49     follows:</para>
50     <example>
51         <title>Configuration dictionary</title>
52         <programlisting linenumbering="numbered">
53       settings = {
54          'global': {
55             'server.socket_port' : 8080,
56             'server.socket_host': "",
57             'server.socket_file': "",
58             'server.socket_queue_size': 5,
59             'server.protocol_version': "HTTP/1.0",
60             'server.log_to_screen': True,
61             'server.log_file': "",
62             'server.reverse_dns': False,
63             'server.thread_pool': 10,
64             'server.environment': "development"
65          },
66          '/service/xmlrpc' : {
67             'xmlrpc_filter.on': True
68          },
69          '/admin': {
70             'session_authenticate_filter.on' :True
71          },
72          '/css/default.css': {
73             'static_filter.on': True,
74             'static_filter.file': "data/css/default.css"
75          }
76       }
77       cherrypy.config.update(settings)
78         </programlisting>
79     </example>
80     <section>
81         <title>Configuration Sections</title>
82         <para>Each section of the configuration refers to an object path; the object path is used to
83         lookup the correct handler for each Request-URI. Therefore when the server receives a
84         Request-URI of <code>/css/default.css</code>, the static filter will handle the request, and
85         the server will actually return the physical file at
86         <filename>data/css/default.css</filename>. Since the path <code>/service/xmlrpc</code> has
87         the XML-RPC filter enabled, all the exposed methods of the object
88         <code>cherrypy.root.service.xmlrpc</code> will be treated as XML-RPC methods.</para>
89         <para>The <code>global</code> entry represents settings which apply outside the request
90         process, including server settings such as the port, the protocol version to use by default,
91         the number of threads to start with the server, etc. This is <emphasis>not</emphasis> the
92         same as the root entry <code>[/]</code>, which maps to cherrypy.root.</para>
93         <para>By default, URI's and object paths are equivalent; however, filters may rewrite the
94         objectPath to produce a different mapping between URI's and handlers. This is necessary, for
95         example, when mounting applications at virtual roots (e.g. serving the object path
96         <code>/welcome</code> at the URI "/users/~rdelon/welcome").</para>
97     </section>
98     <section>
99         <title>Configuration Entries</title>
100         <para>All values in the configuration file must be valid Python values. Strings must be
101         quoted, booleans must be True or False, etc.</para>
102         <section>
103             <title>server.environment</title>
104             <para>The <code>server.environment</code> entry controls how CherryPy should run. Three
105             values are built in:</para>
106             <itemizedlist>
107                 <listitem>
108                     <para>development</para>
109                     <itemizedlist>
110                         <listitem>
111                             <para>log_debug_info_filter is enabled</para>
112                         </listitem>
113                         <listitem>
114                             <para>HTTPErrors (and therefore the default _cp_on_error) display
115                             tracebacks in the browser if errors occur</para>
116                         </listitem>
117                         <listitem>
118                             <para>autoreload is enabled</para>
119                         </listitem>
120                         <listitem>
121                             <para>NotFound errors (404) are listed in the error.log</para>
122                         </listitem>
123                     </itemizedlist>
124                 </listitem>
125                 <listitem>
126                     <para>production</para>
127                     <itemizedlist>
128                         <listitem>
129                             <para>log_debug_info_filter is disabled</para>
130                         </listitem>
131                         <listitem>
132                             <para>tracebacks are logged, but are not displayed in the browser</para>
133                         </listitem>
134                         <listitem>
135                             <para>autoreload is disabled</para>
136                         </listitem>
137                         <listitem>
138                             <para>NotFound errors (404) aren't listed in the error log</para>
139                         </listitem>
140                     </itemizedlist>
141                 </listitem>
142                 <listitem>
143                     <para>staging (same as production for the moment)</para>
144                 </listitem>
145             </itemizedlist>
146             <para>Beginning in CherryPy 2.2, the behavior of each environment is defined in
147             <code>cherrypy.config.environments</code>, a dict whose keys are "development",
148             "production", etc, and whose values are dicts of config keys and values. Application
149             developers are free to modify existing environments, or define new environments for use
150             by their deployers, by modifying this container. For example, if you develop an
151             application which absolutely cannot handle autoreload, your app can set
152             <code>cherrypy.config.environments['development']['autoreload.on'] = False</code>.
153             Deployers who selected the "development" environment would then be free from the danger
154             of autoreload interacting with your application. Another example of using
155             config.environments directly might be an application which needs a "development" and
156             "production" environment, but also separate "beta", "rc", "live data" and/or "testing"
157             environments.</para>
158         </section>
159     </section>
160 </section>
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets