source: TI07-MOLES/trunk/PythonCode/browse/portal/cgi/ndg.wsgi.py @ 1646

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/browse/portal/cgi/ndg.wsgi.py@1646
Revision 1646, 3.3 KB checked in by lawrence, 13 years ago (diff)

Simple wsgi code to run with fastcgi -- discovery wrapper. Works.

Line 
1#!/usr/local/bin/python
2from flup.server.fcgi import WSGIServer
3import cgi
4from browse.DiscoveryGUI import DiscoveryGUI
5from browse.Utilities import Request, Response
6
7def getEnv(environ):
8    ''' Simple test response to show environment '''
9    sorted_keys = environ.keys()
10    sorted_keys.sort()
11    env=[]
12    for k in sorted_keys: env.append('%s:%s'%(k,environ[k]))
13    response = [
14            '<h1>Keys in <tt>environ</tt></h1><p>',
15            '<br />'.join(env),
16            '</p>',
17        ]
18    return response
19   
20def showScript(environ):
21    ''' Simple test response to show path of request, aka script_name '''
22    script = environ['SCRIPT_NAME']
23    response=['<p>','You asked for %s'%script,'</p>']
24    return response
25
26
27def showcgi(fs,response):
28    ''' Take a cgi field storage variable and return some html about it '''
29    response.append('<h3> CGI Stuff </h3>')
30    for key in fs: response.append('<p>cgi key [%s]:%s</p>'%(key,fs[key].value))
31    return response
32
33def cgienviron(self,environ,script):
34    ''' based on paste's cgiapp '''
35    cgi_environ = os.environ.copy()
36    for name in environ:
37        # Should unicode values be encoded?
38        if (name.upper() == name and isinstance(environ[name], str)):
39            cgi_environ[name] = environ[name]
40    cgi_environ['SCRIPT_FILENAME'] = script
41    return cgi_environ
42
43def getcgfs(environ):
44    return cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ,
45                                keep_blank_values=1)
46
47class NDGWrapper(object):
48     
49    def __init__(self, wrapTarget,pageName):
50        ''' Simple example Middleware '''
51       
52        self.wrapTarget=wrapTarget
53
54        self.pageStart='''<?xml version="1.0" encoding="utf-8"?>
55                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
56                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
57                <head>
58                  <meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/>
59                  <title>NDG: %s</title>
60                  <link media="all, screen" href="layout/ndg.css" type="text/css" rel="stylesheet"/>
61                </head>'''%pageName
62       
63        self.header='''
64        <div id="header"></div>
65        '''
66       
67        self.footer='''<div id="Footer">
68        <p> This portal is a pre-alpha release of NDG software.
69        We expect bugs, and things not to work, so please let us know about such problems
70        (please email badc@rl.ac.uk, and include "NDG problem" in the subject line).
71        We will be adding a help link to this page when the interface is slightly more stable.
72        </p> </div>
73        </div> 
74        </body>
75        </html>'''
76       
77    def __call__(self,environ,start_response):
78        ''' Let's prepend and postpend some stuff'''
79        response=[self.pageStart,self.header]
80        for i in self.wrapTarget(environ,start_response): response.append(i)
81        response.append(self.footer)
82        return response
83
84class discovery:
85   
86    def __init__(self,configDir):
87        self.configDir=configDir
88   
89    def __call__(self,environ,start_response):
90        fs=getcgfs(environ)
91        discovery=DiscoveryGUI(Request(environ,fs,wsgi=1),self.configDir)
92        start_response('200 OK', [('Content-Type', 'text/html')])
93        return [discovery.html]
94
95discover=discovery('browse/')
96app=NDGWrapper(discover,'NDG Data Portal')
97WSGIServer(app, bindAddress = '/tmp/fcgi.sock').run()
Note: See TracBrowser for help on using the repository browser.