Files
kindexr/src/ui/mod.rs
T
enki 6cb9428567 add Deluge and qBittorrent sections to Settings page
Both torrent clients now configurable from /ui/settings with enabled
toggle, URL, password, poll interval, and labels/categories fields.
Settings persist to DB and apply on next restart.
2026-05-19 21:24:41 -07:00

323 lines
15 KiB
Rust

use axum::{
body::Bytes,
extract::{Path, State},
http::{header, StatusCode},
response::{IntoResponse, Response},
routing::{get, post},
Router,
};
use crate::{db, AppState};
mod dashboard;
mod indexed;
mod publishers;
mod published;
mod settings;
pub fn router() -> Router<AppState> {
Router::new()
.route("/ui", get(dashboard::handler))
.route("/ui/indexed", get(indexed::handler))
.route("/ui/publishers", get(publishers::list_handler))
.route("/ui/publishers/vouch", post(publishers::vouch_handler))
.route("/ui/publishers/{pubkey}/block", post(publishers::block_handler))
.route("/ui/publishers/{pubkey}/unblock", post(publishers::unblock_handler))
.route("/ui/publishers/{pubkey}/mute", post(publishers::mute_handler))
.route("/ui/published", get(published::handler))
.route("/ui/settings", get(settings::handler))
.route("/ui/settings/network", post(settings::save_network))
.route("/ui/settings/relays/add", post(settings::add_relay))
.route("/ui/settings/relays/remove", post(settings::remove_relay))
.route("/ui/settings/curation", post(settings::save_curation))
.route("/ui/settings/ingest", post(settings::save_ingest))
.route("/ui/settings/tmdb", post(settings::save_tmdb))
.route("/ui/settings/apikeys/create", post(settings::create_api_key))
.route("/ui/settings/apikeys/delete", post(settings::delete_api_key))
.route("/ui/settings/deluge", post(settings::save_deluge))
.route("/ui/settings/qbittorrent", post(settings::save_qbittorrent))
.route("/torrent/{info_hash}", get(torrent_blob_handler))
}
/// Serve a cached .torrent file by info_hash.
async fn torrent_blob_handler(
State(state): State<AppState>,
Path(info_hash): Path<String>,
) -> Response {
match db::get_torrent_blob(&state.pool, &info_hash).await {
Ok(Some(blob)) => (
StatusCode::OK,
[
(header::CONTENT_TYPE, "application/x-bittorrent"),
(
header::CONTENT_DISPOSITION,
&format!("attachment; filename=\"{info_hash}.torrent\""),
),
],
Bytes::from(blob),
)
.into_response(),
Ok(None) => (StatusCode::NOT_FOUND, "torrent file not cached").into_response(),
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, "db error").into_response(),
}
}
// ─── Shared helpers ───────────────────────────────────────────────────────────
pub fn page(active: &str, title: &str, body: &str) -> axum::response::Html<String> {
let nav = [
("dashboard", "/ui", "Dashboard"),
("indexed", "/ui/indexed", "Indexed"),
("publishers", "/ui/publishers", "Publishers"),
("published", "/ui/published", "Published"),
("settings", "/ui/settings", "Settings"),
]
.iter()
.map(|(id, href, label)| {
if *id == active {
format!(r#"<a href="{href}" class="active">{label}</a>"#)
} else {
format!(r#"<a href="{href}">{label}</a>"#)
}
})
.collect::<Vec<_>>()
.join("\n ");
axum::response::Html(format!(
r#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>{title} — kindexr</title>
<style>{css}</style>
</head>
<body>
<nav>
<span class="brand">kindexr</span>
{nav}
</nav>
<main>
{body}
</main>
</body>
</html>"#,
css = CSS,
))
}
pub fn err_page(e: &anyhow::Error) -> axum::response::Html<String> {
page("", "Error", &format!("<div class='err-box'><strong>Error:</strong> {e}</div>"))
}
pub fn esc(s: &str) -> String {
s.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
.replace('"', "&quot;")
}
pub fn fmt_ts(ts: i64) -> String {
use std::time::{Duration, UNIX_EPOCH};
let d = UNIX_EPOCH + Duration::from_secs(ts.max(0) as u64);
// Simple formatting without chrono dependency in this module
// Use chrono which is already in Cargo.toml
let dt = chrono::DateTime::<chrono::Utc>::from(d);
dt.format("%Y-%m-%d %H:%M").to_string()
}
pub fn fmt_ts_ago(ts: i64) -> String {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64;
let diff = (now - ts).max(0);
if diff < 60 {
format!("{diff}s ago")
} else if diff < 3600 {
format!("{}m ago", diff / 60)
} else if diff < 86400 {
format!("{}h ago", diff / 3600)
} else {
format!("{}d ago", diff / 86400)
}
}
pub fn fmt_size(bytes: i64) -> String {
const GIB: i64 = 1 << 30;
const MIB: i64 = 1 << 20;
const KIB: i64 = 1 << 10;
if bytes >= GIB {
format!("{:.1} GiB", bytes as f64 / GIB as f64)
} else if bytes >= MIB {
format!("{:.0} MiB", bytes as f64 / MIB as f64)
} else if bytes >= KIB {
format!("{:.0} KiB", bytes as f64 / KIB as f64)
} else {
format!("{bytes} B")
}
}
pub fn short_pubkey(pk: &str) -> String {
if pk.len() >= 12 {
format!("{}{}", &pk[..6], &pk[pk.len() - 6..])
} else {
pk.to_owned()
}
}
pub fn pagination(base: &str, page: i64, total_pages: i64) -> String {
if total_pages <= 1 {
return String::new();
}
let mut parts = vec![r#"<div class="pager">"#.to_string()];
if page > 1 {
parts.push(format!(r#"<a href="{base}&page={}">&larr;</a>"#, page - 1));
}
let start = (page - 2).max(1);
let end = (page + 2).min(total_pages);
for p in start..=end {
if p == page {
parts.push(format!(r#"<a class="cur" href="{base}&page={p}">{p}</a>"#));
} else {
parts.push(format!(r#"<a href="{base}&page={p}">{p}</a>"#));
}
}
if page < total_pages {
parts.push(format!(r#"<a href="{base}&page={}">&rarr;</a>"#, page + 1));
}
parts.push(format!(
r#"<span>{page} / {total_pages}</span>"#
));
parts.push("</div>".to_string());
parts.join("\n")
}
/// Try to normalise a pubkey string (npub bech32 or 64-char hex) to lowercase hex.
pub fn parse_pubkey(s: &str) -> Option<String> {
let s = s.trim();
if s.len() == 64 && s.chars().all(|c| c.is_ascii_hexdigit()) {
return Some(s.to_lowercase());
}
// try as npub bech32
use nostr::FromBech32;
nostr::PublicKey::from_bech32(s)
.ok()
.map(|pk| pk.to_hex())
}
const CSS: &str = r#"
:root {
--bg: #0f1117; --bg2: #161b27; --bg3: #1e2438;
--border: #2a3050; --text: #dde1f0; --muted: #6b7599;
--accent: #818cf8; --success: #34d399; --danger: #f87171; --warn: #fbbf24;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.6 system-ui, sans-serif; min-height: 100vh; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
nav { display: flex; align-items: center; gap: 1.5rem; padding: .6rem 1.5rem;
background: var(--bg2); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.brand { font-weight: 700; color: var(--accent); margin-right: auto; letter-spacing: -.02em; }
nav a { color: var(--muted); font-size: 13px; }
nav a:hover, nav a.active { color: var(--text); text-decoration: none; }
nav a.active { font-weight: 600; }
main { padding: 1.5rem; max-width: 1200px; margin: 0 auto; }
h1 { font-size: 1.1rem; font-weight: 600; margin-bottom: 1.25rem; }
.stats { display: flex; gap: .75rem; flex-wrap: wrap; margin-bottom: 1.75rem; }
.stat { background: var(--bg2); border: 1px solid var(--border); border-radius: 8px;
padding: .75rem 1.25rem; min-width: 140px; }
.stat .val { font-size: 1.75rem; font-weight: 700; color: var(--accent); line-height: 1; margin-bottom: .15rem; }
.stat .lbl { font-size: .68rem; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }
.section { margin-bottom: 2rem; }
.section-title { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em;
color: var(--muted); margin-bottom: .75rem; font-weight: 600; }
.table-wrap { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
th { background: var(--bg2); padding: .45rem .75rem; text-align: left; font-size: .68rem;
text-transform: uppercase; letter-spacing: .07em; color: var(--muted);
border-bottom: 1px solid var(--border); font-weight: 600; }
td { padding: .45rem .75rem; border-bottom: 1px solid var(--border); font-size: 13px; vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg3); }
.mono { font-family: 'JetBrains Mono', 'Fira Code', ui-monospace, monospace; font-size: 11px; color: var(--muted); }
.badge { display: inline-flex; align-items: center; padding: .12rem .45rem; border-radius: 4px;
font-size: 11px; font-weight: 600; }
.badge-ok { background: #052e1c; color: var(--success); }
.badge-err { background: #2d0c0c; color: var(--danger); }
.badge-warn { background: #2a1a02; color: var(--warn); }
.badge-dim { background: var(--bg3); color: var(--muted); }
.badge-accent { background: #1e2060; color: var(--accent); }
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 5px; }
.dot-ok { background: var(--success); }
.dot-dim { background: var(--muted); }
.dot-err { background: var(--danger); }
button, .btn { padding: .28rem .6rem; border-radius: 5px; border: 1px solid var(--border);
cursor: pointer; font-size: 12px; font-weight: 600; background: var(--bg3); color: var(--text); }
.btn-danger { background: #2d0c0c; color: var(--danger); border-color: #5a1a1a; }
.btn-ok { background: #052e1c; color: var(--success); border-color: #0a4a2b; }
.btn-warn { background: #2a1a02; color: var(--warn); border-color: #5a3600; }
.btn-accent { background: var(--accent); color: #0f1117; border-color: var(--accent); }
.card { background: var(--bg2); border: 1px solid var(--border); border-radius: 8px;
padding: 1rem 1.25rem; margin-bottom: 1.5rem; }
.card-title { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em;
color: var(--muted); margin-bottom: .75rem; font-weight: 600; }
.form-row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
input[type=text], input[type=number], select {
padding: .38rem .7rem; background: var(--bg); border: 1px solid var(--border);
border-radius: 5px; color: var(--text); font-size: 13px; outline: none; }
input[type=text]:focus, select:focus { border-color: var(--accent); }
input.mono-in { font-family: monospace; }
input.wide { flex: 1; min-width: 260px; }
.search-row { display: flex; gap: .6rem; margin-bottom: 1rem; }
.search-row input { flex: 1; max-width: 360px; }
.search-row button { padding: .38rem .9rem; background: var(--accent); color: #0f1117;
border: none; border-radius: 5px; font-weight: 600; cursor: pointer; }
.pager { display: flex; gap: .4rem; margin-top: 1rem; align-items: center; }
.pager a { padding: .22rem .55rem; border-radius: 4px; background: var(--bg2);
border: 1px solid var(--border); color: var(--muted); font-size: 12px; }
.pager a:hover, .pager a.cur { background: var(--accent); color: #0f1117;
border-color: var(--accent); text-decoration: none; }
.pager span { color: var(--muted); font-size: 12px; }
.empty { color: var(--muted); font-style: italic; padding: 1.5rem .75rem; text-align: center; }
.err-box { background: #2d0c0c; border: 1px solid #5a1a1a; color: var(--danger);
border-radius: 8px; padding: 1rem; margin-bottom: 1rem; }
.msg-ok { background: #052e1c; border: 1px solid #0a4a2b; color: var(--success);
border-radius: 8px; padding: .6rem 1rem; margin-bottom: 1rem; font-size: 13px; }
.msg-err { background: #2d0c0c; border: 1px solid #5a1a1a; color: var(--danger);
border-radius: 8px; padding: .6rem 1rem; margin-bottom: 1rem; font-size: 13px; }
form.ifrm { display: inline; }
.pub-identity { display: flex; align-items: center; gap: .5rem; }
.avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; flex-shrink: 0; }
.avatar-placeholder { width: 28px; height: 28px; border-radius: 50%; background: var(--bg3);
border: 1px solid var(--border); display: inline-flex; align-items: center;
justify-content: center; font-size: 11px; font-weight: 700;
color: var(--muted); flex-shrink: 0; }
.pub-name { font-size: 13px; font-weight: 500; }
.pub-nip05 { font-size: 11px; color: var(--muted); }
.tabs { display: flex; gap: 0; margin-bottom: 1rem; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; width: fit-content; }
.tabs a { padding: .35rem .9rem; font-size: 12px; font-weight: 600; color: var(--muted); background: var(--bg2); }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.cur { background: var(--accent); color: #0f1117; }
.relay-list { display: flex; flex-direction: column; gap: .4rem; }
.relay-row { display: flex; align-items: center; justify-content: space-between;
background: var(--bg3); border-radius: 5px; padding: .4rem .75rem; font-size: 13px; }
.relay-url { font-family: monospace; font-size: 12px; color: var(--text); }
.relay-ts { font-size: 11px; color: var(--muted); }
.banner-restart { background: #2a1a02; border: 1px solid #5a3600; color: var(--warn);
border-radius: 8px; padding: .7rem 1rem; margin-bottom: 1.25rem; font-size: 13px; }
.radio-group { display: flex; flex-direction: column; gap: .45rem; }
.radio-label { display: flex; align-items: center; gap: .5rem; font-size: 13px; cursor: pointer; }
.radio-label input[type=radio] { accent-color: var(--accent); width: 15px; height: 15px; }
.toggle-label { display: flex; align-items: center; gap: .5rem; font-size: 13px; cursor: pointer; }
.toggle-label input[type=checkbox] { accent-color: var(--accent); width: 15px; height: 15px; }
.setting-row { display: flex; flex-direction: column; gap: .25rem; margin-bottom: .9rem; }
.setting-label { font-size: 12px; font-weight: 600; color: var(--text); }
.setting-hint { display: block; font-size: 11px; font-weight: 400; color: var(--muted); margin-top: .1rem; }
.new-key-box { background: #052e1c; border: 1px solid #0a4a2b; border-radius: 8px;
padding: .9rem 1.1rem; margin-bottom: 1.25rem; }
.new-key-title { font-weight: 600; color: var(--success); margin-bottom: .2rem; }
.new-key-hint { font-size: 11px; color: var(--muted); margin-bottom: .6rem; }
.new-key-row { display: flex; gap: .5rem; align-items: center; }
.new-key-input { flex: 1; font-size: 12px; background: var(--bg); border-color: #0a4a2b; }
"#;