| 
									
										
										
										
											2020-06-21 15:37:36 +02:00
										 |  |  | """Friendly reminder that if you want to host this somewhere on the internet, that it's licensed under MIT Berserker66
 | 
					
						
							|  |  |  | So unless you're Berserker you need to include license information.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | import threading | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | from pony.flask import Pony | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  | from flask import Flask, request, redirect, url_for, render_template, Response, session, abort | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  | from flask_caching import Cache | 
					
						
							| 
									
										
										
										
											2020-06-25 03:42:05 +02:00
										 |  |  | from flaskext.autoversion import Autoversion | 
					
						
							| 
									
										
										
										
											2020-06-30 07:32:05 +02:00
										 |  |  | from flask_compress import Compress | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | from .models import * | 
					
						
							| 
									
										
										
										
											2020-06-16 11:26:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-14 09:45:23 +02:00
										 |  |  | UPLOAD_FOLDER = os.path.relpath('uploads') | 
					
						
							|  |  |  | LOGS_FOLDER = os.path.relpath('logs') | 
					
						
							| 
									
										
										
										
											2020-06-13 10:16:29 +02:00
										 |  |  | os.makedirs(LOGS_FOLDER, exist_ok=True) | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def allowed_file(filename): | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  |     return filename.endswith(('multidata', ".zip")) | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | app = Flask(__name__) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | Pony(app) | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  | app.config["SELFHOST"] = True | 
					
						
							|  |  |  | app.config["SELFLAUNCH"] = True | 
					
						
							| 
									
										
										
										
											2020-07-10 15:32:58 +02:00
										 |  |  | app.config["DEBUG"] = False | 
					
						
							|  |  |  | app.config["PORT"] = 80 | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  | app.config['MAX_CONTENT_LENGTH'] = 4 * 1024 * 1024  # 4 megabyte limit | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | # if you want persistent sessions on your server, make sure you make this a constant in your config.yaml | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | app.config["SECRET_KEY"] = os.urandom(32) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | app.config['SESSION_PERMANENT'] = True | 
					
						
							| 
									
										
										
										
											2020-06-24 08:34:53 +02:00
										 |  |  | app.config[ | 
					
						
							|  |  |  |     "WAITRESS_THREADS"] = 10  # waitress uses one thread for I/O, these are for processing of views that then get sent | 
					
						
							| 
									
										
										
										
											2020-06-14 07:44:59 +02:00
										 |  |  | app.config["PONY"] = { | 
					
						
							|  |  |  |     'provider': 'sqlite', | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     'filename': os.path.abspath('db.db3'), | 
					
						
							| 
									
										
										
										
											2020-06-14 07:44:59 +02:00
										 |  |  |     'create_db': True | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  | app.config["CACHE_TYPE"] = "simple" | 
					
						
							| 
									
										
										
										
											2020-06-25 03:42:05 +02:00
										 |  |  | app.autoversion = True | 
					
						
							|  |  |  | av = Autoversion(app) | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  | cache = Cache(app) | 
					
						
							| 
									
										
										
										
											2020-06-30 07:32:05 +02:00
										 |  |  | Compress(app) | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 08:34:53 +02:00
										 |  |  | # this local cache is risky business if app hosting is done with subprocesses as it will not sync. Waitress is fine though | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 22:48:38 +02:00
										 |  |  | @app.before_request | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | def register_session(): | 
					
						
							|  |  |  |     session.permanent = True  # technically 31 days after the last visit | 
					
						
							|  |  |  |     if not session.get("_id", None): | 
					
						
							|  |  |  |         session["_id"] = uuid4()  # uniquely identify each session without needing a login | 
					
						
							| 
									
										
										
										
											2020-06-13 10:16:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  | @app.route('/seed/<uuid:seed>') | 
					
						
							|  |  |  | def view_seed(seed: UUID): | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     seed = Seed.get(id=seed) | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  |     if not seed: | 
					
						
							| 
									
										
										
										
											2020-06-21 15:32:31 +02:00
										 |  |  |         abort(404) | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  |     return render_template("view_seed.html", seed=seed, | 
					
						
							|  |  |  |                            rooms=[room for room in seed.rooms if room.owner == session["_id"]]) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  | @app.route('/new_room/<uuid:seed>') | 
					
						
							|  |  |  | def new_room(seed: UUID): | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     seed = Seed.get(id=seed) | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  |     if not seed: | 
					
						
							|  |  |  |         abort(404) | 
					
						
							|  |  |  |     room = Room(seed=seed, owner=session["_id"], tracker=uuid4()) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     commit() | 
					
						
							|  |  |  |     return redirect(url_for("host_room", room=room.id)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 10:16:29 +02:00
										 |  |  | def _read_log(path: str): | 
					
						
							| 
									
										
										
										
											2020-06-14 08:11:56 +02:00
										 |  |  |     if os.path.exists(path): | 
					
						
							| 
									
										
										
										
											2020-06-22 03:53:00 +02:00
										 |  |  |         with open(path, encoding="utf-8-sig") as log: | 
					
						
							| 
									
										
										
										
											2020-06-14 08:11:56 +02:00
										 |  |  |             yield from log | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2020-06-14 09:45:23 +02:00
										 |  |  |         yield f"Logfile {path} does not exist. " \ | 
					
						
							|  |  |  |               f"Likely a crash during spinup of multiworld instance or it is still spinning up." | 
					
						
							| 
									
										
										
										
											2020-06-13 10:16:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  | @app.route('/log/<uuid:room>') | 
					
						
							|  |  |  | def display_log(room: UUID): | 
					
						
							| 
									
										
										
										
											2020-06-13 10:16:29 +02:00
										 |  |  |     # noinspection PyTypeChecker | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     return Response(_read_log(os.path.join("logs", str(room) + ".txt")), mimetype="text/plain;charset=UTF-8") | 
					
						
							| 
									
										
										
										
											2020-06-13 10:16:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-26 19:29:33 +02:00
										 |  |  | @app.route('/hosted/<uuid:room>', methods=['GET', 'POST']) | 
					
						
							|  |  |  | def host_room(room: UUID): | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     room = Room.get(id=room) | 
					
						
							| 
									
										
										
										
											2020-06-23 02:50:07 +02:00
										 |  |  |     if room is None: | 
					
						
							|  |  |  |         return abort(404) | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     if request.method == "POST": | 
					
						
							|  |  |  |         if room.owner == session["_id"]: | 
					
						
							|  |  |  |             cmd = request.form["cmd"] | 
					
						
							|  |  |  |             Command(room=room, commandtext=cmd) | 
					
						
							|  |  |  |             commit() | 
					
						
							| 
									
										
										
										
											2020-06-14 07:44:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 17:42:22 +02:00
										 |  |  |     with db_session: | 
					
						
							|  |  |  |         room.last_activity = datetime.utcnow()  # will trigger a spinup, if it's not already running | 
					
						
							| 
									
										
										
										
											2020-06-13 10:16:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 20:03:06 +02:00
										 |  |  |     return render_template("host_room.html", room=room) | 
					
						
							| 
									
										
										
										
											2020-06-13 08:37:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 16:59:37 +02:00
										 |  |  | from WebHostLib.customserver import run_server_process | 
					
						
							| 
									
										
										
										
											2020-07-04 23:50:18 +02:00
										 |  |  | from . import tracker, upload, landing  # to trigger app routing picking up on it |