webserver_rs/web_core/
assets.rs

1use std::{collections::VecDeque, task::Poll};
2
3use futures::Stream;
4use salvo::{http::HeaderMap, prelude::*};
5
6use crate::HttpResult;
7
8#[allow(dead_code)]
9pub(crate) fn common_assets(pub_dir: String, listing: bool) -> Router {
10    Router::with_path("public/<**path>").get(
11        StaticDir::new([pub_dir])
12            .defaults("index.html")
13            .auto_list(listing),
14    )
15}
16
17#[handler]
18pub(crate) async fn favicon_ico(res: &mut Response) -> HttpResult<()> {
19    res.send_file("./favicon.ico", &HeaderMap::new()).await;
20    Ok(())
21}
22
23/// Response to the clients with the memory data by streaming
24pub struct MemoryStream(VecDeque<Vec<u8>>);
25
26impl MemoryStream {
27    /// Construct a `MemoryStream` by binary data and a required fragment size
28    #[allow(dead_code)]
29    pub fn new(data: Vec<u8>, chunk_size: usize) -> Self {
30        Self(data.chunks(chunk_size).map(|v| v.to_vec()).collect())
31    }
32}
33
34impl Stream for MemoryStream {
35    type Item = Result<Vec<u8>, std::io::Error>;
36
37    fn poll_next(
38        mut self: std::pin::Pin<&mut Self>,
39        _cx: &mut std::task::Context<'_>,
40    ) -> std::task::Poll<Option<Self::Item>> {
41        if let Some(frame) = self.0.pop_front() {
42            Poll::Ready(Some(Ok(frame)))
43        } else {
44            Poll::Ready(None)
45        }
46    }
47}