-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsgi_layer.py
51 lines (45 loc) · 1.69 KB
/
wsgi_layer.py
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
#encoding=utf-8
from wsgiref.simple_server import make_server
from cgi import parse_qs
from uuid import uuid1
from datetime import datetime
import re, os
class Request(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self
class Application:
def __init__(self, routes):
self.routes = routes
self.layout = open('layout.html').read()
def __call__(self, environ, start_response):
request = Request()
request.update({
'path' : environ['PATH_INFO'],
'method': environ['REQUEST_METHOD'],
'accept': environ['HTTP_ACCEPT'] if 'HTTP_ACCEPT' in environ else 'text/html',
'GET' : parse_qs(environ['QUERY_STRING']) if environ['REQUEST_METHOD'] == 'GET' else {}
})
raw_response = ""
for pattern, controller in self.routes.items():
they_match = re.match(pattern, request.path)
print they_match
if they_match:
raw_response = controller(request, **they_match.groupdict())
break
else:
raw_response = "No sé qué hacer con %s" % request.path
if request.accept == 'text/plain':
response = raw_response
else:
response = self.layout % {'content': raw_response}
start_response(
"200 OK",
[('Content-Type', request.accept),
('Content-Length', str(len(response)))]
)
return [response]
def serve(self):
from wsgiref.simple_server import make_server
daemon = make_server('127.0.0.1', 8000, self)
daemon.serve_forever()