-
Notifications
You must be signed in to change notification settings - Fork 0
/
pieweb.py
86 lines (64 loc) · 2.3 KB
/
pieweb.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
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
import re
from wsgiref.simple_server import make_server
from urllib.parse import parse_qsl
from http.client import responses
class Response:
def __init__(self, data={}, status_code=200, headers=[]):
self.response_data = data
self.status = f"{status_code} {responses.get(status_code)}"
self.headers = headers
if not self.headers:
self.headers.append(
('Content-Type', 'application/json; charset=utf-8')
)
def __iter__(self, *args, **kwargs):
yield f"{self.response_data}".encode('utf-8')
class Request:
def __init__(self, environ):
self.request = environ
@property
def GET(self):
return dict(parse_qsl(self.request["QUERY_STRING"]))
@property
def POST(self):
content_length = int(self.request.get('CONTENT_LENGTH', 0))
if content_length:
data = self.request['wsgi.input'].read(content_length)
return dict(parse_qsl(data.decode('utf-8')))
return {}
class Router:
def __init__(self):
self.routes = {}
def register_route(self, route, fn):
if route not in self.routes:
self.routes[route] = fn
def get_route_from_url(self, url):
for route in self.routes:
if re.match(route, url):
return route
raise Exception("Route not registered")
class PieWeb:
def __init__(self, name):
self.name = name
self.router = Router()
def route(self, route):
def decorator(fn):
nonlocal self, route
self.router.register_route(route, fn)
def wrapper(environ, start_response):
request = Request(environ)
response = fn(request)
start_response(response.status, response.headers)
return iter(response)
return wrapper
return decorator
def application(self, environ, start_response):
url_path = environ['PATH_INFO']
route = self.router.get_route_from_url(url_path)
fn = self.router.routes[route]
return self.route(route)(fn)(environ, start_response)
def run(self, port=9000):
with make_server('', port, self.application) as server:
server.serve_forever()
def __call__(self):
self.run()