better authenticarion
This commit is contained in:
+29
-2
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user