Skip to content

Commit ea3dbeb

Browse files
committedJan 24, 2016
Misc cleanup
1 parent 3792ee3 commit ea3dbeb

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Setting up the webpage
55
----------------------
66

77
You will have to install node.js, doT.js and their dependencies to compile
8-
the serverlist webpage template.
8+
the server list webpage template.
99

1010
First install node.js, e.g.:
1111

@@ -82,7 +82,7 @@ Setting up the server
8282

8383
$ ./server.py
8484
$ # Or for production:
85-
$ uwsgi -s /tmp/serverlist.sock --plugin python -w server:app --enable-threads
85+
$ uwsgi -s /tmp/minetest-master.sock --plugin python -w server:app --enable-threads
8686
$ # Then configure according to http://flask.pocoo.org/docs/deploying/uwsgi/
8787

8888
7. (optional) Configure the proxy server, if any. You should make the server

‎server.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from flask import Flask, request, send_from_directory
77

88

9+
# Set up scheduler
910
sched = BackgroundScheduler(timezone="UTC")
1011
sched.start()
1112

@@ -17,6 +18,8 @@
1718
app.config.from_pyfile("config.py")
1819

1920

21+
# Views
22+
2023
@app.route("/")
2124
def index():
2225
return app.send_static_file("index.html")
@@ -36,7 +39,7 @@ def announce():
3639
if ip.startswith("::ffff:"):
3740
ip = ip[7:]
3841

39-
data = request.form["json"] if request.method == "POST" else request.args["json"]
42+
data = request.values["json"]
4043

4144
if len(data) > 5000:
4245
return "JSON data is too big.", 413
@@ -52,7 +55,11 @@ def announce():
5255
if not "action" in server:
5356
return "Missing action field.", 400
5457

55-
if server["action"] == "start":
58+
action = server["action"]
59+
if action not in ("start", "update", "delete"):
60+
return "Invalid action field.", 400
61+
62+
if action == "start":
5663
server["uptime"] = 0
5764

5865
server["ip"] = ip
@@ -65,9 +72,9 @@ def announce():
6572
server["port"] = int(server["port"])
6673
#### End compatability code ####
6774

68-
old = serverList.get(server["ip"], server["port"])
75+
old = serverList.get(ip, server["port"])
6976

70-
if server["action"] == "delete":
77+
if action == "delete":
7178
if not old:
7279
return "Server not found.", 500
7380
serverList.remove(old)
@@ -76,7 +83,7 @@ def announce():
7683
elif not checkRequest(server):
7784
return "Invalid JSON data.", 400
7885

79-
if server["action"] != "start" and not old:
86+
if action == "update" and not old:
8087
if app.config["ALLOW_UPDATE_WITHOUT_OLD"]:
8188
old = server
8289
old["start"] = time.time()
@@ -88,15 +95,15 @@ def announce():
8895

8996
server["update_time"] = time.time()
9097

91-
server["start"] = time.time() if server["action"] == "start" else old["start"]
98+
server["start"] = time.time() if action == "start" else old["start"]
9299

93100
if "clients_list" in server:
94101
server["clients"] = len(server["clients_list"])
95102

96103
server["clients_top"] = max(server["clients"], old["clients_top"]) if old else server["clients"]
97104

98105
# Make sure that startup options are saved
99-
if server["action"] != "start":
106+
if action == "update":
100107
for field in ("dedicated", "rollback", "mapgen", "privs",
101108
"can_see_far_names", "mods"):
102109
if field in old:
@@ -117,11 +124,10 @@ def announce():
117124

118125
return "Thanks, your request has been filed.", 202
119126

120-
def purgeOld():
121-
serverList.purgeOld()
122-
123-
sched.add_job(purgeOld, "interval", seconds=60, coalesce=True, max_instances=1)
127+
sched.add_job(lambda: serverList.purgeOld(), "interval",
128+
seconds=60, coalesce=True, max_instances=1)
124129

130+
# Utilities
125131

126132
# Returns ping time in seconds (up), False (down), or None (error).
127133
def serverUp(info):
@@ -194,7 +200,7 @@ def checkRequest(server):
194200
# Accept strings in boolean fields but convert it to a
195201
# boolean, because old servers sent some booleans as strings.
196202
if data[1] == "bool" and type(server[name]).__name__ == "str":
197-
server[name] = True if server[name].lower() in ["true", "1"] else False
203+
server[name] = True if server[name].lower() in ("true", "1") else False
198204
continue
199205
# clients_max was sent as a string instead of an integer
200206
if name == "clients_max" and type(server[name]).__name__ == "str":

0 commit comments

Comments
 (0)
Please sign in to comment.