an attempt at diagnosing wsgi issues more accurately
[plcapi.git] / apache / plc.wsgi
1 # -*- python -*-
2 #
3 # Apache mod_wsgi python interface
4 #
5 # Copyright (C) 2004-2006 The Trustees of Princeton University
6 #
7
8 import sys
9 sys.path.append('/usr/share/plc_api')
10 sys.stdout = sys.stderr
11 import traceback
12 from PLC.Debug import log
13 from PLC.API import PLCAPI
14
15 api = PLCAPI()
16
17 # recipe from this page
18 # http://code.google.com/p/modwsgi/wiki/DebuggingTechniques
19 # let ErrorMiddleware deal with exceptions
20 def application(environ, start_response):
21     status = '200 OK'
22     if environ.get('REQUEST_METHOD') != 'POST':
23         content_type = 'text/html'
24         output = """
25 <html><head>
26 <title>PLCAPI WSGI XML-RPC/SOAP Interface</title>
27 </head><body>
28 <h1>PLCAPI WSGI XML-RPC/SOAP Interface</h1>
29 <p>Please use XML-RPC or SOAP to access the PLCAPI.</p>
30 </body></html>
31 """
32     else:
33         api.environ = environ
34         content_type = 'text/xml'
35         ip = environ.get('REMOTE_ADDR')
36         port = environ.get('REMOTE_PORT')
37         output = api.handle((ip,port),environ.get('wsgi.input').read())
38         # Shut down database connection, otherwise up to MaxClients DB
39         # connections will remain open.
40         api.db.close()
41
42     # Write response
43     response_headers = [('Content-type', '%s' % content_type),
44                        ('Content-Length', str(len(output)))]
45     start_response(status, response_headers)
46     return [output] 
47
48 from paste.exceptions.errormiddleware import ErrorMiddleware
49 application = ErrorMiddleware(application, debug=True)