-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·63 lines (49 loc) · 1.5 KB
/
main.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
#!/usr/bin/env python
from aiohttp import web
import aiohttp_jinja2
import jinja2
import aiocoap.resource as resource
import aiocoap
import routes
import resources
import asyncio
import hashlib
import logging
from pathlib import Path
import os.path
PROJECT_ROOT = '.'
def get_files():
p = Path('./uploads')
files = []
for child in p.iterdir():
with child.open('rb') as f:
m = hashlib.sha1(f.read()).hexdigest()[:16]
url = os.path.join('f', m)
files.append(resources.FirmwareFile(child, url, m))
return files
@asyncio.coroutine
def init(loop, app):
srv = yield from loop.create_server(app.make_handler(),
'::1', 8080)
return srv
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
loop = asyncio.get_event_loop()
coap = resource.Site()
coap.add_resource(('.well-known', 'core'),
resource.WKCResource(coap.get_resources_as_linkheader))
app = web.Application(loop=loop)
aiohttp_jinja2.setup(app,
loader=jinja2.FileSystemLoader('templates'))
app['uploads'] = get_files()
app['coap'] = coap
for dpt in app['uploads']:
resources.add_file_resource(coap, dpt)
routes.setup_routes(app, PROJECT_ROOT)
asyncio.Task(aiocoap.Context.create_server_context(coap))
loop.run_until_complete(init(loop, app))
print("starting loop!")
try:
loop.run_forever()
except KeyboardInterrupt:
pass