From 839653908a2f99987dd2e655d1163f6a8ea4a62e Mon Sep 17 00:00:00 2001 From: phil Date: Tue, 23 Sep 2025 12:48:01 -0400 Subject: [PATCH] nicer pages or whatever --- Cargo.lock | 4 ++-- src/bin/allegedly.rs | 4 ++-- src/mirror.rs | 56 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 39e8bd0..c219a3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -211,9 +211,9 @@ dependencies = [ [[package]] name = "brotli" -version = "8.0.1" +version = "8.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9991eea70ea4f293524138648e41ee89b0b2b12ddef3b255effa43c8056e0e0d" +checksum = "4bd8b9603c7aa97359dbd97ecf258968c95f3adddd6db2f7e7a5bef101c84560" dependencies = [ "alloc-no-stdlib", "alloc-stdlib", diff --git a/src/bin/allegedly.rs b/src/bin/allegedly.rs index 1b8dcd8..3f9f20e 100644 --- a/src/bin/allegedly.rs +++ b/src/bin/allegedly.rs @@ -234,9 +234,9 @@ async fn main() { let (tx, rx) = mpsc::channel(2); // upstream poller + let mut url = args.upstream.clone(); tokio::task::spawn(async move { log::info!("starting poll reader..."); - let mut url = args.upstream; url.set_path("/export"); tokio::task::spawn( async move { poll_upstream(Some(latest), url, tx).await.unwrap() }, @@ -249,7 +249,7 @@ async fn main() { pages_to_pg(poll_db, rx).await.unwrap(); }); - serve(wrap, bind).await.unwrap(); + serve(&args.upstream, wrap, bind).await.unwrap(); } Commands::Tail { after } => { let mut url = args.upstream; diff --git a/src/mirror.rs b/src/mirror.rs index ab2c104..0f2aba9 100644 --- a/src/mirror.rs +++ b/src/mirror.rs @@ -14,11 +14,49 @@ use std::time::Duration; struct State { client: Client, plc: Url, + upstream: Url, } #[handler] -fn hello() -> String { - logo("mirror") +fn hello(Data(State { upstream, .. }): Data<&State>) -> String { + format!( + r#"{} + +This is a PLC[1] mirror running Allegedly[2] in mirror mode. Allegedly synchronizes and proxies to a downstream PLC reference server instance[3] (why?[4]). + + +Configured upstream: + + {upstream} + + +Available APIs: + + - All PLC GET requests [5]. + - Rejects POSTs. This is a mirror. + + try `GET /{{did}}` to resolve an identity + + +[1] https://web.plc.directory +[2] https://tangled.org/@microcosm.blue/Allegedly +[3] https://github.com/did-method-plc/did-method-plc +[4] https://updates.microcosm.blue/3lz7nwvh4zc2u +[5] https://web.plc.directory/api/redoc + +"#, + logo("mirror") + ) +} + +fn failed_to_reach_wrapped() -> String { + format!( + r#"{} + +Failed to reach the wrapped reference PLC server. Sorry. +"#, + logo("mirror 502 :( ") + ) } #[handler] @@ -33,7 +71,7 @@ async fn proxy(req: &Request, Data(state): Data<&State>) -> Result Result { Ok(()) } -pub async fn serve(plc: Url, bind: SocketAddr) -> std::io::Result<()> { - let client = Client::builder() +pub async fn serve(upstream: &Url, plc: Url, bind: SocketAddr) -> std::io::Result<()> { + let wrapped_req_client = Client::builder() .timeout(Duration::from_secs(3)) .build() .unwrap(); - let state = State { client, plc }; + + let state = State { + client: wrapped_req_client, + plc, + upstream: upstream.clone(), + }; let app = Route::new() .at("/", get(hello)) @@ -66,5 +109,6 @@ pub async fn serve(plc: Url, bind: SocketAddr) -> std::io::Result<()> { .with(Compression::new()) .with(CatchPanic::new()) .with(Tracing); + Server::new(TcpListener::bind(bind)).run(app).await } -- 2.51.2