-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.py
More file actions
69 lines (60 loc) · 2.14 KB
/
router.py
File metadata and controls
69 lines (60 loc) · 2.14 KB
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
import re
from typing import List
from datetime import datetime
from wrapper import RequestObject, ResponseObject
class Route:
def __init__(self, method: str, route_str, function):
self.method: str = method
self.route_str: str = route_str
self.function = function
self.static = True
self.keys = []
self.__replace_regex()
def match(self, method: str, uri: str):
return method == self.method and self.uri.fullmatch(uri)
def __replace_regex(self):
t = self.route_str.split("/")
res = []
static = True
keys = []
for i, w in enumerate(t):
if not w.startswith("<"):
res.append(w)
continue
static = False
x = w[1:-1]
key, type = x.split(":")
if type == "int":
res.append("\\d+")
else:
res.append("\\w+")
keys.append((i, key))
self.static = static
self.keys = keys
uri = "\\/".join(res)
print("original: ", self.route_str)
print("uri: ", uri)
self.uri = re.compile(uri)
def parse_path_variables(self, uri):
t = uri.split("/")
path_variables = {}
for i, key in self.keys:
path_variables[key] = t[i]
return path_variables
class Router:
def __init__(self, default_header={}):
self.routes: List[Route] = []
self.all_route_headers = default_header
self.__datetime_format = "%a, %d %b %Y %H:%M:%S %Z"
def add_route(self, route: Route):
self.routes.append(route)
def route_request(self, req: RequestObject, resp: ResponseObject):
for route in self.routes:
if route.match(req.method, req.uri):
for key, value in self.all_route_headers.items():
resp.set_header(key, value)
if not route.static:
req.path_variables = route.parse_path_variables(req.uri)
route.function(req, resp)
resp.set_header("Date", datetime.now().strftime(self.__datetime_format))
return resp