remove demo pages, rename themes, add Content-Length header

This commit is contained in:
blankie 2026-06-16 05:11:37 +00:00
parent 0f614c88f8
commit 66a9fafd26
10 changed files with 1652 additions and 2370 deletions

View file

@ -1,9 +1,19 @@
from db import get_db, return_db, get_setting, set_setting, get_site_name
import templates as templates_mod
from templates import esc, DEFAULT_TEMPLATE
from ._helpers import _respond, _json_response, _csrf_field, _get_bookmark_token
from ._helpers import _respond, _redirect, _json_response, _csrf_field, _get_bookmark_token, _request_local
from .subscriptions import _count_shared_pages
_flash = {}
def _set_flash(msg):
_flash[_request_local.csrf_token] = msg
def _get_flash():
return _flash.pop(_request_local.csrf_token, "")
def handle_style_form(msg="", gateway_host="", scheme="http"):
template = get_setting("custom_template") or DEFAULT_TEMPLATE
@ -21,7 +31,6 @@ def handle_style_form(msg="", gateway_host="", scheme="http"):
reranker = get_setting("use_reranker", "0")
reranker_checked = " checked" if reranker == "1" else ""
disabled = "" if semantic == "1" else " disabled"
dimmed = ' style="opacity:0.4"' if semantic != "1" else ""
tcp_enabled = get_setting("tcp_enabled", "1")
tcp_checked = " checked" if tcp_enabled == "1" else ""
tcp_disabled = "" if tcp_enabled == "1" else " disabled"
@ -32,31 +41,65 @@ def handle_style_form(msg="", gateway_host="", scheme="http"):
lora_enabled = get_setting("lora_enabled", "0")
lora_checked = " checked" if lora_enabled == "1" else ""
lora_disabled = "" if lora_enabled == "1" else " disabled"
lora_dimmed = ' style="opacity:0.4"' if lora_enabled != "1" else ""
lora_port = get_setting("lora_port", "")
lora_frequency = get_setting("lora_frequency", "867200000")
lora_bandwidth = get_setting("lora_bandwidth", "125000")
lora_txpower = get_setting("lora_txpower", "7")
lora_sf = get_setting("lora_sf", "8")
lora_cr = get_setting("lora_cr", "5")
csrf = _csrf_field()
from handlers import forum_plugin as _fp
if _fp is not None:
forum_section = (
forum_body = (
f"<section id=\"forum\">"
f'<form method="post" action="/style">'
f"{csrf}"
f'<input type="hidden" name="_action" value="forum">'
f"<h2>forum</h2>"
f'<label><input type="checkbox" name="forum_enabled" value="1"{forum_checked}>'
f" enable forum (shared URL discussion board)</label><br>"
f"<small>Share URLs and discuss them with other TinyWeb instances. "
f"Requires <code>tinyweb-forum</code> — "
f'<a href="https://codeberg.org/tinyweb/tinyweb-forum">more info</a>.</small><br><br>'
f"<small>Share URLs and discuss them with other TinyWeb instances.</small><br><br>"
f'<input type="submit" value="save forum">'
f"</form>"
f"</section>"
)
forum_nav = ' · <a href="#forum">forum</a>'
else:
forum_section = ""
forum_body = ""
forum_nav = ""
msg = _get_flash() or msg
msg_html = ""
if msg:
msg_html = '<p><em>{msg}</em></p>'.format(msg=esc(msg))
return _respond(
f"<h1>customize</h1>"
f"<h2>name your search engine</h2>"
f'<form method="post" action="/style" id="settings">'
f'{_csrf_field()}'
f'<input name="site_name" value="{esc(name)}" placeholder="tinyweb" size="30"><br><br>'
f"{msg_html}"
f'<nav>'
f'<a href="#site-name">site name</a>'
f' · <a href="#sharing">sharing</a>'
f'{forum_nav}'
f' · <a href="#search">search</a>'
f' · <a href="#mesh">mesh</a>'
f' · <a href="#template">template</a>'
f' · <a href="#tools">tools</a>'
f'</nav>'
f"<hr>"
f"<section id=\"site-name\">"
f'<form method="post" action="/style">'
f"{csrf}"
f'<input type="hidden" name="_action" value="name">'
f"<h2>site name</h2>"
f'<input name="site_name" value="{esc(name)}" placeholder="tinyweb" size="30">'
f' <input type="submit" value="save name">'
f"</form>"
f"</section>"
f"<hr>"
f"<section id=\"sharing\">"
f'<form method="post" action="/style">'
f"{csrf}"
f'<input type="hidden" name="_action" value="sharing">'
f"<h2>sharing</h2>"
f'<label><input type="checkbox" name="sharing_enabled" value="1"{checked}>'
f" share your site list publicly at /api/sites</label><br>"
@ -66,39 +109,51 @@ def handle_style_form(msg="", gateway_host="", scheme="http"):
f' share all pages except those tagged <code>private</code></label><br>'
f'<label><input type="radio" name="sharing_mode" value="require_public"{require_checked}>'
f' share only pages tagged <code>public</code></label><br>'
f'<small>The <code>private</code> tag always excludes a page, even in public-only mode.</small>'
f'</div>'
f"</div>"
f'<p style="margin-top:0.6rem">'
f'Currently sharing <b>{shared_count}</b> page(s). '
f'<a href="/share/preview">preview what subscribers would see</a>'
f'</p>'
f'<a href="/share/preview">preview</a>'
f"</p>"
f'<input type="submit" value="save sharing">'
f"</form>"
f"</section>"
f"<hr>"
f"{forum_body}"
f"<hr>"
f"<section id=\"search\">"
f'<form method="post" action="/style">'
f"{csrf}"
f'<input type="hidden" name="_action" value="search">'
f"<h2>search</h2>"
f'<label><input type="checkbox" name="semantic_search" value="1"{semantic_checked}>'
f" semantic search</label><br><br>"
f'<label><input type="checkbox" name="use_reranker" value="1"{reranker_checked}{disabled}>'
f" cross-encoder reranking</label><br><br>"
f'<label><input type="checkbox" name="compress_embeddings" value="1"{compress_checked}{disabled}>'
f" compress embeddings</label><br><br>"
f'<a href="/reindex">manage semantic index</a>'
f'<br><input type="submit" value="save search">'
f"</form>"
f"</section>"
f"<hr>"
f"<section id=\"mesh\">"
f'<form method="post" action="/style">'
f"{csrf}"
f'<input type="hidden" name="_action" value="mesh">'
f"<h2>mesh network</h2>"
f"<p>Choose how to connect to the mesh. You can enable both for maximum reach.</p>"
f"<p>Choose how to connect to the mesh.</p>"
f"<h3>internet</h3>"
f'<label><input type="checkbox" name="tcp_enabled" value="1"{tcp_checked} '
f'onchange="var d=!this.checked;'
f'for(var e of document.querySelectorAll(\'#tcp-fields input\'))e.disabled=d;'
f'document.getElementById(\'tcp-fields\').style.opacity=d?\'0.4\':\'1\'">'
f'<label><input type="checkbox" name="tcp_enabled" value="1"{tcp_checked}>'
f" connect via internet transport node</label><br>"
f"<small>Reach peers anywhere online.</small><br>"
f'<div id="tcp-fields" style="margin-top:0.5rem{";opacity:0.4" if tcp_enabled != "1" else ""}">'
f"<small>Default: rnode.bre.land:4242</small><br>"
f'<input name="transport_host" value="{esc(transport_host)}" placeholder="hostname" size="30"{tcp_disabled}>'
f' <input name="transport_port" value="{esc(transport_port)}" placeholder="port" size="6"{tcp_disabled}><br>'
f'<p><a href="https://rmap.world/" target="_blank" rel="noreferrer noopener">discover more nodes</a></p>'
f'</div><br>'
f'<input name="transport_host" value="{esc(transport_host)}" size="30"{tcp_disabled}>'
f' <input name="transport_port" value="{esc(transport_port)}" size="6"{tcp_disabled}><br>'
f"<h3>LoRa</h3>"
f'<label><input type="checkbox" name="lora_enabled" value="1"{lora_checked} '
f'onchange="var d=!this.checked;document.getElementById(\'lora-port\').disabled=d;'
f'document.getElementById(\'lora-extras\').style.opacity=d?\'0.4\':\'1\';'
f'for(var e of document.querySelectorAll(\'#lora-extras input\'))e.disabled=d">'
f'<label><input type="checkbox" name="lora_enabled" value="1"{lora_checked}>'
f" connect via LoRa radio</label><br>"
f"<small>Reach nearby peers off-grid with an <a href=\"https://unsigned.io/rnode/\" target=\"_blank\" rel=\"noreferrer noopener\">RNode</a>.</small><br><br>"
f'<div id="lora-fields" style="{";opacity:0.4" if lora_enabled != "1" else ""}">'
f'<label>Serial port: <input id="lora-port" name="lora_port" value="{esc(lora_port)}" '
f'placeholder="/dev/ttyUSB0" size="20"{lora_disabled}></label><br><br>'
f'<details><summary>advanced radio settings</summary>'
f'<div id="lora-extras" style="margin-top:0.5rem">'
f'placeholder="/dev/ttyUSB0" size="20"{lora_disabled}></label><br>'
f"<details><summary>advanced radio settings</summary>"
f'<label>Frequency (Hz): <input name="lora_frequency" value="{esc(lora_frequency)}" size="12"{lora_disabled}></label><br>'
f"<small>ISM band frequency. Default: 867200000 (868 MHz EU). US: 915000000.</small><br><br>"
f'<label>Bandwidth (Hz): <input name="lora_bandwidth" value="{esc(lora_bandwidth)}" size="8"{lora_disabled}></label><br>'
@ -108,137 +163,123 @@ def handle_style_form(msg="", gateway_host="", scheme="http"):
f'<label>Spreading Factor: <input name="lora_sf" value="{esc(lora_sf)}" size="4"{lora_disabled}></label><br>'
f"<small>5-12. Higher = longer range, slower speed.</small><br><br>"
f'<label>Coding Rate: <input name="lora_cr" value="{esc(lora_cr)}" size="4"{lora_disabled}></label><br>'
f"<small>5-8. Higher = more error correction.</small><br>"
f'</div></details></div><br>'
f"<h2>search</h2>"
f"<h3>ai</h3>"
f'<label><input type="checkbox" name="semantic_search" value="1"{semantic_checked} '
f'onchange="var d=!this.checked;document.getElementById(\'reranker\').disabled=d;'
f'document.getElementById(\'ai-extras\').style.opacity=d?\'0.4\':\'1\'">'
f" semantic search (similarity matching)</label><br>"
f"<small>Requires onnxruntime, tokenizers, hnswlib. Downloads ~30MB of models on first use.</small><br><br>"
f'<div id="ai-extras"{dimmed}>'
f'<label><input type="checkbox" id="reranker" name="use_reranker" value="1"{reranker_checked}{disabled}>'
f" cross-encoder reranking (more accurate)</label><br>"
f"<small>Uses a 22MB model. Adds ~50ms per search. Disable for faster results.</small><br><br>"
f'<label><input type="checkbox" name="compress_embeddings" value="1"{compress_checked}{disabled}>'
f" compress embeddings (50% storage savings)</label><br>"
f"<small>Saves ~50% on storage for embeddings. Slight quality reduction at large scale.</small><br><br>"
f'<a href="/reindex">manage semantic index</a><br><br>'
f"</div>"
f"{forum_section}"
f'<p id="settings-status" style="font-size:0.85rem;color:#888;min-height:1.2em"></p>'
f'<noscript><button type="submit">save settings</button></noscript>'
f"<small>5-8. Higher = more error correction.</small>"
f"</details>"
f'<br><input type="submit" value="save mesh">'
f"</form>"
'<script>'
'(function(){'
"var f=document.getElementById('settings');"
"var csrf=f.querySelector('input[name=_csrf]').value;"
"var st=document.getElementById('settings-status');"
"f.querySelectorAll('input:not([type=hidden]):not([type=submit]),select').forEach(function(el){"
"el.addEventListener('change',function(){"
"var key=el.name,val=el.type=='checkbox'?(el.checked?'1':'0'):el.value;"
"st.textContent='saving...';"
"fetch('/style/field',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},"
"body:'_csrf='+encodeURIComponent(csrf)+'&key='+encodeURIComponent(key)+'&value='+encodeURIComponent(val)})"
".then(function(r){return r.json()})"
".then(function(d){st.textContent=d.status=='ok'?'saved \\u2713':d.message||'error';setTimeout(function(){st.textContent=''},3000)})"
".catch(function(){st.textContent='save failed';setTimeout(function(){st.textContent=''},3000)});"
"},false);});"
'})();'
'</script>'
f"<h2>custom html</h2>"
f"<p>Edit the full page template. Use <code>{esc('{{content}}')}</code> "
f"where page content should appear.</p>"
f"</section>"
f"<hr>"
f"<section id=\"template\">"
f"<h2>template</h2>"
f"<p>Edit the full page template.</p>"
f'<form method="post" action="/style/template">'
f'{_csrf_field()}'
f'<textarea name="template" rows="20" cols="60">{esc(template)}</textarea><br>'
f'<button type="submit">save template</button>'
f"{csrf}"
f'<textarea name="template" rows="12" cols="60" style="width:100%">{esc(template)}</textarea><br><br>'
f'<input type="submit" value="save template">'
f"</form>"
f"<h2>bookmarklet</h2>"
f"<p>Drag this link to your bookmarks bar. Click it on any page to index it instantly.</p>"
f"</section>"
f"<hr>"
f"<section id=\"tools\">"
f"<h2>tools</h2>"
f"<h3>bookmarklet</h3>"
f"<p>Drag this link to your bookmarks bar.</p>"
f'<p><a href="javascript:void(fetch(\'{esc(scheme)}://{esc(gateway_host or "localhost:8080")}/bookmark?url=\'+encodeURIComponent(location.href)+\'&token={_get_bookmark_token()}\').then(r=>r.text()).then(t=>alert(t)).catch(()=>alert(\'tinyweb not running\')))">+ save to {esc(name)}</a></p>'
f"<h2>reset</h2>"
f'<form method="post" action="/style/reset" '
f'onsubmit="return confirm(\'Reset the template to default? Your custom template will be lost.\')">'
f'{_csrf_field()}'
f'<button type="submit">reset template to default</button>'
f"<h3>reset template</h3>"
f'<form method="post" action="/style/reset">'
f"{csrf}"
f'<input type="submit" value="reset to default">'
f"</form>"
f"<h2>maintenance</h2>"
f"<h3>vacuum database</h3>"
f'<form method="post" action="/style/vacuum">'
f'{_csrf_field()}'
f'<button type="submit">vacuum database</button>'
f"{csrf}"
f'<input type="submit" value="vacuum">'
f"</form>"
f"<p>{msg}</p>"
f"</section>"
f'<a href="/">back</a>',
use_default=True,
)
def handle_style_submit(body, gateway_host="", scheme="http"):
template = body.get("template", [""])[0].replace("\r\n", "\n").replace("\r", "\n")
name = body.get("site_name", ["tinyweb"])[0].strip()
sharing = "1" if body.get("sharing_enabled") else "0"
sharing_mode = body.get("sharing_mode", ["exclude_private"])[0]
if sharing_mode not in ("exclude_private", "require_public"):
sharing_mode = "exclude_private"
set_setting("sharing_mode", sharing_mode)
semantic = "1" if body.get("semantic_search") else "0"
reranker = "1" if body.get("use_reranker") else "0"
compress = "1" if body.get("compress_embeddings") else "0"
tcp_enabled = "1" if body.get("tcp_enabled") else "0"
transport_host = body.get("transport_host", [""])[0].strip()
transport_port = body.get("transport_port", [""])[0].strip()
set_setting("custom_template", template if template.strip() != DEFAULT_TEMPLATE.strip() else "")
set_setting("site_name", name or "tinyweb")
set_setting("sharing_enabled", sharing)
set_setting("semantic_search", semantic)
set_setting("use_reranker", reranker)
set_setting("compress_embeddings", compress)
set_setting("tcp_enabled", tcp_enabled)
if transport_host:
set_setting("transport_host", transport_host)
if transport_port:
set_setting("transport_port", transport_port)
lora_enabled = "1" if body.get("lora_enabled") else "0"
set_setting("lora_enabled", lora_enabled)
set_setting("lora_port", body.get("lora_port", [""])[0].strip())
set_setting("lora_frequency", body.get("lora_frequency", ["867200000"])[0].strip())
set_setting("lora_bandwidth", body.get("lora_bandwidth", ["125000"])[0].strip())
set_setting("lora_txpower", body.get("lora_txpower", ["7"])[0].strip())
set_setting("lora_sf", body.get("lora_sf", ["8"])[0].strip())
set_setting("lora_cr", body.get("lora_cr", ["5"])[0].strip())
forum_enabled = "1" if body.get("forum_enabled") else "0"
current_forum = get_setting("forum_enabled", "0")
if forum_enabled != current_forum:
from handlers import forum_plugin
if forum_enabled == "1" and forum_plugin is None:
return handle_style_form(
"Forum plugin not installed. Run: pip install tinyweb-forum",
gateway_host=gateway_host, scheme=scheme,
)
if forum_enabled == "1":
forum_plugin.enable()
try:
forum_plugin.fdb.set_setting("forum_enabled", "1")
except Exception:
pass
else:
forum_plugin.disable()
try:
forum_plugin.fdb.set_setting("forum_enabled", "0")
except Exception:
pass
set_setting("forum_enabled", forum_enabled)
templates_mod.FORUM_ENABLED = (forum_enabled == "1")
return handle_style_form("Saved. Restart TinyWeb for mesh network changes to take effect.",
gateway_host=gateway_host, scheme=scheme)
action = body.get("_action", [""])[0]
if action == "name":
name = body.get("site_name", ["tinyweb"])[0].strip()
set_setting("site_name", name or "tinyweb")
_set_flash("Saved.")
return _redirect("/style")
if action == "sharing":
sharing = "1" if body.get("sharing_enabled") else "0"
sharing_mode = body.get("sharing_mode", ["exclude_private"])[0]
if sharing_mode not in ("exclude_private", "require_public"):
sharing_mode = "exclude_private"
set_setting("sharing_mode", sharing_mode)
set_setting("sharing_enabled", sharing)
_set_flash("Saved.")
return _redirect("/style")
if action == "forum":
forum_enabled = "1" if body.get("forum_enabled") else "0"
current_forum = get_setting("forum_enabled", "0")
if forum_enabled != current_forum:
from handlers import forum_plugin
if forum_enabled == "1" and forum_plugin is None:
_set_flash("Forum plugin not installed. Run: pip install tinyweb-forum")
return _redirect("/style")
if forum_enabled == "1":
forum_plugin.enable()
try:
forum_plugin.fdb.set_setting("forum_enabled", "1")
except Exception:
pass
else:
forum_plugin.disable()
try:
forum_plugin.fdb.set_setting("forum_enabled", "0")
except Exception:
pass
set_setting("forum_enabled", forum_enabled)
templates_mod.FORUM_ENABLED = (forum_enabled == "1")
_set_flash("Saved.")
return _redirect("/style")
if action == "search":
semantic = "1" if body.get("semantic_search") else "0"
reranker = "1" if body.get("use_reranker") else "0"
compress = "1" if body.get("compress_embeddings") else "0"
set_setting("semantic_search", semantic)
set_setting("use_reranker", reranker)
set_setting("compress_embeddings", compress)
_set_flash("Saved.")
return _redirect("/style")
if action == "mesh":
tcp_enabled = "1" if body.get("tcp_enabled") else "0"
transport_host = body.get("transport_host", [""])[0].strip()
transport_port = body.get("transport_port", [""])[0].strip()
set_setting("tcp_enabled", tcp_enabled)
if transport_host:
set_setting("transport_host", transport_host)
if transport_port:
set_setting("transport_port", transport_port)
lora_enabled = "1" if body.get("lora_enabled") else "0"
set_setting("lora_enabled", lora_enabled)
set_setting("lora_port", body.get("lora_port", [""])[0].strip())
set_setting("lora_frequency", body.get("lora_frequency", ["867200000"])[0].strip())
set_setting("lora_bandwidth", body.get("lora_bandwidth", ["125000"])[0].strip())
set_setting("lora_txpower", body.get("lora_txpower", ["7"])[0].strip())
set_setting("lora_sf", body.get("lora_sf", ["8"])[0].strip())
set_setting("lora_cr", body.get("lora_cr", ["5"])[0].strip())
_set_flash("Saved.")
return _redirect("/style")
def handle_style_template_submit(body, gateway_host="", scheme="http"):
template = body.get("template", [""])[0].replace("\r\n", "\n").replace("\r", "\n")
set_setting("custom_template", template if template.strip() != DEFAULT_TEMPLATE.strip() else "")
return handle_style_form("Template saved.", gateway_host=gateway_host, scheme=scheme)
_set_flash("Template saved.")
return _redirect("/style")
def handle_field_save(body):