Add subpath support (#7)
This commit is contained in:
parent
4b96b73c42
commit
5ef2ced310
12 changed files with 36 additions and 18 deletions
13
README.md
13
README.md
|
@ -6,12 +6,13 @@ personal diary with txt style, can be private!
|
||||||
|
|
||||||
## environment variables
|
## environment variables
|
||||||
|
|
||||||
| name | description | required |
|
| name | description | required |
|
||||||
| :----------------: | :-------------------: | :-------------: |
|
| :----------------: | :---------------------------------: | :-------------: |
|
||||||
| `TD_USERNAME` | user name | no |
|
| `TD_USERNAME` | user name | no |
|
||||||
| `TD_USERPASS` | user password | yes |
|
| `TD_USERPASS` | user password | yes |
|
||||||
| `TD_PRIVATE` | post visibility | empty = `false` |
|
| `TD_PRIVATE` | post visibility | empty = `false` |
|
||||||
| `TD_LOGINLIFETIME` | login lifetime (days) | empty = `7` |
|
| `TD_LOGINLIFETIME` | login lifetime (days) | empty = `7` |
|
||||||
|
| `TD_BASEROUTE` | base route, must start with a slash | no |
|
||||||
|
|
||||||
## volumes (docker)
|
## volumes (docker)
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ app = Flask(__name__, static_url_path="/")
|
||||||
for file in glob("*/routes/*.py"):
|
for file in glob("*/routes/*.py"):
|
||||||
module = file.replace("/", ".").split(".")[-2]
|
module = file.replace("/", ".").split(".")[-2]
|
||||||
exec(f"from routes.{module} import router as {module}")
|
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)
|
app.secret_key = urandom(12)
|
||||||
|
|
||||||
|
|
|
@ -38,11 +38,11 @@ def new_post(post_id: int | None) -> Response:
|
||||||
update = create_post(filename, content)
|
update = create_post(filename, content)
|
||||||
|
|
||||||
flash(
|
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:
|
else:
|
||||||
flash(f"invalid post: {content}")
|
flash(f"invalid post: {content}")
|
||||||
else:
|
else:
|
||||||
flash("you can't do that.")
|
flash("you can't do that.")
|
||||||
|
|
||||||
return redirect("/")
|
return redirect(Config.base)
|
||||||
|
|
|
@ -30,4 +30,4 @@ def check() -> Response:
|
||||||
else:
|
else:
|
||||||
flash("already logged.")
|
flash("already logged.")
|
||||||
|
|
||||||
return redirect("/")
|
return redirect(Config.base)
|
||||||
|
|
|
@ -37,4 +37,4 @@ def remove_post(file: int) -> Response:
|
||||||
else:
|
else:
|
||||||
flash(f"{filename} doesn't exists")
|
flash(f"{filename} doesn't exists")
|
||||||
|
|
||||||
return redirect("/")
|
return redirect(Config.base)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<h1>diary{{ " of " + config.user.name if config.user.name else "" }}</h1>
|
<h1>diary{{ " of " + config.user.name if config.user.name else "" }}</h1>
|
||||||
|
|
||||||
{% if not (login_page or config.is_logged()) %}
|
{% 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">
|
<svg width="30px" fill="none" viewBox="0 0 24 24">
|
||||||
<path
|
<path
|
||||||
stroke="#000"
|
stroke="#000"
|
||||||
|
|
|
@ -18,7 +18,9 @@
|
||||||
{% else %}
|
{% else %}
|
||||||
<ul>
|
<ul>
|
||||||
{% for p in posts: %}
|
{% 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 %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -10,14 +10,14 @@
|
||||||
<h3>login page</h3>
|
<h3>login page</h3>
|
||||||
|
|
||||||
<span id="login-form">
|
<span id="login-form">
|
||||||
<form action="/login" method="post">
|
<form action="{{ config.sanitized_base() }}/login" method="post">
|
||||||
<input
|
<input
|
||||||
type="password"
|
type="password"
|
||||||
name="password"
|
name="password"
|
||||||
placeholder="password"
|
placeholder="password"
|
||||||
autofocus
|
autofocus
|
||||||
/>
|
/>
|
||||||
<a href="/">Cancel</a>
|
<a href="{{ config.base }}">Cancel</a>
|
||||||
<button type="submit">Login</button>
|
<button type="submit">Login</button>
|
||||||
</form></span
|
</form></span
|
||||||
>
|
>
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
{% if config.is_logged() %}
|
{% 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>
|
<textarea name="p">{{ "\n".join(file) }}</textarea>
|
||||||
<br />
|
<br />
|
||||||
<button type="submit">send</button>
|
<button type="submit">send</button>
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
<footer>
|
<footer>
|
||||||
<p>
|
<p>
|
||||||
{% if file %}posted at {{ date }} // {% endif %}
|
{% if file %}posted at {{ date }} // {% endif %}
|
||||||
<a href="/">back to index</a>
|
<a href="{{ config.base }}">back to index</a>
|
||||||
</p>
|
</p>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
{% if "timezone" not in session %}
|
{% if "timezone" not in session %}
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
fetch("/set_timezone", { method: "POST", body: timezone });
|
fetch("{{ config.sanitized_base() }}/set_timezone", {
|
||||||
|
method: "POST",
|
||||||
|
body: timezone,
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -6,6 +6,7 @@ VAR_USERNAME = "TD_USERNAME"
|
||||||
VAR_USERPASS = "TD_USERPASS"
|
VAR_USERPASS = "TD_USERPASS"
|
||||||
VAR_PRIVATE = "TD_PRIVATE"
|
VAR_PRIVATE = "TD_PRIVATE"
|
||||||
VAR_LOGLIFETIME = "TD_LOGINLIFETIME"
|
VAR_LOGLIFETIME = "TD_LOGINLIFETIME"
|
||||||
|
VAR_BASE = "TD_BASEROUTE"
|
||||||
|
|
||||||
|
|
||||||
class User:
|
class User:
|
||||||
|
@ -47,6 +48,14 @@ class Config:
|
||||||
float(envar[VAR_LOGLIFETIME]) if VAR_LOGLIFETIME in envar else 7.0
|
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
|
@staticmethod
|
||||||
def is_logged() -> bool:
|
def is_logged() -> bool:
|
||||||
"""where the info about connection is stored"""
|
"""where the info about connection is stored"""
|
||||||
|
|
Loading…
Reference in a new issue