Files
SMANAGE/backup/_flask_backup/TempLateModule/Main.py
T

81 lines
3.2 KiB
Python

from tools import *
from AppBridge import *
from Utils.Config import *
from Utils.Lib import *
Main = flask.Blueprint("Main", __name__, url_prefix='/', subdomain = "www", static_folder = JoinPath(FlaskTempLate), template_folder = TempLate, static_url_path="/")
@Main.route("/")
def MainIndex():
if flask.request.args.get("UserUUID") or flask.request.args.get("UserName"): return flask.render_template("/usercard.html")
return ProcessTemplate("index.html")
@Main.route("/index.html")
def MainIndexHtml():
return flask.redirect(flask.url_for("Main.MainIndex"))
@Main.route("/favicon.ico")
def MainFavicon():
return flask.Response(openr(JoinPath(TempLate,"favicon.ico"), 2), mimetype='image/x-icon')
@Main.route("/<file>")
def MainElseHtml(file):
if os.path.isfile(JoinPath(TempLate,file)):
if file.split(".")[-1].lower() == "html": return ProcessTemplate(file)
return openr(JoinPath(TempLate,file), "2")
else:
flask.abort(404)
@Main.after_request
def MainAfterIndex(r: flask.Response):
if os.path.isfile(JoinPath(FlaskTempLate, flask.request.path[1:])) and flask.request.path.split(".")[-1].lower() == "html" and ".".join(flask.request.path.split(".")[:-1]).count("/") != 1:
return flask.make_response(ProcessTemplate(flask.request.path[1:], DefaultUse = False))
if r.status_code != 404: return r
if os.path.isfile(JoinPath(FlaskTempLate, flask.request.path[1:], "index.html")):
return flask.make_response(ProcessTemplate(flask.request.path[1:] + "/index.html"))
return r
@Main.errorhandler(404)
def MainNotFound(e):
return ProcessTemplate("/Errors/404.html",e=e,path=flask.request.full_path,time=time.time()), 404
@Main.errorhandler(500)
def MainInternalServerError(e):
print(traceback.format_exc())
return ProcessTemplate("/Errors/500.html",e=e,path=flask.request.full_path,time=time.time(),errorcode=traceback.format_exc()), 500
@Main.errorhandler(503)
def MainServiceTemporarilyUnavailable(e):
return ProcessTemplate("/Errors/503.html",e=e,path=flask.request.full_path,time=time.time(),errorcode=traceback.format_exc()), 503
@App.errorhandler(429)
def RatelimitHandler(e):
return flask.jsonify({"code": 429, "msg": f"Too Many Requests, Rate Limit Exceeded. Try Again In {e.description.split()[-2]}."}), 429
@Main.errorhandler(flask_wtf.csrf.CSRFError)
def MainCSRError(e):
return ProcessTemplate("/Errors/_CSRFError",e=e,path=flask.request.full_path,time=time.time(),errorcode=traceback.format_exc()), 503
@Main.route("/robots.txt")
def MainRobots():
if App.config['WEBSITE_DEBUG']:
return openr(JoinPath(TempLate,"robots_d.txt"))
return openr(JoinPath(TempLate,"robots_a.txt"))
@socketio.on_error_default
def default_ErrorHandler(e):
traceback.print_exc()
print(f"WebSocket Error: {str(e)}")
@App.after_request
def handle_ssl_errors(response):
try:
return response
except ssl.SSLEOFError:
App.logger.warning("Client SSL connection terminated abruptly")
return None
@socketio.on('*')
def catch_all(event, data):
print(f"📡 Socket Event [Name: {event} | Data: {data}]")