From 6af3a6fc41462e5b55d4abd4b224b8e15cbb8b31 Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Fri, 22 Mar 2013 19:14:34 -0400 Subject: [PATCH] Add WSGI application support and documentation --- README.txt | 2 ++ docs/wsgi.md | 18 ++++++++++++++++++ nilmdb/server/__init__.py | 2 +- nilmdb/server/server.py | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 docs/wsgi.md diff --git a/README.txt b/README.txt index 9adc4d2..d7c8227 100644 --- a/README.txt +++ b/README.txt @@ -24,3 +24,5 @@ Usage: nilmdb-server --help nilmtool --help + +See docs/wsgi.md for info on setting up a WSGI application in Apache. diff --git a/docs/wsgi.md b/docs/wsgi.md new file mode 100644 index 0000000..8dc8a2f --- /dev/null +++ b/docs/wsgi.md @@ -0,0 +1,18 @@ +WSGI Application in Apache +-------------------------- + +Apache configuration should look something like: + + + WSGIScriptAlias /nilmdb /home/nilmdb/nilmdb.wsgi + WSGIApplicationGroup nilmdb + WSGIProcessGroup nilmdb + WSGIDaemonProcess nilmdb threads=32 + + +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. diff --git a/nilmdb/server/__init__.py b/nilmdb/server/__init__.py index a903088..3c30990 100644 --- a/nilmdb/server/__init__.py +++ b/nilmdb/server/__init__.py @@ -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 diff --git a/nilmdb/server/server.py b/nilmdb/server/server.py index f9842a5..b34b7c8 100644 --- a/nilmdb/server/server.py +++ b/nilmdb/server/server.py @@ -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()