printer on atproto
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

add api for printing via http

dawn e144c3cf 3457b34e

+78 -1
+22
Cargo.lock
··· 74 74 ] 75 75 76 76 [[package]] 77 + name = "async_tiny" 78 + version = "0.2.0" 79 + source = "registry+https://github.com/rust-lang/crates.io-index" 80 + checksum = "972ac50620540846a056f3e94384a25a0ff465c361c8e6a061d79be45a504ee6" 81 + dependencies = [ 82 + "bytes", 83 + "http", 84 + "http-body-util", 85 + "hyper", 86 + "hyper-util", 87 + "tokio", 88 + ] 89 + 90 + [[package]] 77 91 name = "atomic-waker" 78 92 version = "1.1.2" 79 93 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 83 97 name = "atspool" 84 98 version = "0.1.0" 85 99 dependencies = [ 100 + "async_tiny", 86 101 "chrono", 87 102 "escpos", 88 103 "futures-util", ··· 711 726 checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" 712 727 713 728 [[package]] 729 + name = "httpdate" 730 + version = "1.0.3" 731 + source = "registry+https://github.com/rust-lang/crates.io-index" 732 + checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" 733 + 734 + [[package]] 714 735 name = "hyper" 715 736 version = "1.9.0" 716 737 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 723 744 "http", 724 745 "http-body", 725 746 "httparse", 747 + "httpdate", 726 748 "itoa", 727 749 "pin-project-lite", 728 750 "smallvec",
+1
Cargo.toml
··· 16 16 chrono = "0.4" 17 17 reqwest = { version = "0.13", default-features = false, features = ["json", "rustls"] } 18 18 linemd = { version = "0.5.0", default-features = false } 19 + async_tiny = "0.2" 19 20 20 21 [profile.dev] 21 22 opt-level = 1
+55 -1
src/main.rs
··· 85 85 Default::default(), 86 86 )?; 87 87 88 + let password = std::env::var("SPOOL_PASSWORD").ok(); 89 + 88 90 let allowed_dids: Vec<String> = tokio::fs::read_to_string("allowed.txt") 89 91 .await 90 92 .into_diagnostic()? ··· 106 108 107 109 let (task_tx, task_rx) = mpsc::channel(16); 108 110 let job_handler = tokio::task::spawn_blocking(move || handle_jobs(printer, task_rx)); 109 - let job_stream = tokio::spawn(stream_jobs(jetstream_query, task_tx, resolver)); 111 + let job_stream = tokio::spawn(stream_jobs(jetstream_query, task_tx.clone(), resolver)); 112 + let api = tokio::spawn(serve_api(password, task_tx)); 110 113 111 114 tokio::select! { 112 115 r = job_stream => r.into_diagnostic().flatten(), 113 116 r = job_handler => r.into_diagnostic().flatten(), 117 + r = api => r.into_diagnostic().flatten(), 114 118 } 115 119 } 116 120 ··· 215 219 216 220 Ok(()) 217 221 } 222 + 223 + async fn serve_api(password: Option<String>, task_tx: TaskTx) -> Result<()> { 224 + use async_tiny::{Response, Server}; 225 + 226 + let mut server = Server::http("0.0.0.0:9889", true).await.into_diagnostic()?; 227 + 228 + while let Some(request) = server.next().await { 229 + let url = request.url().to_string(); 230 + let (path, query_str) = url.split_once('?').unwrap_or((&url, "")); 231 + 232 + let params: std::collections::HashMap<&str, &str> = query_str 233 + .split('&') 234 + .filter_map(|p| p.split_once('=')) 235 + .collect(); 236 + 237 + if let Some(ref pw) = password { 238 + if params.get("token").copied() != Some(pw.as_str()) { 239 + let _ = request.respond(Response::from_status_and_string(400, "400 Bad Request")); 240 + continue; 241 + } 242 + } 243 + 244 + let Some(&name) = params.get("name") else { 245 + let _ = request.respond(Response::from_status_and_string(400, "400 Bad Request")); 246 + continue; 247 + }; 248 + let name = name.to_string(); 249 + 250 + let response = match path { 251 + "/print" => { 252 + let text = String::from_utf8_lossy(request.body()).into_owned(); 253 + let _ = task_tx 254 + .send(Task { 255 + date: Utc::now(), 256 + identifier: name, 257 + job: Job { 258 + content: JobContent::Text(TextContent { text }), 259 + }, 260 + }) 261 + .await; 262 + Response::from_string("ok").with_content_type("application/json") 263 + } 264 + _ => Response::from_status_and_string(404, "404 Not Found"), 265 + }; 266 + 267 + let _ = request.respond(response); 268 + } 269 + 270 + Ok(()) 271 + }