forked from geopython/pycsw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csw.wsgi
136 lines (111 loc) · 3.97 KB
/
csw.wsgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: iso-8859-15 -*-
# =================================================================
#
# Authors: Adam Hinz <[email protected]>
#
# Copyright (c) 2012 Adam Hinz
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# =================================================================
# WSGI wrapper for pycsw
#
# Apache mod_wsgi configuration
#
# ServerName host1
# WSGIDaemonProcess host1 home=/var/www/pycsw processes=2
# WSGIProcessGroup host1
#
# WSGIScriptAlias /pycsw-wsgi /var/www/pycsw/csw.wsgi
#
# <Directory /var/www/pycsw>
# Order deny,allow
# Allow from all
# </Directory>
#
# or invoke this script from the command line:
#
# $ python ./csw.wsgi
#
# which will publish pycsw to:
#
# http://localhost:8000/
#
from StringIO import StringIO
import os
import sys
app_path = os.path.dirname(os.path.realpath(__file__))
# Activate enviroment, assuming this is a source installation
activate_this = os.path.realpath(os.path.join(app_path, '../..', 'bin/activate_this.py'))
execfile(activate_this, dict(__file__=activate_this))
sys.path.append(app_path)
from pycsw import server
def application(env, start_response):
"""WSGI wrapper"""
config = 'default.cfg'
if 'PYCSW_CONFIG' in env:
config = env['PYCSW_CONFIG']
if env['QUERY_STRING'].lower().find('config') != -1:
for kvp in env['QUERY_STRING'].split('&'):
if kvp.lower().find('config') != -1:
config = kvp.split('=')[1]
if not os.path.isabs(config):
config = os.path.join(app_path, config)
if 'HTTP_HOST' in env and ':' in env['HTTP_HOST']:
env['HTTP_HOST'] = env['HTTP_HOST'].split(':')[0]
env['local.app_root'] = app_path
csw = server.Csw(config, env)
gzip = False
if ('HTTP_ACCEPT_ENCODING' in env and
env['HTTP_ACCEPT_ENCODING'].find('gzip') != -1):
# set for gzip compressed response
gzip = True
# set compression level
if csw.config.has_option('server', 'gzip_compresslevel'):
gzip_compresslevel = \
int(csw.config.get('server', 'gzip_compresslevel'))
else:
gzip_compresslevel = 0
contents = csw.dispatch_wsgi()
headers = {}
if gzip and gzip_compresslevel > 0:
import gzip
buf = StringIO()
gzipfile = gzip.GzipFile(mode='wb', fileobj=buf,
compresslevel=gzip_compresslevel)
gzipfile.write(contents)
gzipfile.close()
contents = buf.getvalue()
headers['Content-Encoding'] = 'gzip'
headers['Content-Length'] = str(len(contents))
headers['Content-Type'] = csw.contenttype
status = '200 OK'
start_response(status, headers.items())
return [contents]
if __name__ == '__main__': # run inline using WSGI reference implementation
from wsgiref.simple_server import make_server
port = 8000
if len(sys.argv) > 1:
port = int(sys.argv[1])
httpd = make_server('', port, application)
print "Serving on port %d..." % port
httpd.serve_forever()