-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
61 lines (44 loc) · 1.84 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
import os
import json
import socket
import datetime
import asgineer
from makesite import create_assets
stats_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send_stats(request, status_code=None, rtime=None, is_page=None):
""" Send request stats over UPD to a stats server. """
p = request.path
stats = {"group": os.getenv("MYPAAS_SERVICE", "")}
stats["requests|count"] = 1
stats["path|cat"] = f"{status_code} - {p}" if (status_code and p) else p
if rtime is not None:
stats["rtime|num|s"] = float(rtime)
if is_page: # anomimously register page view, visitors, language, and more
stats["pageview"] = request.headers
try:
stats_socket.sendto(json.dumps(stats).encode(), ("stats", 8125))
except Exception:
pass
asset_handler = asgineer.utils.make_asset_handler(create_assets())
redirects = {
"www.pyzo.org": "https://pyzo.org",
"community.pyzo.org": "https://github.com/pyzo/pyzo/discussions",
"repo.pyzo.org": "https://github.com/pyzo/pyzo",
"issues.pyzo.org": "https://github.com/pyzo/pyzo/issues",
"guide.pyzo.org": "https://pyzo.org/guide.html",
"miniconda.pyzo.org": "https://conda.io/miniconda.html",
"matlab.pyzo.org": "https://numpy.org/doc/stable/user/numpy-for-matlab-users.html",
}
@asgineer.to_asgi
async def main_handler(request):
if request.host in redirects:
url = redirects[request.host]
return 307, {"Location": url }, b""
path = request.path.lstrip("/")
response = await asset_handler(request, path or "index.html")
response = asgineer.utils.normalize_response(response)
is_page = "." not in path or path.endswith(".html")
send_stats(request, response[0], is_page=is_page)
return response
if __name__ == "__main__":
asgineer.run(main_handler, "uvicorn", "0.0.0.0:80", log_level="warning")