Skip to content

Commit e845e15

Browse files
Merge pull request #16 from CreatCodeBuild/dev
Dev
2 parents 5dc42f3 + 5493d22 commit e845e15

File tree

10 files changed

+57
-1247
lines changed

10 files changed

+57
-1247
lines changed

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# common
22
.idea/
33
*.pyc
4-
public/
4+
example/
55
.cache/
6-
__pycache__/
6+
__pycache__/
7+
8+
# package
9+
build/
10+
dist/
11+
*.egg-info

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
# Super Fast HTTP2 Framework for Progressive Web Application
22

3-
4-
# Dependency
5-
Python3.5+
6-
Future version might only support Python3.6+ since `curio` might only support 3.6+ in the future.
7-
```bash
8-
pip install h2
9-
pip install curio
3+
# Installation
4+
Clone this project to your local directory. In this directory,
5+
```
6+
python setup.py
107
```
11-
I will make it available on pip once I have the first release.
8+
This will automatically install `hyper2web` to the associated Python as a site-package.
9+
10+
I will make it available on PyPi once I have the first release.
11+
12+
## Python Version
13+
Python3.5+
14+
15+
## Dependency
16+
This project uses `h2` and `curio`.
1217

13-
This project is at its very early stage. I still need to learn a lot about h2, curio, HTTP and Web.
1418
[h2 Github](https://github.com/python-hyper/hyper-h2) [doc](https://python-hyper.org/h2/en/stable/)
1519
[curio Github](https://github.com/dabeaz/curio) [doc](https://curio.readthedocs.io/en/latest/)
1620

1721
# How to
18-
First clone this repo to your disk.
19-
20-
Under this repo, create a dir `public` or whatever names, put your frontend code there.
21-
22-
Then create an `app.py` or whatever names.
22+
Assuming you have a directory structure like
23+
```
24+
your project/
25+
--public/
26+
--index.html
27+
--index.js
28+
...
29+
--app.py
30+
```
31+
Your `app.py` looks like
2332
```python
2433
from hyper2web import app
2534

app.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

hyper2web/app.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ async def default_get(http, stream, parameters):
4141
else:
4242
self._router = router(None)
4343

44+
# will server this file on GET '/'
45+
if default_file:
46+
async def get_index(http, stream, para):
47+
await http.send_file(stream, os.path.join(self.root, self.default_file))
48+
self.get('/', get_index)
49+
4450
def up(self):
4551
kernel = Kernel()
4652
kernel.run(h2_server(address=("localhost", self.port),
4753
certfile="{}.crt.pem".format("localhost"),
4854
keyfile="{}.key".format("localhost"),
49-
app=self),
50-
shutdown=True)
55+
app=self))
5156

5257
def get(self, route: str, handler):
5358
self._router.register('GET', route, handler)

hyper2web/router.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,43 @@ def find_match(self, path: str):
3030
# todo: now the problem is how to implement it
3131
# todo: pattern matching should be independent from :method,
3232
# todo: but the current implementation doesn't support it. Should improve it later.
33-
print(self._routes.values())
3433
for routes_of_this_method in self._routes.values():
35-
print(routes_of_this_method)
3634
for route in routes_of_this_method:
3735
matched, parameters = self._match(route, path)
36+
# this function returns the first match, not the best match
3837
if matched:
3938
return route, parameters
4039
return None, None
4140

4241
@classmethod
4342
def _match(cls, route, path):
44-
# todo: it seems like that regular expression is not necessary
45-
# note: Could it be simpler? Could regex help?
46-
route = route.split('/')
47-
path = path.split('/')
43+
# '/something/xxx/' to 'something/xxx'. Get rid of '/' at the left and the right end of a string
44+
route = route.lstrip('/').rstrip('/').split('/')
45+
path = path.lstrip('/').rstrip('/').split('/')
46+
print(route, path)
4847
if len(route) != len(path):
4948
return False, None
5049
else:
5150
# todo: implement it
5251
parameters = {}
5352
for r, p in zip(route, path):
53+
if r == p == '':
54+
return True, None
5455
if r[0] == '{' and r[-1] == '}':
5556
parameters[r[1:-1]] = p
5657
elif r != p:
5758
return False, None
59+
print('out of for loop')
5860
return True, parameters
5961

6062
# async
6163
async def handle_route(self, http: HTTP, stream: Stream):
6264
print('app.App.handle_route')
6365

64-
path = stream.headers[':path'].lstrip('/')
66+
path = stream.headers[':path']
6567
method = stream.headers[':method']
6668

6769
route, parameters = self.find_match(path)
68-
print('app.App.handle_route', route)
6970

7071
# 如果没有任何匹配,就默认为静态文件读取
7172
if route is None:

localhost.crt.pem

Lines changed: 0 additions & 21 deletions
This file was deleted.

localhost.key

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)