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,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