From 808529861282036f9ed4d1f7fffc18a015aa70ec Mon Sep 17 00:00:00 2001 From: Noa Aarts Date: Sat, 13 Jul 2024 20:15:51 +0200 Subject: [PATCH] feat: it should be streaming now --- Cargo.lock | 21 ++++++++++++++++++++- Cargo.toml | 2 +- src/main.rs | 30 ++++++++++++++++++++++-------- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f910b5..c55929f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,7 @@ checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf" dependencies = [ "async-trait", "axum-core", + "axum-macros", "bytes 1.6.0", "futures-util", "http", @@ -169,6 +170,18 @@ dependencies = [ "tracing", ] +[[package]] +name = "axum-macros" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00c055ee2d014ae5981ce1016374e8213682aa14d9bf40e48ab48b5f3ef20eaa" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "backtrace" version = "0.3.73" @@ -552,6 +565,12 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "heck" version = "0.5.0" @@ -1379,7 +1398,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e535eb8dded36d55ec13eddacd30dec501792ff23a0b1682c38601b8cf2349" dependencies = [ "cfg-expr", - "heck", + "heck 0.5.0", "pkg-config", "toml", "version-compare", diff --git a/Cargo.toml b/Cargo.toml index d6cdf8e..a5bff8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -axum = { version = "0.7.5" } +axum = { version = "0.7.5", features = ["macros"] } bytes = "1.6.0" futures = "0.3.30" futures-util = "0.3.30" diff --git a/src/main.rs b/src/main.rs index 870260f..cf64e67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,15 @@ use std::{ time::Duration, }; -use futures::Stream; -use hyper::body::Frame; +use axum::{ + body::Body, + extract::State, + http::header, + response::{IntoResponse, Response}, + routing::get, +}; +use futures::{Stream, StreamExt}; +use http_body_util::StreamBody; use image::{GenericImageView, Rgb}; use tokio::{ io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, @@ -154,7 +161,7 @@ impl ImageStreamer { } impl Stream for ImageStreamer { - type Item = io::Result>; + type Item = io::Result>; fn poll_next( mut self: std::pin::Pin<&mut Self>, @@ -173,7 +180,7 @@ impl Stream for ImageStreamer { let r = self.v.read().unwrap(); let v: Vec = (*r.clone().into_inner()).to_vec(); - return Poll::Ready(Some(Ok(Frame::data(v.into())))); + return Poll::Ready(Some(Ok(v))); } } } @@ -371,14 +378,15 @@ where } } -async fn image_handler(State(state): State) { +#[axum::debug_handler] +async fn image_handler(State(state): State) -> impl IntoResponse { let cstrem = state.clone(); let header = [(header::CONTENT_TYPE, "image/jpeg")]; - let body = StreamBody::new(cstrem); + let body = Body::from_stream(cstrem); - (header, body); + (header, body).into_response() } #[tokio::main] @@ -405,7 +413,13 @@ async fn main() -> io::Result<()> { let web_listener = TcpListener::bind("0.0.0.0:7792").await?; println!("bound web listener"); - let _ = tokio::spawn(async move { web(web_listener, streamer) }); + let router = axum::Router::new() + .route("/image", get(image_handler)) + .with_state(streamer); + + let _ = tokio::spawn(async move { + return axum::serve(web_listener, router).await; + }); println!("web server started"); loop {