From 5ef2ced310868c5c7c406ea3dea372362318f3f1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 8 Mar 2024 18:39:28 +0100 Subject: [PATCH] Add subpath support (#7) --- README.md | 13 +++++++------ src/app.py | 2 +- src/routes/index.py | 4 ++-- src/routes/login.py | 2 +- src/routes/read.py | 2 +- src/templates/header.html | 2 +- src/templates/index.html | 4 +++- src/templates/login.html | 4 ++-- src/templates/post.html | 5 ++++- src/templates/read.html | 2 +- src/templates/tz.html | 5 ++++- src/utils/config.py | 9 +++++++++ 12 files changed, 36 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 040f4cc..d76bff5 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,13 @@ personal diary with txt style, can be private! ## environment variables -| name | description | required | -| :----------------: | :-------------------: | :-------------: | -| `TD_USERNAME` | user name | no | -| `TD_USERPASS` | user password | yes | -| `TD_PRIVATE` | post visibility | empty = `false` | -| `TD_LOGINLIFETIME` | login lifetime (days) | empty = `7` | +| name | description | required | +| :----------------: | :---------------------------------: | :-------------: | +| `TD_USERNAME` | user name | no | +| `TD_USERPASS` | user password | yes | +| `TD_PRIVATE` | post visibility | empty = `false` | +| `TD_LOGINLIFETIME` | login lifetime (days) | empty = `7` | +| `TD_BASEROUTE` | base route, must start with a slash | no | ## volumes (docker) diff --git a/src/app.py b/src/app.py index 79115d3..28e35d7 100644 --- a/src/app.py +++ b/src/app.py @@ -11,7 +11,7 @@ app = Flask(__name__, static_url_path="/") for file in glob("*/routes/*.py"): module = file.replace("/", ".").split(".")[-2] exec(f"from routes.{module} import router as {module}") - exec(f"app.register_blueprint({module})") + exec(f"app.register_blueprint({module}, url_prefix='{Config.base}')") app.secret_key = urandom(12) diff --git a/src/routes/index.py b/src/routes/index.py index 189dc6a..ea39417 100644 --- a/src/routes/index.py +++ b/src/routes/index.py @@ -38,11 +38,11 @@ def new_post(post_id: int | None) -> Response: update = create_post(filename, content) flash( - f"post {'updated' if update else 'created'}." + f"post {'updated' if update else 'created'}." ) else: flash(f"invalid post: {content}") else: flash("you can't do that.") - return redirect("/") + return redirect(Config.base) diff --git a/src/routes/login.py b/src/routes/login.py index 76d6a02..3d2c0cd 100644 --- a/src/routes/login.py +++ b/src/routes/login.py @@ -30,4 +30,4 @@ def check() -> Response: else: flash("already logged.") - return redirect("/") + return redirect(Config.base) diff --git a/src/routes/read.py b/src/routes/read.py index 4d67c89..a719efd 100644 --- a/src/routes/read.py +++ b/src/routes/read.py @@ -37,4 +37,4 @@ def remove_post(file: int) -> Response: else: flash(f"{filename} doesn't exists") - return redirect("/") + return redirect(Config.base) diff --git a/src/templates/header.html b/src/templates/header.html index b417d24..ef0319c 100644 --- a/src/templates/header.html +++ b/src/templates/header.html @@ -2,7 +2,7 @@

diary{{ " of " + config.user.name if config.user.name else "" }}

{% if not (login_page or config.is_logged()) %} - + {% for p in posts: %} -
  • {{ p }}.txt
  • +
  • + {{ p }}.txt +
  • {% endfor %} {% endif %} diff --git a/src/templates/login.html b/src/templates/login.html index b15bb54..ecb4807 100644 --- a/src/templates/login.html +++ b/src/templates/login.html @@ -10,14 +10,14 @@

    login page

    -
    + - Cancel + Cancel
    diff --git a/src/templates/post.html b/src/templates/post.html index b96229d..835aabd 100644 --- a/src/templates/post.html +++ b/src/templates/post.html @@ -1,5 +1,8 @@ {% if config.is_logged() %} -
    +
    diff --git a/src/templates/read.html b/src/templates/read.html index 3f496aa..03ab0a8 100644 --- a/src/templates/read.html +++ b/src/templates/read.html @@ -20,7 +20,7 @@ diff --git a/src/templates/tz.html b/src/templates/tz.html index 4e0b184..6508d76 100644 --- a/src/templates/tz.html +++ b/src/templates/tz.html @@ -1,6 +1,9 @@ {% if "timezone" not in session %} {% endif %} diff --git a/src/utils/config.py b/src/utils/config.py index 7c2ca40..c1d9dcf 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -6,6 +6,7 @@ VAR_USERNAME = "TD_USERNAME" VAR_USERPASS = "TD_USERPASS" VAR_PRIVATE = "TD_PRIVATE" VAR_LOGLIFETIME = "TD_LOGINLIFETIME" +VAR_BASE = "TD_BASEROUTE" class User: @@ -47,6 +48,14 @@ class Config: float(envar[VAR_LOGLIFETIME]) if VAR_LOGLIFETIME in envar else 7.0 ) + # base + base = envar[VAR_BASE] if VAR_BASE in envar else "/" + + @staticmethod + def sanitized_base() -> str: + """Sanitized base used in templates""" + return "" if Config.base == "/" else Config.base + @staticmethod def is_logged() -> bool: """where the info about connection is stored"""