Skip to content

Commit 70879fa

Browse files
parameterized routing implemented
1 parent eaae95c commit 70879fa

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed

app.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
if __name__ == '__main__':
44

5+
app = app.App()
6+
57
# A basic callback style API is provided
6-
async def get_name(http, stream):
8+
async def post_name(http, stream, parameters):
79
# this route essentially echo the data received back to client
810
print('data received:')
911
print(str(stream.data, encoding='utf8'))
1012
await http.send_and_end(stream, stream.data)
13+
app.post('name', post_name)
14+
15+
async def get_user(http, stream, parameters):
16+
print(parameters)
17+
await http.send_error(stream, 200)
18+
app.get('user/{userId}', get_user)
1119

12-
app = app.App()
13-
app.post('name', get_name)
1420
app.up()

hyper2web/abstract.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ async def handle_route(self, http, stream):
1818

1919

2020
class AbstractRouter:
21-
def _route(self, method: str, route: str, handler):
22-
raise NotImplementedError
23-
24-
def get(self, route: str, handler):
25-
raise NotImplementedError
26-
27-
def post(self, route: str, handler):
21+
def register(self, method: str, route: str, handler):
2822
raise NotImplementedError
2923

3024
async def handle_route(self, http, stream):

hyper2web/app.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@ def __init__(self, port=5000, root='./public',
2828
self.default_file = default_file
2929

3030
if auto_serve_static_file:
31-
async def default_get(http, stream):
31+
async def default_get(http, stream, parameters):
32+
print('default_get')
3233
route = stream.headers[':path'].lstrip('/')
3334
full_path = os.path.join(self.root, route)
3435
if os.path.exists(full_path):
3536
await http.send_file(stream, full_path)
3637
else:
3738
await http.send_error(stream, 404)
3839

39-
self._router = router(default_get, None)
40+
self._router = router(default_get)
4041
else:
41-
self._router = router(None, None)
42+
self._router = router(None)
4243

4344
def up(self):
4445
kernel = Kernel()
@@ -49,10 +50,10 @@ def up(self):
4950
shutdown=True)
5051

5152
def get(self, route: str, handler):
52-
self._router.get(route, handler)
53+
self._router.register('GET', route, handler)
5354

5455
def post(self, route: str, handler):
55-
self._router.post(route, handler)
56+
self._router.register('POST', route, handler)
5657

5758
# async
5859
async def handle_route(self, http: HTTP, stream: Stream):

hyper2web/router.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,18 @@ class Router(AbstractRouter):
88
"""User should never construct Router"""
99

1010
# todo: I may want to change the constructor
11-
def __init__(self, default_get, default_post):
11+
def __init__(self, default_get):
1212
self._routes = {
1313
'GET': {},
1414
'POST': {}
1515
}
16-
self.method_default = {
17-
'GET': default_get,
18-
'POST': default_post
19-
}
16+
self.default_get = default_get
2017

21-
def _route(self, method: str, route: str, handler):
22-
assert method in ['GET', 'POST']
18+
def register(self, method: str, route: str, handler):
19+
assert method in ['GET', 'POST'] # 这只是目前为了测试而加的限制
2320
self._routes[method][route] = handler
2421

25-
def get(self, route: str, handler):
26-
self._route('GET', route, handler)
27-
28-
def post(self, route: str, handler):
29-
self._route('POST', route, handler)
30-
31-
def match(self, path: str):
22+
def find_match(self, path: str):
3223
"""
3324
'user/{userId}' should match 'user/abc'
3425
userId = abc
@@ -39,11 +30,14 @@ def match(self, path: str):
3930
# todo: now the problem is how to implement it
4031
# todo: pattern matching should be independent from :method,
4132
# todo: but the current implementation doesn't support it. Should improve it later.
33+
print(self._routes.values())
4234
for routes_of_this_method in self._routes.values():
35+
print(routes_of_this_method)
4336
for route in routes_of_this_method:
4437
matched, parameters = self._match(route, path)
4538
if matched:
4639
return route, parameters
40+
return None, None
4741

4842
@classmethod
4943
def _match(cls, route, path):
@@ -70,8 +64,23 @@ async def handle_route(self, http: HTTP, stream: Stream):
7064
path = stream.headers[':path'].lstrip('/')
7165
method = stream.headers[':method']
7266

73-
route, parameters = self.match(path)
67+
route, parameters = self.find_match(path)
68+
print('app.App.handle_route', route)
69+
70+
# 如果没有任何匹配,就默认为静态文件读取
71+
if route is None:
72+
if method == 'GET':
73+
print('GET')
74+
handler = self.default_get
75+
else:
76+
handler = None
77+
else:
78+
handler = self._routes[method].get(route, None)
7479

75-
handler = self._routes[method].get(route, self.method_default[method])
7680
if handler is not None:
77-
await handler(http, stream, parameters=parameters)
81+
print('handle')
82+
print(handler)
83+
await handler(http, stream, parameters)
84+
else:
85+
# maybe raise an error?
86+
raise Exception(path, 'is not a valid request path')

0 commit comments

Comments
 (0)