-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_server.py
54 lines (39 loc) · 1.62 KB
/
gui_server.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
import asyncio
import os
from aiohttp import web
from environment import Environment
from gui_server_ws import broadcast, handle_ws_remote
from proxy_log import ProxyLog
def static_dir() -> str:
return os.path.join('.', 'webapp', 'dist')
async def handle_download(request):
hash_value = request.match_info['hash']
proxy_log: ProxyLog = request.app.get('proxy_log')
content = proxy_log.get_content_storage().read(hash_value)
if content is None:
return web.Response(status=404)
content_type = request.query.get('content_type', 'application/octet-stream')
return web.Response(body=content, status=200, headers={'Content-Type': content_type})
def run_gui_server(environment: Environment, proxy_log: ProxyLog):
app = web.Application()
app.setdefault('proxy_log', proxy_log)
async def index(request):
return web.FileResponse(os.path.join(static_dir(), 'index.html'))
app.add_routes([
web.get('/', index),
web.get('/remote', handle_ws_remote),
web.get('/download/{hash}.{extension}', handle_download),
web.get('/download/{hash}', handle_download),
])
app.router.add_static('/', path=static_dir(), name='static')
async def on_startup(_app):
_app['broadcast_task'] = asyncio.create_task(broadcast(app))
async def on_cleanup(_app):
_app['broadcast_task'].cancel()
await _app['broadcast_task']
app.on_startup.append(on_startup)
app.on_cleanup.append(on_cleanup)
try:
web.run_app(app, host=environment.GUI_LISTEN_HOST, port=environment.GUI_LISTEN_PORT)
except KeyboardInterrupt:
app.shutdown()