Docs: rewrite README with clear Docker and Docker Compose instructions; refactor app into package (auth, admin, pages, db); keep single entrypoint app.py
This commit is contained in:
76
app/pages.py
Normal file
76
app/pages.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from flask import Blueprint, redirect, url_for, abort, Response
|
||||
|
||||
from .db import get_db
|
||||
from .auth import is_logged_in
|
||||
|
||||
|
||||
pages_bp = Blueprint("pages", __name__)
|
||||
|
||||
|
||||
@pages_bp.route("/")
|
||||
def home():
|
||||
if not is_logged_in():
|
||||
return redirect(url_for("auth.login"))
|
||||
return redirect(url_for("admin.index"))
|
||||
|
||||
|
||||
@pages_bp.route("/p/<string:uid>")
|
||||
def view_page(uid: str):
|
||||
db = get_db()
|
||||
row = db.execute("SELECT html, title FROM pages WHERE uuid = ?", (uid,)).fetchone()
|
||||
if row is None:
|
||||
abort(404)
|
||||
|
||||
html: str = row["html"]
|
||||
page_title = row["title"] or ""
|
||||
if page_title:
|
||||
lower = html.lower()
|
||||
if "</head>" in lower:
|
||||
i = lower.rfind("</head>")
|
||||
html = html[:i] + f"<title>{page_title}</title>" + html[i:]
|
||||
elif "<html" in lower:
|
||||
if "<body" in lower:
|
||||
j = lower.find("<body")
|
||||
html = html[:j] + f"<head><meta charset=\"utf-8\"><title>{page_title}</title></head>" + html[j:]
|
||||
else:
|
||||
html = f"<head><meta charset=\"utf-8\"><title>{page_title}</title></head>" + html
|
||||
|
||||
# Admin button only for logged-in users
|
||||
if is_logged_in():
|
||||
admin_url = url_for("admin.index")
|
||||
toolbar = (
|
||||
'<div style="position:fixed;top:16px;right:16px;z-index:2147483647;">'
|
||||
f'<a href="{admin_url}" '
|
||||
'style="background:linear-gradient(135deg,#111,#333);color:#fff;padding:10px 14px;'
|
||||
'border-radius:10px;box-shadow:0 8px 20px rgba(0,0,0,0.25);'
|
||||
'text-decoration:none;font-weight:600;letter-spacing:.2px;'
|
||||
'font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif;"'
|
||||
'>Админка</a></div>'
|
||||
)
|
||||
lower = html.lower()
|
||||
if "</body>" in lower:
|
||||
idx = lower.rfind("</body>")
|
||||
html = html[:idx] + toolbar + html[idx:]
|
||||
else:
|
||||
html = html + toolbar
|
||||
|
||||
# Watermark for everyone (top-left)
|
||||
wm = (
|
||||
"<link rel=\"preconnect\" href=\"https://fonts.googleapis.com\">"
|
||||
"<link rel=\"preconnect\" href=\"https://fonts.gstatic.com\" crossorigin>"
|
||||
"<link href=\"https://fonts.googleapis.com/css2?family=Pacifico&display=swap\" rel=\"stylesheet\">"
|
||||
"<div style=\"position:fixed;top:16px;left:16px;z-index:2147483647;"
|
||||
"font-family:'Pacifico',cursive;letter-spacing:.2px;color:rgba(255,255,255,.92);"
|
||||
"text-shadow:0 2px 8px rgba(0,0,0,.35);pointer-events:none;user-select:none;\">"
|
||||
"Made by Ruslan"
|
||||
"</div>"
|
||||
)
|
||||
lower_all = html.lower()
|
||||
if "</body>" in lower_all:
|
||||
i2 = lower_all.rfind("</body>")
|
||||
html = html[:i2] + wm + html[i2:]
|
||||
else:
|
||||
html = html + wm
|
||||
|
||||
return Response(html, mimetype="text/html; charset=utf-8")
|
||||
|
||||
Reference in New Issue
Block a user