feat: remove rss, status, and badge features
This commit is contained in:
parent
553d7d1780
commit
36720e2ba5
21 changed files with 904 additions and 1200 deletions
|
|
@ -1,16 +0,0 @@
|
|||
[package]
|
||||
name = "noisebell-rss"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
axum = "0.8"
|
||||
noisebell-common = { path = "../noisebell-common" }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
time = { version = "0.3", features = ["formatting"] }
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "sync", "signal"] }
|
||||
tower-http = { version = "0.6", features = ["trace"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
# RSS Service
|
||||
|
||||
Serves an Atom feed of door status history. Stateless — it fetches from the cache service's `/history` endpoint on each request and renders the last 7 days as Atom XML.
|
||||
|
||||
## API
|
||||
|
||||
| Method | Path | Description |
|
||||
|--------|------|-------------|
|
||||
| `GET` | `/feed` | Atom feed |
|
||||
| `GET` | `/health` | Health check |
|
||||
|
||||
## Configuration
|
||||
|
||||
NixOS options under `services.noisebell-rss`:
|
||||
|
||||
| Option | Default | Description |
|
||||
|--------|---------|-------------|
|
||||
| `domain` | required | Caddy virtual host domain |
|
||||
| `cacheUrl` | required | Cache service URL (e.g. `http://localhost:3000`) |
|
||||
| `port` | `3002` | Listen port |
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
pkg:
|
||||
{ config, lib, ... }:
|
||||
|
||||
let
|
||||
cfg = config.services.noisebell-rss;
|
||||
bin = "${pkg}/bin/noisebell-rss";
|
||||
in
|
||||
{
|
||||
options.services.noisebell-rss = {
|
||||
enable = lib.mkEnableOption "noisebell RSS/Atom feed";
|
||||
|
||||
domain = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Domain for the Caddy virtual host.";
|
||||
};
|
||||
|
||||
port = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
default = 3002;
|
||||
};
|
||||
|
||||
cacheUrl = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "URL of the cache service (e.g. http://localhost:3000).";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
users.users.noisebell-rss = {
|
||||
isSystemUser = true;
|
||||
group = "noisebell-rss";
|
||||
};
|
||||
users.groups.noisebell-rss = {};
|
||||
|
||||
services.caddy.virtualHosts.${cfg.domain}.extraConfig = ''
|
||||
reverse_proxy localhost:${toString cfg.port}
|
||||
'';
|
||||
|
||||
systemd.services.noisebell-rss = {
|
||||
description = "Noisebell RSS/Atom feed";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network-online.target" "noisebell-cache.service" ];
|
||||
wants = [ "network-online.target" ];
|
||||
environment = {
|
||||
NOISEBELL_RSS_PORT = toString cfg.port;
|
||||
NOISEBELL_RSS_CACHE_URL = cfg.cacheUrl;
|
||||
NOISEBELL_RSS_SITE_URL = "https://${cfg.domain}";
|
||||
RUST_LOG = "info";
|
||||
};
|
||||
script = ''
|
||||
exec ${bin}
|
||||
'';
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
Restart = "on-failure";
|
||||
RestartSec = 5;
|
||||
User = "noisebell-rss";
|
||||
Group = "noisebell-rss";
|
||||
NoNewPrivileges = true;
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
PrivateTmp = true;
|
||||
ProtectKernelTunables = true;
|
||||
ProtectKernelModules = true;
|
||||
ProtectControlGroups = true;
|
||||
RestrictSUIDSGID = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
@ -1,187 +0,0 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use axum::extract::State;
|
||||
use axum::http::{StatusCode, header};
|
||||
use axum::response::IntoResponse;
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
use noisebell_common::HistoryEntry;
|
||||
use tower_http::trace::TraceLayer;
|
||||
use tracing::{error, info, Level};
|
||||
|
||||
struct AppState {
|
||||
client: reqwest::Client,
|
||||
cache_url: String,
|
||||
site_url: String,
|
||||
}
|
||||
|
||||
fn unix_to_rfc3339(ts: u64) -> String {
|
||||
let dt = time::OffsetDateTime::from_unix_timestamp(ts as i64).unwrap_or(time::OffsetDateTime::UNIX_EPOCH);
|
||||
dt.format(&time::format_description::well_known::Rfc3339).unwrap_or_else(|_| "1970-01-01T00:00:00Z".to_string())
|
||||
}
|
||||
|
||||
fn escape_xml(s: &str) -> String {
|
||||
s.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
.replace('"', """)
|
||||
.replace('\'', "'")
|
||||
}
|
||||
|
||||
fn status_description(status: &str) -> &str {
|
||||
match status {
|
||||
"open" => "The door at Noisebridge is open.",
|
||||
"closed" => "The door at Noisebridge is closed.",
|
||||
"offline" => "The Noisebridge Pi is offline.",
|
||||
_ => "Unknown status.",
|
||||
}
|
||||
}
|
||||
|
||||
fn status_title(status: &str) -> &str {
|
||||
match status {
|
||||
"open" => "Door is open",
|
||||
"closed" => "Door is closed",
|
||||
"offline" => "Pi is offline",
|
||||
_ => "Unknown",
|
||||
}
|
||||
}
|
||||
|
||||
fn unix_now() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
async fn get_feed(
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> impl IntoResponse {
|
||||
let url = format!("{}/history", state.cache_url);
|
||||
|
||||
let resp = match state.client.get(&url).send().await {
|
||||
Ok(resp) if resp.status().is_success() => resp,
|
||||
Ok(resp) => {
|
||||
error!(status = %resp.status(), "cache service returned error");
|
||||
return (StatusCode::BAD_GATEWAY, "upstream error").into_response();
|
||||
}
|
||||
Err(e) => {
|
||||
error!(error = %e, "failed to reach cache service");
|
||||
return (StatusCode::BAD_GATEWAY, "upstream unavailable").into_response();
|
||||
}
|
||||
};
|
||||
|
||||
let entries: Vec<HistoryEntry> = match resp.json().await {
|
||||
Ok(entries) => entries,
|
||||
Err(e) => {
|
||||
error!(error = %e, "failed to parse cache response");
|
||||
return (StatusCode::BAD_GATEWAY, "invalid upstream response").into_response();
|
||||
}
|
||||
};
|
||||
|
||||
let updated = entries
|
||||
.first()
|
||||
.map(|e| unix_to_rfc3339(e.timestamp))
|
||||
.unwrap_or_else(|| unix_to_rfc3339(unix_now()));
|
||||
|
||||
let site_url = escape_xml(&state.site_url);
|
||||
let mut xml = format!(
|
||||
r#"<?xml version="1.0" encoding="utf-8"?>
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<title>Noisebell Door Status</title>
|
||||
<link href="{site_url}/feed" rel="self"/>
|
||||
<link href="{site_url}" rel="alternate"/>
|
||||
<id>urn:noisebell:door-status</id>
|
||||
<updated>{updated}</updated>
|
||||
"#,
|
||||
);
|
||||
|
||||
let seven_days_ago = unix_now().saturating_sub(7 * 24 * 60 * 60);
|
||||
for entry in &entries {
|
||||
if entry.timestamp < seven_days_ago {
|
||||
continue;
|
||||
}
|
||||
let ts_rfc = unix_to_rfc3339(entry.timestamp);
|
||||
xml.push_str(&format!(
|
||||
r#" <entry>
|
||||
<title>{title}</title>
|
||||
<id>urn:noisebell:event:{id}</id>
|
||||
<updated>{ts}</updated>
|
||||
<content type="text">{description}</content>
|
||||
</entry>
|
||||
"#,
|
||||
title = escape_xml(status_title(&entry.status)),
|
||||
id = entry.id,
|
||||
ts = ts_rfc,
|
||||
description = escape_xml(status_description(&entry.status)),
|
||||
));
|
||||
}
|
||||
|
||||
xml.push_str("</feed>\n");
|
||||
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(header::CONTENT_TYPE, "application/atom+xml; charset=utf-8")],
|
||||
xml,
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
let port: u16 = std::env::var("NOISEBELL_RSS_PORT")
|
||||
.unwrap_or_else(|_| "3002".into())
|
||||
.parse()
|
||||
.context("NOISEBELL_RSS_PORT must be a valid u16")?;
|
||||
|
||||
let cache_url = std::env::var("NOISEBELL_RSS_CACHE_URL")
|
||||
.context("NOISEBELL_RSS_CACHE_URL is required")?;
|
||||
|
||||
let site_url = std::env::var("NOISEBELL_RSS_SITE_URL")
|
||||
.unwrap_or_else(|_| "https://rss.noisebell.extremist.software".to_string());
|
||||
|
||||
info!(port, %cache_url, "starting noisebell-rss");
|
||||
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(10))
|
||||
.build()
|
||||
.context("failed to build HTTP client")?;
|
||||
|
||||
let app_state = Arc::new(AppState {
|
||||
client,
|
||||
cache_url,
|
||||
site_url,
|
||||
});
|
||||
|
||||
let app = Router::new()
|
||||
.route("/health", get(|| async { StatusCode::OK }))
|
||||
.route("/feed", get(get_feed))
|
||||
.layer(
|
||||
TraceLayer::new_for_http()
|
||||
.make_span_with(tower_http::trace::DefaultMakeSpan::new().level(Level::INFO))
|
||||
.on_response(tower_http::trace::DefaultOnResponse::new().level(Level::INFO)),
|
||||
)
|
||||
.with_state(app_state);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(("0.0.0.0", port))
|
||||
.await
|
||||
.context(format!("failed to bind to 0.0.0.0:{port}"))?;
|
||||
|
||||
info!(port, "listening");
|
||||
|
||||
let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
||||
.context("failed to register SIGTERM handler")?;
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(async move {
|
||||
sigterm.recv().await;
|
||||
})
|
||||
.await
|
||||
.context("server error")?;
|
||||
|
||||
info!("shutdown complete");
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue