feat: Add error message to header for HTML error responses
This commit is contained in:
30
server.py
30
server.py
@@ -149,6 +149,18 @@ def javascript_get(name):
|
|||||||
return error_response(request)
|
return error_response(request)
|
||||||
return send_from_directory("templates/assets/js", request.path.split("/")[-1])
|
return send_from_directory("templates/assets/js", request.path.split("/")[-1])
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/download/<path:path>")
|
||||||
|
def download_get(path):
|
||||||
|
if path not in DOWNLOAD_ROUTES:
|
||||||
|
return error_response(request, message="Invalid download")
|
||||||
|
# Check if file exists
|
||||||
|
path = DOWNLOAD_ROUTES[path]
|
||||||
|
if os.path.isfile(path):
|
||||||
|
return send_file(path)
|
||||||
|
|
||||||
|
return error_response(request, message="File not found")
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
# region PWA routes
|
# region PWA routes
|
||||||
|
|
||||||
@@ -553,7 +565,7 @@ def qrcode_get(data):
|
|||||||
qr.make()
|
qr.make()
|
||||||
|
|
||||||
qr_image: Image.Image = qr.make_image(
|
qr_image: Image.Image = qr.make_image(
|
||||||
fill_color="black", back_color="white").convert('RGB') # type: ignore
|
fill_color="black", back_color="white").convert('RGB') # type: ignore
|
||||||
|
|
||||||
# Add logo
|
# Add logo
|
||||||
logo = Image.open("templates/assets/img/favicon/logo.png")
|
logo = Image.open("templates/assets/img/favicon/logo.png")
|
||||||
@@ -583,18 +595,6 @@ def supersecretpath_get():
|
|||||||
return render_template("ascii.html", ascii_art=ascii_art_html)
|
return render_template("ascii.html", ascii_art=ascii_art_html)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/download/<path:path>")
|
|
||||||
def download_get(path):
|
|
||||||
if path not in DOWNLOAD_ROUTES:
|
|
||||||
return error_response(request, message="Invalid download")
|
|
||||||
# Check if file exists
|
|
||||||
path = DOWNLOAD_ROUTES[path]
|
|
||||||
if os.path.isfile(path):
|
|
||||||
return send_file(path)
|
|
||||||
|
|
||||||
return error_response(request, message="File not found")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/hosting/send-enquiry", methods=["POST"])
|
@app.route("/hosting/send-enquiry", methods=["POST"])
|
||||||
def hosting_post():
|
def hosting_post():
|
||||||
global EMAIL_REQUEST_COUNT
|
global EMAIL_REQUEST_COUNT
|
||||||
@@ -681,7 +681,7 @@ def hosting_post():
|
|||||||
webhook_url = os.getenv("HOSTING_WEBHOOK")
|
webhook_url = os.getenv("HOSTING_WEBHOOK")
|
||||||
if not webhook_url:
|
if not webhook_url:
|
||||||
return json_response(request, "Hosting webhook not set", 500)
|
return json_response(request, "Hosting webhook not set", 500)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"content": "",
|
"content": "",
|
||||||
"embeds": [
|
"embeds": [
|
||||||
@@ -756,11 +756,13 @@ def catch_all_get(path: str):
|
|||||||
|
|
||||||
return error_response(request)
|
return error_response(request)
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
def not_found(e):
|
def not_found(e):
|
||||||
return error_response(request)
|
return error_response(request)
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True, port=5000, host="127.0.0.1")
|
app.run(debug=True, port=5000, host="127.0.0.1")
|
||||||
|
|||||||
20
tools.py
20
tools.py
@@ -1,7 +1,8 @@
|
|||||||
from flask import Request, render_template, jsonify
|
from flask import Request, render_template, jsonify, make_response
|
||||||
import os
|
import os
|
||||||
from functools import cache
|
from functools import cache
|
||||||
|
|
||||||
|
|
||||||
def getClientIP(request):
|
def getClientIP(request):
|
||||||
x_forwarded_for = request.headers.get("X-Forwarded-For")
|
x_forwarded_for = request.headers.get("X-Forwarded-For")
|
||||||
if x_forwarded_for:
|
if x_forwarded_for:
|
||||||
@@ -10,6 +11,7 @@ def getClientIP(request):
|
|||||||
ip = request.remote_addr
|
ip = request.remote_addr
|
||||||
return ip
|
return ip
|
||||||
|
|
||||||
|
|
||||||
def getGitCommit():
|
def getGitCommit():
|
||||||
# if .git exists, get the latest commit hash
|
# if .git exists, get the latest commit hash
|
||||||
if os.path.isdir(".git"):
|
if os.path.isdir(".git"):
|
||||||
@@ -34,7 +36,7 @@ def getGitCommit():
|
|||||||
def isCurl(request: Request) -> bool:
|
def isCurl(request: Request) -> bool:
|
||||||
"""
|
"""
|
||||||
Check if the request is from curl
|
Check if the request is from curl
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
request (Request): The Flask request object
|
request (Request): The Flask request object
|
||||||
Returns:
|
Returns:
|
||||||
@@ -47,6 +49,7 @@ def isCurl(request: Request) -> bool:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def isCrawler(request: Request) -> bool:
|
def isCrawler(request: Request) -> bool:
|
||||||
"""
|
"""
|
||||||
Check if the request is from a web crawler (e.g., Googlebot, Bingbot)
|
Check if the request is from a web crawler (e.g., Googlebot, Bingbot)
|
||||||
@@ -79,6 +82,7 @@ def getFilePath(name, path):
|
|||||||
if name in files:
|
if name in files:
|
||||||
return os.path.join(root, name)
|
return os.path.join(root, name)
|
||||||
|
|
||||||
|
|
||||||
def json_response(request: Request, message: str = "404 Not Found", code: int = 404):
|
def json_response(request: Request, message: str = "404 Not Found", code: int = 404):
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
@@ -92,8 +96,14 @@ def json_response(request: Request, message: str = "404 Not Found", code: int =
|
|||||||
def error_response(request: Request, message: str = "404 Not Found", code: int = 404, force_json: bool = False):
|
def error_response(request: Request, message: str = "404 Not Found", code: int = 404, force_json: bool = False):
|
||||||
if force_json or isCurl(request):
|
if force_json or isCurl(request):
|
||||||
return json_response(request, message, code)
|
return json_response(request, message, code)
|
||||||
|
|
||||||
# Check if <error code>.html exists in templates
|
# Check if <error code>.html exists in templates
|
||||||
|
response = make_response(render_template(
|
||||||
|
"404.html", code=code, message=message), code)
|
||||||
if os.path.isfile(f"templates/{code}.html"):
|
if os.path.isfile(f"templates/{code}.html"):
|
||||||
return render_template(f"{code}.html"), code
|
response = make_response(render_template(
|
||||||
return render_template("404.html"), code
|
f"{code}.html", code=code, message=message), code)
|
||||||
|
|
||||||
|
# Add message to response headers
|
||||||
|
response.headers["X-Error-Message"] = message
|
||||||
|
return response
|
||||||
|
|||||||
Reference in New Issue
Block a user