Misc cleanup
This commit is contained in:
parent
3792ee39b2
commit
ea3dbeb889
@ -5,7 +5,7 @@ Setting up the webpage
|
|||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
You will have to install node.js, doT.js and their dependencies to compile
|
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.:
|
First install node.js, e.g.:
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ Setting up the server
|
|||||||
|
|
||||||
$ ./server.py
|
$ ./server.py
|
||||||
$ # Or for production:
|
$ # 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/
|
$ # Then configure according to http://flask.pocoo.org/docs/deploying/uwsgi/
|
||||||
|
|
||||||
7. (optional) Configure the proxy server, if any. You should make the server
|
7. (optional) Configure the proxy server, if any. You should make the server
|
||||||
|
30
server.py
30
server.py
@ -6,6 +6,7 @@ from apscheduler.schedulers.background import BackgroundScheduler
|
|||||||
from flask import Flask, request, send_from_directory
|
from flask import Flask, request, send_from_directory
|
||||||
|
|
||||||
|
|
||||||
|
# Set up scheduler
|
||||||
sched = BackgroundScheduler(timezone="UTC")
|
sched = BackgroundScheduler(timezone="UTC")
|
||||||
sched.start()
|
sched.start()
|
||||||
|
|
||||||
@ -17,6 +18,8 @@ if os.path.isfile(os.path.join(app.root_path, "config.py")):
|
|||||||
app.config.from_pyfile("config.py")
|
app.config.from_pyfile("config.py")
|
||||||
|
|
||||||
|
|
||||||
|
# Views
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
return app.send_static_file("index.html")
|
return app.send_static_file("index.html")
|
||||||
@ -36,7 +39,7 @@ def announce():
|
|||||||
if ip.startswith("::ffff:"):
|
if ip.startswith("::ffff:"):
|
||||||
ip = ip[7:]
|
ip = ip[7:]
|
||||||
|
|
||||||
data = request.form["json"] if request.method == "POST" else request.args["json"]
|
data = request.values["json"]
|
||||||
|
|
||||||
if len(data) > 5000:
|
if len(data) > 5000:
|
||||||
return "JSON data is too big.", 413
|
return "JSON data is too big.", 413
|
||||||
@ -52,7 +55,11 @@ def announce():
|
|||||||
if not "action" in server:
|
if not "action" in server:
|
||||||
return "Missing action field.", 400
|
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["uptime"] = 0
|
||||||
|
|
||||||
server["ip"] = ip
|
server["ip"] = ip
|
||||||
@ -65,9 +72,9 @@ def announce():
|
|||||||
server["port"] = int(server["port"])
|
server["port"] = int(server["port"])
|
||||||
#### End compatability code ####
|
#### 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:
|
if not old:
|
||||||
return "Server not found.", 500
|
return "Server not found.", 500
|
||||||
serverList.remove(old)
|
serverList.remove(old)
|
||||||
@ -76,7 +83,7 @@ def announce():
|
|||||||
elif not checkRequest(server):
|
elif not checkRequest(server):
|
||||||
return "Invalid JSON data.", 400
|
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"]:
|
if app.config["ALLOW_UPDATE_WITHOUT_OLD"]:
|
||||||
old = server
|
old = server
|
||||||
old["start"] = time.time()
|
old["start"] = time.time()
|
||||||
@ -88,7 +95,7 @@ def announce():
|
|||||||
|
|
||||||
server["update_time"] = time.time()
|
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:
|
if "clients_list" in server:
|
||||||
server["clients"] = len(server["clients_list"])
|
server["clients"] = len(server["clients_list"])
|
||||||
@ -96,7 +103,7 @@ def announce():
|
|||||||
server["clients_top"] = max(server["clients"], old["clients_top"]) if old else server["clients"]
|
server["clients_top"] = max(server["clients"], old["clients_top"]) if old else server["clients"]
|
||||||
|
|
||||||
# Make sure that startup options are saved
|
# Make sure that startup options are saved
|
||||||
if server["action"] != "start":
|
if action == "update":
|
||||||
for field in ("dedicated", "rollback", "mapgen", "privs",
|
for field in ("dedicated", "rollback", "mapgen", "privs",
|
||||||
"can_see_far_names", "mods"):
|
"can_see_far_names", "mods"):
|
||||||
if field in old:
|
if field in old:
|
||||||
@ -117,11 +124,10 @@ def announce():
|
|||||||
|
|
||||||
return "Thanks, your request has been filed.", 202
|
return "Thanks, your request has been filed.", 202
|
||||||
|
|
||||||
def purgeOld():
|
sched.add_job(lambda: serverList.purgeOld(), "interval",
|
||||||
serverList.purgeOld()
|
seconds=60, coalesce=True, max_instances=1)
|
||||||
|
|
||||||
sched.add_job(purgeOld, "interval", seconds=60, coalesce=True, max_instances=1)
|
|
||||||
|
|
||||||
|
# Utilities
|
||||||
|
|
||||||
# Returns ping time in seconds (up), False (down), or None (error).
|
# Returns ping time in seconds (up), False (down), or None (error).
|
||||||
def serverUp(info):
|
def serverUp(info):
|
||||||
@ -194,7 +200,7 @@ def checkRequest(server):
|
|||||||
# Accept strings in boolean fields but convert it to a
|
# Accept strings in boolean fields but convert it to a
|
||||||
# boolean, because old servers sent some booleans as strings.
|
# boolean, because old servers sent some booleans as strings.
|
||||||
if data[1] == "bool" and type(server[name]).__name__ == "str":
|
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
|
continue
|
||||||
# clients_max was sent as a string instead of an integer
|
# clients_max was sent as a string instead of an integer
|
||||||
if name == "clients_max" and type(server[name]).__name__ == "str":
|
if name == "clients_max" and type(server[name]).__name__ == "str":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user