aboutsummaryrefslogtreecommitdiffstats
path: root/index.wsgi
blob: 4acfcd6d9198ab415041b114e1f9d1d51ad1f485 (plain)
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
#!/usr/bin/python
#
#   CMS WSGI wrapper
#
#   Copyright (C) 2011-2012 Michael Buesch <m@bues.ch>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import atexit
try:
	from urlparse import parse_qs
except ImportError:
	from cgi import parse_qs
try:
	sys.path.append("/var/cms") # workaround for old WSGI
	from cms import *
except ImportError:
	raise Exception("Failed to import cms.py. Wrong WSGIPythonPath?")

cms = None

def __initCMS(environ):
	global cms
	if cms:
		return # Already initialized
	try:
		domain = environ["cms.domain"]
		cmsBase = environ["cms.cmsBase"]
		wwwBase = environ["cms.wwwBase"]
	except (KeyError), e:
		raise Exception("WSGI environment %s not set" % str(e))
	debug = False
	try:
		debug = stringBool(environ["cms.debug"])
	except (KeyError), e:
		pass
	# Initialize the CMS module
	cms = CMS(dbPath = cmsBase + "/db",
		  wwwPath = wwwBase,
		  domain = domain,
		  debug = debug)
	atexit.register(cms.shutdown)

def application(environ, start_response):
	__initCMS(environ)
	if cms.debug:
		startStamp = datetime.now()
	status = "200 OK"
	additional_headers = []
	try:
		method = environ["REQUEST_METHOD"].upper()
		path = environ["PATH_INFO"]
		query = parse_qs(environ["QUERY_STRING"])
		if method == "GET":
			response_body, response_mime = cms.get(path, query)
		elif method == "POST":
			response_body, response_mime = cms.post(path, query)
		else:
			response_body, response_mime =\
				"INVALID REQUEST_METHOD", "text/plain"
	except (CMSException), e:
		status = e.httpStatus
		response_body, response_mime, additional_headers = cms.getErrorPage(e)
	if cms.debug and response_mime.lower() == "text/html":
		delta = datetime.now() - startStamp
		sec = float(delta.seconds) + float(delta.microseconds) / 1000000
		response_body += "\n<!-- generated in %.3f seconds -->" % sec
	response_headers = [ ('Content-Type', response_mime),
			     ('Content-Length', str(len(response_body))) ]
	response_headers.extend(additional_headers)
	start_response(status, response_headers)
	return (response_body,)
bues.ch cgit interface