feat: Cleanup code
All checks were successful
Check Code Quality / RuffCheck (push) Successful in 55s
Build Docker / BuildImage (push) Successful in 57s

This commit is contained in:
2026-02-11 17:56:51 +11:00
parent c6ff4350cc
commit d2db66d527
6 changed files with 61 additions and 32 deletions

16
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
rev: 0.9.8
hooks:
- id: uv-lock
- id: uv-export
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.14.4
hooks:
# Run the linter.
- id: ruff-check
# Run the formatter.
- id: ruff-format

View File

@@ -9,7 +9,7 @@ dependencies = [
"gunicorn>=23.0.0", "gunicorn>=23.0.0",
"python-dotenv>=1.2.1", "python-dotenv>=1.2.1",
"requests>=2.32.5", "requests>=2.32.5",
"authlib==1.3.0", "authlib>=1.3.0",
"flask-caching>=2.3.1", "flask-caching>=2.3.1",
] ]

View File

@@ -18,7 +18,6 @@ from datetime import datetime
import dotenv import dotenv
from authlib.integrations.flask_client import OAuth from authlib.integrations.flask_client import OAuth
from flask_caching import Cache from flask_caching import Cache
from werkzeug.middleware.proxy_fix import ProxyFix
dotenv.load_dotenv() dotenv.load_dotenv()
@@ -27,22 +26,23 @@ app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
app.secret_key = os.getenv("APP_SECRET_KEY", os.urandom(24)) app.secret_key = os.getenv("APP_SECRET_KEY", os.urandom(24))
# Cache Configuration # Cache Configuration
cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 300}) cache = Cache(app, config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 300})
# OAuth Configuration # OAuth Configuration
oauth = OAuth(app) oauth = OAuth(app)
oauth.register( oauth.register(
name='authentik', name="authentik",
server_metadata_url=os.getenv('AUTHENTIK_METADATA_URL'), server_metadata_url=os.getenv("AUTHENTIK_METADATA_URL"),
client_id=os.getenv('AUTHENTIK_CLIENT_ID'), client_id=os.getenv("AUTHENTIK_CLIENT_ID"),
client_secret=os.getenv('AUTHENTIK_CLIENT_SECRET'), client_secret=os.getenv("AUTHENTIK_CLIENT_SECRET"),
client_kwargs={ client_kwargs={
'scope': 'openid profile email', "scope": "openid profile email",
} },
) )
def load_services(): def load_services():
with open('services.json', 'r') as f: with open("services.json", "r") as f:
return json.load(f) return json.load(f)
@@ -78,6 +78,7 @@ def send_assets(path):
return render_template("404.html"), 404 return render_template("404.html"), 404
@app.route("/services/<string:category>/<string:service>.png") @app.route("/services/<string:category>/<string:service>.png")
@cache.cached(timeout=3600, query_string=True) @cache.cached(timeout=3600, query_string=True)
def service_images(category: str, service: str): def service_images(category: str, service: str):
@@ -88,10 +89,14 @@ def service_images(category: str, service: str):
if "icon" in svc: if "icon" in svc:
# If the icon isn't a URL, try to serve it from the filesystem # If the icon isn't a URL, try to serve it from the filesystem
if not svc["icon"].startswith("http"): if not svc["icon"].startswith("http"):
icon_path = os.path.join("templates/assets/img/services", svc["icon"]) icon_path = os.path.join(
"templates/assets/img/services", svc["icon"]
)
if os.path.isfile(icon_path): if os.path.isfile(icon_path):
return make_response( return make_response(
open(icon_path, "rb").read(), 200, {"Content-Type": "image/png"} open(icon_path, "rb").read(),
200,
{"Content-Type": "image/png"},
) )
else: else:
print(f"Icon file not found for {service} at {icon_path}") print(f"Icon file not found for {service} at {icon_path}")
@@ -102,18 +107,20 @@ def service_images(category: str, service: str):
req = requests.get(svc["icon"], timeout=5) req = requests.get(svc["icon"], timeout=5)
if req.status_code == 200: if req.status_code == 200:
return make_response( return make_response(
req.content, 200, {"Content-Type": req.headers["Content-Type"]} req.content,
200,
{"Content-Type": req.headers["Content-Type"]},
) )
except Exception as e: except Exception as e:
print(f"Failed to fetch icon for {service}: {e}") print(f"Failed to fetch icon for {service}: {e}")
# Read default favicon into memory to allow caching (pickling) # Read default favicon into memory to allow caching (pickling)
with open("templates/assets/img/favicon.png", "rb") as f: with open("templates/assets/img/favicon.png", "rb") as f:
return make_response(f.read(), 200, {"Content-Type": "image/png"}) return make_response(f.read(), 200, {"Content-Type": "image/png"})
return render_template("404.html"), 404 return render_template("404.html"), 404
# region Special routes # region Special routes
@app.route("/favicon.png") @app.route("/favicon.png")
def faviconPNG(): def faviconPNG():
@@ -137,11 +144,13 @@ def wellknown(path):
def index(): def index():
# Get current time in the format "dd MMM YYYY hh:mm AM/PM" # Get current time in the format "dd MMM YYYY hh:mm AM/PM"
current_datetime = datetime.now().strftime("%d %b %Y %I:%M %p") current_datetime = datetime.now().strftime("%d %b %Y %I:%M %p")
services = load_services() services = load_services()
user = session.get('user') user = session.get("user")
return render_template("index.html", datetime=current_datetime, services=services, user=user) return render_template(
"index.html", datetime=current_datetime, services=services, user=user
)
@app.route("/<path:path>") @app.route("/<path:path>")
@@ -205,34 +214,38 @@ def not_found(e):
# endregion # endregion
# region Auth routes # region Auth routes
@app.route('/login')
@app.route("/login")
def login(): def login():
redirect_uri = url_for('auth_callback', _external=True) redirect_uri = url_for("auth_callback", _external=True)
return oauth.authentik.authorize_redirect(redirect_uri) # type: ignore return oauth.authentik.authorize_redirect(redirect_uri) # type: ignore
@app.route('/auth/callback') @app.route("/auth/callback")
def auth_callback(): def auth_callback():
token = oauth.authentik.authorize_access_token() # type: ignore token = oauth.authentik.authorize_access_token() # type: ignore
user = token.get('userinfo') user = token.get("userinfo")
if user: if user:
session['user'] = user session["user"] = user
return redirect(url_for('index')) return redirect(url_for("index"))
@app.route('/logout') @app.route("/logout")
def logout(): def logout():
session.pop('user', None) session.pop("user", None)
return redirect(url_for('index')) return redirect(url_for("index"))
# endregion # endregion
# region Error handling # region Error handling
@app.errorhandler(InternalServerError) @app.errorhandler(InternalServerError)
def handle_internal_server_error(e: InternalServerError): def handle_internal_server_error(e: InternalServerError):
return render_template("500.html", message=e.original_exception), 500 return render_template("500.html", message=e.original_exception), 500
# endregion # endregion

View File

@@ -4,7 +4,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nathan.Woodburn/</title> <title>Woodburn/</title>
<link rel="icon" href="/assets/img/favicon.png" type="image/png"> <link rel="icon" href="/assets/img/favicon.png" type="image/png">
<link rel="stylesheet" href="/assets/css/404.css"> <link rel="stylesheet" href="/assets/css/404.css">
</head> </head>

View File

@@ -4,7 +4,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nathan.Woodburn/</title> <title>Woodburn/</title>
<link rel="icon" href="/assets/img/favicon.png" type="image/png"> <link rel="icon" href="/assets/img/favicon.png" type="image/png">
<link rel="stylesheet" href="/assets/css/500.css"> <link rel="stylesheet" href="/assets/css/500.css">
</head> </head>

2
uv.lock generated
View File

@@ -555,7 +555,7 @@ dev = [
[package.metadata] [package.metadata]
requires-dist = [ requires-dist = [
{ name = "authlib", specifier = "==1.3.0" }, { name = "authlib", specifier = ">=1.3.0" },
{ name = "flask", specifier = ">=3.1.2" }, { name = "flask", specifier = ">=3.1.2" },
{ name = "flask-caching", specifier = ">=2.3.1" }, { name = "flask-caching", specifier = ">=2.3.1" },
{ name = "gunicorn", specifier = ">=23.0.0" }, { name = "gunicorn", specifier = ">=23.0.0" },