Browse Source

Restructure cherrypy application more correctly

Specifically, switch from using global configuration and several apps,
to using application-specific configuration with a single app.  This
should hopefully make it easier to plug this into another
WSGI-compliant server someday, and also silences some startup warnings
about missing application configs.
tags/nilmdb-1.0
Jim Paris 10 years ago
parent
commit
f6a2c7620a
1 changed files with 17 additions and 8 deletions
  1. +17
    -8
      nilmdb/server/server.py

+ 17
- 8
nilmdb/server/server.py View File

@@ -391,25 +391,32 @@ class Server(object):
# into it from separate threads.
self.embedded = embedded
self.db = nilmdb.utils.Serializer(db)

# Build up global server configuration
cherrypy.config.update({
'server.socket_host': host,
'server.socket_port': port,
'engine.autoreload_on': False,
'server.max_request_body_size': 4*1024*1024,
'error_page.default': self.json_error_page,
})
if self.embedded:
cherrypy.config.update({ 'environment': 'embedded' })

# Build up application specific configuration
app_config = {}
app_config.update({
'error_page.default': self.json_error_page,
})

# Send a permissive Access-Control-Allow-Origin (CORS) header
# with all responses so that browsers can send cross-domain
# requests to this server.
cherrypy.config.update({ 'response.headers.Access-Control-Allow-Origin':
'*' })
app_config.update({ 'response.headers.Access-Control-Allow-Origin':
'*' })

# Send tracebacks in error responses. They're hidden by the
# error_page function for client errors (code 400-499).
cherrypy.config.update({ 'request.show_tracebacks' : True })
app_config.update({ 'request.show_tracebacks' : True })
self.force_traceback = force_traceback

# Patch CherryPy error handler to never pad out error messages.
@@ -417,11 +424,13 @@ class Server(object):
# error messages.
cherrypy._cperror._ie_friendly_error_sizes = {}

cherrypy.tree.apps = {}
cherrypy.tree.mount(Root(self.db, self.version), "/")
cherrypy.tree.mount(Stream(self.db), "/stream")
# Build up the application and mount it
root = Root(self.db, self.version)
root.stream = Stream(self.db)
if stoppable:
cherrypy.tree.mount(Exiter(), "/exit")
root.exit = Exiter()
cherrypy.tree.apps = {}
cherrypy.tree.mount(root, "/", config = { "/" : app_config })

# Shutdowns normally wait for clients to disconnect. To speed
# up tests, set fast_shutdown = True


Loading…
Cancel
Save