feat: Move error responses to new function in tools.py
All checks were successful
Build Docker / BuildImage (push) Successful in 1m6s
All checks were successful
Build Docker / BuildImage (push) Successful in 1m6s
This commit is contained in:
27
tools.py
27
tools.py
@@ -1,7 +1,15 @@
|
||||
from flask import Request
|
||||
from flask import Request, render_template, jsonify
|
||||
import os
|
||||
from functools import cache
|
||||
|
||||
def getClientIP(request):
|
||||
x_forwarded_for = request.headers.get("X-Forwarded-For")
|
||||
if x_forwarded_for:
|
||||
ip = x_forwarded_for.split(",")[0]
|
||||
else:
|
||||
ip = request.remote_addr
|
||||
return ip
|
||||
|
||||
def isCurl(request: Request) -> bool:
|
||||
"""
|
||||
Check if the request is from curl
|
||||
@@ -48,4 +56,19 @@ def getAddress(coin: str) -> str:
|
||||
def getFilePath(name, path):
|
||||
for root, dirs, files in os.walk(path):
|
||||
if name in files:
|
||||
return os.path.join(root, name)
|
||||
return os.path.join(root, name)
|
||||
|
||||
def error_response(request: Request, message: str = "404 Not Found", code: int = 404):
|
||||
if isCurl(request):
|
||||
return jsonify(
|
||||
{
|
||||
"status": code,
|
||||
"message": message,
|
||||
"ip": getClientIP(request),
|
||||
}
|
||||
), code
|
||||
|
||||
# Check if <error code>.html exists in templates
|
||||
if os.path.isfile(f"templates/{code}.html"):
|
||||
return render_template(f"{code}.html"), code
|
||||
return render_template("404.html"), code
|
||||
Reference in New Issue
Block a user