Skip to content
Merged

Dev #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# common
.idea/
*.pyc
example/
/example/*
/example/game/game_record.json
.cache/
__pycache__/

# package
build/
dist/
*.egg-info
!/example/game
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Super Fast HTTP2 Framework for Progressive Web Application

# Installation
Clone this project to your local directory. In this directory,
```
python setup.py
```
This will automatically install `hyper2web` to the associated Python as a site-package.
At this point, the best way to install it is probably download the zip and extract it to your projects' directory.

I have not figured out how to make it installable with `pip` yet.

I will make it available on PyPi once I have the first release.

Expand Down Expand Up @@ -56,6 +54,9 @@ from hyper2web import app
app.App(port=5000).up()
```

# Example
See the example folders for examples.

# Test
Python modules/packages and imports are confusing. You have to do
```python
Expand Down
31 changes: 31 additions & 0 deletions example/game/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json

from curio import aopen

from hyper2web import app
from game import update_record, game_record, game_record_path


app = app.App()
# should raise an error if no response method is called
# should raise an error if response method is not called with await
async def post_record(http, stream, para):
record = json.loads(str(stream.data, encoding='utf8'))
update_record(record, game_record)
await http.send_error(stream, 200)

# write records to disk
async with aopen(game_record_path, mode='w') as f:
game_record_string = json.dumps(game_record, indent='\t')
await f.write(game_record_string)

app.post('/post_record', post_record)


async def get_top10(http, stream, para):
level_top10 = game_record.get(para['levelIndex'], [])
string = json.dumps(level_top10)
await http.send_and_end(stream, bytes(string, encoding='utf8'))
app.get('/get_top10/{levelIndex}', get_top10)

app.up()
31 changes: 31 additions & 0 deletions example/game/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json


def update_record(this_record, old_records):
timeUsed = this_record['timeUsed']

levelIndex = str(this_record['level'])
level_top10 = old_records.get(levelIndex, [])

i = 0
while i < 10 and i < len(level_top10):
if timeUsed < level_top10[i]['timeUsed']:
level_top10.insert(i, this_record)
break
i += 1
else:
level_top10.insert(i, this_record)
if len(level_top10) > 10:
level_top10.pop()

old_records[levelIndex] = level_top10


game_record_path = 'game_record.json'

try:
with open(game_record_path, encoding='utf8') as f:
game_record = json.load(f, encoding='utf8')
except json.decoder.JSONDecodeError and FileNotFoundError:
print('init a new record')
game_record = {}
21 changes: 21 additions & 0 deletions example/game/localhost.crt.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDhTCCAm2gAwIBAgIJAOrxh0dOYJLdMA0GCSqGSIb3DQEBCwUAMFkxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xNTA5MTkxNDE2
NDRaFw0xNTEwMTkxNDE2NDRaMFkxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21l
LVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNV
BAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMqt
A1iu8EN00FU0eBcBGlLVmNEgV7Jkbukra+kwS8j/U2y50QPGJc/FiIVDfuBqk5dL
ACTNc6A/FQcXvWmOc5ixmC3QKKasMpuofqKz0V9C6irZdYXZ9rcsW0gHQIr989yd
R+N1VbIlEVW/T9FJL3B2UD9GVIkUELzm47CSOWZvAxQUlsx8CUNuUCWqyZJoqTFN
j0LeJDOWGCsug1Pkj0Q1x+jMVL6l6Zf6vMkLNOMsOsWsxUk+0L3tl/OzcTgUOCsw
UzY59RIi6Rudrp0oaU8NuHr91yiSqPbKFlX10M9KwEEdnIpcxhND3dacrDycj3ux
eWlqKync2vOFUkhwiaMCAwEAAaNQME4wHQYDVR0OBBYEFA0PN+PGoofZ+QIys2Jy
1Zz94vBOMB8GA1UdIwQYMBaAFA0PN+PGoofZ+QIys2Jy1Zz94vBOMAwGA1UdEwQF
MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAEplethBoPpcP3EbR5Rz6snDDIcbtAJu
Ngd0YZppGT+P0DYnPJva4vRG3bb84ZMSuppz5j67qD6DdWte8UXhK8BzWiHzwmQE
QmbKyzzTMKQgTNFntpx5cgsSvTtrHpNYoMHzHOmyAOboNeM0DWiRXsYLkWTitLTN
qbOpstwPubExbT9lPjLclntShT/lCupt+zsbnrR9YiqlYFY/fDzfAybZhrD5GMBY
XdMPItwAc/sWvH31yztarjkLmld76AGCcO5r8cSR/cX98SicyfjOBbSco8GkjYNY
582gTPkKGYpStuN7GNT5tZmxvMq935HRa2XZvlAIe8ufp8EHVoYiF3c=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions example/game/localhost.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAyq0DWK7wQ3TQVTR4FwEaUtWY0SBXsmRu6Str6TBLyP9TbLnR
A8Ylz8WIhUN+4GqTl0sAJM1zoD8VBxe9aY5zmLGYLdAopqwym6h+orPRX0LqKtl1
hdn2tyxbSAdAiv3z3J1H43VVsiURVb9P0UkvcHZQP0ZUiRQQvObjsJI5Zm8DFBSW
zHwJQ25QJarJkmipMU2PQt4kM5YYKy6DU+SPRDXH6MxUvqXpl/q8yQs04yw6xazF
ST7Qve2X87NxOBQ4KzBTNjn1EiLpG52unShpTw24ev3XKJKo9soWVfXQz0rAQR2c
ilzGE0Pd1pysPJyPe7F5aWorKdza84VSSHCJowIDAQABAoIBACp+nh4BB/VMz8Wd
q7Q/EfLeQB1Q57JKpoqTBRwueSVai3ZXe4CMEi9/HkG6xiZtkiZ9njkZLq4hq9oB
2z//kzMnwV2RsIRJxI6ohGy+wR51HD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