-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
165 lines (126 loc) · 4.61 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
""" Server that serves a simple landing page that includes a list of
links to apps. These apps are statically served.
Also hosts an API that flexx can POST to to submit static apps.
"""
import os
import io
import sys
import shutil
import asyncio
import zipfile
from aiohttp import web
config = {"appdir": "~/flexx_apps", "host": "127.0.0.1", "port": 8080}
## Submit stuff
async def handle_submit(request):
""" http handler to post apps.
"""
name = request.match_info.get("name", "")
token = request.match_info.get("token", "")
blob = await request.read()
try:
submit_app(name, token, blob)
except Exception as err:
return web.Response(status=403, text=str(err))
else:
return web.Response(status=200, text="Thanks for submitting app %r!" % name)
def get_app_dir(sub=None):
appdir = os.path.expanduser(config["appdir"])
if not os.path.isdir(appdir):
os.makedirs(appdir)
if sub:
return os.path.join(appdir, sub)
else:
return appdir
def submit_app(name, token, blob):
# Init
appdir = get_app_dir(name)
tokenfile = os.path.join(appdir, ".token")
name = name.strip().lower()
if ">>" in token:
cur_token, _, new_token = token.partition(">>")
cur_token, new_token = cur_token.strip(), new_token.strip()
else:
cur_token = new_token = token.strip()
# Check app and token
if not name.isidentifier(): # must also be nonempty
raise Exception("Invalid app name provided.")
if not cur_token:
raise Exception("Invalid token provided.")
# Check whether we have "permission" to write this app
ref_token = None
if os.path.isfile(tokenfile):
ref_token = open(tokenfile, "rb").read().decode().strip()
if ref_token is not None and cur_token != ref_token:
raise Exception("Invalid token provided for app %r." % name)
# Blob ok?
if len(blob) > 10 * 2 ** 20:
t = "Refusing submitted app %r because its payload is over 10 MiB."
raise Exception(t % name)
# Prepare appdir (clear and write token)
if os.path.isdir(appdir):
shutil.rmtree(appdir)
os.mkdir(appdir)
with open(tokenfile, "wb") as f:
f.write(new_token.encode())
# Unpack
f = io.BytesIO(blob)
zf = zipfile.ZipFile(f, "r")
for fname in zf.namelist():
if fname == ".token":
continue # don't overwrite token!
zf.extract(fname, appdir)
zf.close()
## Main Website
async def handle_root(request):
# Redirect?
if request.host.endswith(".live"):
return web.Response(status=307, headers=[("Location", "https://flexx.app")])
# Image asset?
fname = request.match_info.get("fname", "")
if fname:
if fname in ("flexx.ico", "flexx.png"):
fname = os.path.join(os.path.dirname(__file__), fname)
return web.Response(body=open(fname, "rb").read())
elif fname == "stop" and request.remote == "127.0.0.1":
asyncio.get_event_loop().stop()
return web.Response(text="Stopping server ...")
return web.Response(status=404)
# Collect html for list of apps
app_lines = []
for dname in sorted(os.listdir(get_app_dir())):
app_lines.append("<li><a href='/open/%s/'>%s</a></li>" % (dname, dname))
app_lines.insert(0, "<ul>")
app_lines.append("</ul>")
# Build html
fname = os.path.join(os.path.dirname(__file__), "index.html")
template = open(fname, "rb").read().decode()
text = template.replace("STATIC_APP_LIST", "\n".join(app_lines))
return web.Response(text=text, content_type="text/html")
async def handle_app_root(request):
fname = os.path.join(
get_app_dir(), request.match_info.get("name", ""), "index.html"
)
if os.path.isfile(fname):
text = open(fname, "rb").read().decode()
return web.Response(text=text, content_type="text/html")
else:
return web.Response(status=404)
## Setting up
app = web.Application()
def start():
# Parse cli args to init config
for arg in sys.argv[1:]:
if "=" in arg:
key, _, value = arg.partition("=")
key = key.lower()
if key in config:
config[key] = value
app.router.add_get("/", handle_root)
app.router.add_get("/{fname}", handle_root)
app.router.add_post("/submit/{name}/{token}", handle_submit)
# app.router.add_get('/open/*', handle_app)
app.router.add_get("/open/{name}/", handle_app_root)
app.router.add_static("/open", get_app_dir())
web.run_app(app, host=config["host"], port=int(config["port"]))
if __name__ == "__main__":
start()