Add subpath support (#7)

This commit is contained in:
Mylloon 2024-03-08 18:39:28 +01:00
parent 4b96b73c42
commit 5ef2ced310
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
12 changed files with 36 additions and 18 deletions

View file

@ -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)

View file

@ -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)

View file

@ -38,11 +38,11 @@ def new_post(post_id: int | None) -> Response:
update = create_post(filename, content)
flash(
f"<a href='/read/{filename}'>post {'updated' if update else 'created'}</a>."
f"<a href='{Config.sanitized_base()}/read/{filename}'>post {'updated' if update else 'created'}</a>."
)
else:
flash(f"invalid post: {content}")
else:
flash("you can't do that.")
return redirect("/")
return redirect(Config.base)

View file

@ -30,4 +30,4 @@ def check() -> Response:
else:
flash("already logged.")
return redirect("/")
return redirect(Config.base)

View file

@ -37,4 +37,4 @@ def remove_post(file: int) -> Response:
else:
flash(f"{filename} doesn't exists")
return redirect("/")
return redirect(Config.base)

View file

@ -2,7 +2,7 @@
<h1>diary{{ " of " + config.user.name if config.user.name else "" }}</h1>
{% if not (login_page or config.is_logged()) %}
<a href="/login">
<a href="{{ config.sanitized_base() }}/login">
<svg width="30px" fill="none" viewBox="0 0 24 24">
<path
stroke="#000"

View file

@ -18,7 +18,9 @@
{% else %}
<ul>
{% for p in posts: %}
<li><a href="read/{{ p }}">{{ p }}.txt</a></li>
<li>
<a href="{{ config.sanitized_base() }}/read/{{ p }}">{{ p }}.txt</a>
</li>
{% endfor %}
</ul>
{% endif %}

View file

@ -10,14 +10,14 @@
<h3>login page</h3>
<span id="login-form">
<form action="/login" method="post">
<form action="{{ config.sanitized_base() }}/login" method="post">
<input
type="password"
name="password"
placeholder="password"
autofocus
/>
<a href="/">Cancel</a>
<a href="{{ config.base }}">Cancel</a>
<button type="submit">Login</button>
</form></span
>

View file

@ -1,5 +1,8 @@
{% if config.is_logged() %}
<form action="/{% if name %}{{ post_file_id(name) }}{% endif %}" method="post">
<form
action="{{ config.sanitized_base() }}/{% if name %}{{ post_file_id(name) }}{% endif %}"
method="post"
>
<textarea name="p">{{ "\n".join(file) }}</textarea>
<br />
<button type="submit">send</button>

View file

@ -20,7 +20,7 @@
<footer>
<p>
{% if file %}posted at {{ date }} // {% endif %}
<a href="/">back to index</a>
<a href="{{ config.base }}">back to index</a>
</p>
</footer>

View file

@ -1,6 +1,9 @@
{% if "timezone" not in session %}
<script type="text/javascript">
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
fetch("/set_timezone", { method: "POST", body: timezone });
fetch("{{ config.sanitized_base() }}/set_timezone", {
method: "POST",
body: timezone,
});
</script>
{% endif %}

View file

@ -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"""