Skip to content

Commit

Permalink
Misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowNinja committed Jan 24, 2016
1 parent 3792ee3 commit ea3dbeb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -5,7 +5,7 @@ Setting up the webpage
----------------------

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

First install node.js, e.g.:

Expand Down Expand Up @@ -82,7 +82,7 @@ Setting up the server

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

7. (optional) Configure the proxy server, if any. You should make the server
Expand Down
30 changes: 18 additions & 12 deletions server.py
Expand Up @@ -6,6 +6,7 @@
from flask import Flask, request, send_from_directory


# Set up scheduler
sched = BackgroundScheduler(timezone="UTC")
sched.start()

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


# Views

@app.route("/")
def index():
return app.send_static_file("index.html")
Expand All @@ -36,7 +39,7 @@ def announce():
if ip.startswith("::ffff:"):
ip = ip[7:]

data = request.form["json"] if request.method == "POST" else request.args["json"]
data = request.values["json"]

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

if server["action"] == "start":
action = server["action"]
if action not in ("start", "update", "delete"):
return "Invalid action field.", 400

if action == "start":
server["uptime"] = 0

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

old = serverList.get(server["ip"], server["port"])
old = serverList.get(ip, server["port"])

if server["action"] == "delete":
if action == "delete":
if not old:
return "Server not found.", 500
serverList.remove(old)
Expand All @@ -76,7 +83,7 @@ def announce():
elif not checkRequest(server):
return "Invalid JSON data.", 400

if server["action"] != "start" and not old:
if action == "update" and not old:
if app.config["ALLOW_UPDATE_WITHOUT_OLD"]:
old = server
old["start"] = time.time()
Expand All @@ -88,15 +95,15 @@ def announce():

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

server["start"] = time.time() if server["action"] == "start" else old["start"]
server["start"] = time.time() if action == "start" else old["start"]

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

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

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

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

def purgeOld():
serverList.purgeOld()

sched.add_job(purgeOld, "interval", seconds=60, coalesce=True, max_instances=1)
sched.add_job(lambda: serverList.purgeOld(), "interval",
seconds=60, coalesce=True, max_instances=1)

# Utilities

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

0 comments on commit ea3dbeb

Please sign in to comment.