forked from Chaosthebot/Chaos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
52 lines (41 loc) · 1.5 KB
/
webserver.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
import hug
import json
import linecache
import logging
from http import server
__log = logging.getLogger("webserver")
responses = server.BaseHTTPRequestHandler.responses
index_content = open("/root/workspace/Chaos/server/index.html", "r").read()
errorpage_content = open("/root/workspace/Chaos/server/error.html", "r").read()
@hug.get("/", output=hug.output_format.html)
def render_index():
return index_content
@hug.get("/api/errorpage", output=hug.output_format.html)
def render_error(code: hug.types.number = 500):
status = responses[code]
return errorpage_content % {
"code": code,
"message": status[0],
"explain": status[1]
}
@hug.get("/api/voters", examples=["amount=20", "amount=0"])
def get_voters(amount: hug.types.number = 20):
try:
voters = sorted(json.loads(
linecache.getline("/root/workspace/Chaos/server/voters.json", 1)).items(),
key=lambda x: x[1], reverse=True)
if amount > 0:
voters = voters[:amount]
return {x[0]: x[1] for x in voters}
except:
__log.exception("Failed to read voters!")
@hug.get("/api/meritocracy", examples=["amount=5", "amount=0"])
def get_meritocracy(amount: hug.types.number = 20):
try:
meritocracy = json.loads(
linecache.getline("/root/workspace/Chaos/server/meritocracy.json", 1))
if amount > 0:
meritocracy = meritocracy[:amount]
return meritocracy
except:
__log.exception("Failed to read meritocracy!")