better authenticarion

This commit is contained in:
2026-08-30 11:33:13 +02:00
parent fcae3569fc
commit ea591fc8fb
10 changed files with 299 additions and 16 deletions
+29 -2
View File
@@ -3,7 +3,7 @@ from __future__ import annotations
import logging
import os
from flask import Flask, Response, abort, jsonify, render_template
from flask import Flask, Response, abort, jsonify, render_template, url_for
from .collector import Collector
from .config import Config
@@ -11,12 +11,34 @@ from .netatmo import NetatmoClient
from .rrd import ALIASES, PERIODS, RRD_ERROR, RRDStore, SCHEMAS
class PrefixMiddleware:
"""Mount a WSGI application below a fixed URL path."""
def __init__(self, application, prefix: str):
self.application = application
self.prefix = prefix
def __call__(self, environ, start_response):
path = environ.get("PATH_INFO", "")
if path == self.prefix or path.startswith(f"{self.prefix}/"):
environ["SCRIPT_NAME"] = environ.get("SCRIPT_NAME", "") + self.prefix
environ["PATH_INFO"] = path[len(self.prefix):] or "/"
return self.application(environ, start_response)
start_response("404 Not Found", [("Content-Type", "text/plain; charset=utf-8")])
return [b"Not Found\n"]
def create_app(test_config: dict | None = None) -> Flask:
app = Flask(__name__)
app.config.from_object(Config)
if test_config:
app.config.update(test_config)
prefix = app.config["URL_PREFIX"]
app.config["APPLICATION_ROOT"] = prefix or "/"
if prefix:
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix)
logging.basicConfig(level=app.config.get("LOG_LEVEL", "INFO"))
store = app.config.get("RRD_STORE") or RRDStore(
app.config["RRD_FOLDER"], app.config["GRAPH_WIDTH"], app.config["GRAPH_HEIGHT"]
@@ -37,7 +59,12 @@ def create_app(test_config: dict | None = None) -> Flask:
@app.get("/")
def dashboard():
return render_template("dashboard.html", rrd_names=list(SCHEMAS), periods=list(PERIODS))
return render_template(
"dashboard.html",
rrd_names=list(SCHEMAS),
periods=list(PERIODS),
graph_url_template=url_for("graph", rrd_name="__name__", period="__period__"),
)
@app.get("/health")
def health():