Browse Source

Add WSGI application support and documentation

tags/nilmdb-1.4.4
Jim Paris 9 years ago
parent
commit
6af3a6fc41
4 changed files with 36 additions and 1 deletions
  1. +2
    -0
      README.txt
  2. +18
    -0
      docs/wsgi.md
  3. +1
    -1
      nilmdb/server/__init__.py
  4. +15
    -0
      nilmdb/server/server.py

+ 2
- 0
README.txt View File

@@ -24,3 +24,5 @@ Usage:

nilmdb-server --help
nilmtool --help

See docs/wsgi.md for info on setting up a WSGI application in Apache.

+ 18
- 0
docs/wsgi.md View File

@@ -0,0 +1,18 @@
WSGI Application in Apache
--------------------------

Apache configuration should look something like:

<VirtualHost>
WSGIScriptAlias /nilmdb /home/nilmdb/nilmdb.wsgi
WSGIApplicationGroup nilmdb
WSGIProcessGroup nilmdb
WSGIDaemonProcess nilmdb threads=32
</VirtualHost>
where `/home/nilmdb/nilmdb.wsgi` is a file containing:

import nilmdb.server
application = nilmdb.server.wsgi_application("/home/nilmdb/db")
where `/home/nilmdb/db` is the path to the database.

+ 1
- 1
nilmdb/server/__init__.py View File

@@ -17,5 +17,5 @@ except (ImportError, TypeError): # pragma: no cover
pass

from nilmdb.server.nilmdb import NilmDB
from nilmdb.server.server import Server
from nilmdb.server.server import Server, wsgi_application
from nilmdb.server.errors import NilmDBError, StreamError, OverlapError

+ 15
- 0
nilmdb/server/server.py View File

@@ -536,6 +536,14 @@ class Server(object):
else:
cherrypy.server.shutdown_timeout = 5

def get_application(self):
"""Return a WSGI application object"""
def app(environ, start_response):
if environ['wsgi.multiprocess']:
raise Exception("can't function in a multi-process environment")
return cherrypy.tree(environ, start_response)
return app

def json_error_page(self, status, message, traceback, version):
"""Return a custom error page in JSON so the client can parse it"""
errordata = { "status" : status,
@@ -602,3 +610,10 @@ class Server(object):

def stop(self):
cherrypy.engine.exit()

def wsgi_application(dbpath):
"""Return a WSGI application object with a database at the
specified path."""
db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(dbpath)
server = nilmdb.server.Server(db, embedded = True)
return server.get_application()

Loading…
Cancel
Save