zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use crate::prelude2::*;

use futures::stream::StreamExt;
use futures::SinkExt;

// curl -N http://localhost:8000/actions/labs/channel_subscribe
pub async fn channel_subscribe() -> HttpResponse {
    let rx = create_channel().await;

    // Create a streaming response that sends numbers from the channel
    HttpResponse::Ok()
        // .content_type(mime::TEXT_PLAIN_UTF_8)
        .append_header(("Content-Type", "text/event-stream; charset=utf-8"))
        .append_header(("Cache-Control", "no-store"))
        .append_header(("X-Accel-Buffering", "no"))
        .streaming(rx.map(
            |message| -> core::result::Result<actix_web::web::Bytes, std::convert::Infallible> {
                log::info!("{}", message);
                Ok(actix_web::web::Bytes::from(message)) // Convert to Bytes
            },
        ))
}

pub async fn create_channel() -> futures::channel::mpsc::UnboundedReceiver<String> {
    let (mut tx, rx) = futures::channel::mpsc::unbounded::<String>();

    let _ = tx.send(String::from("Channel Connected")).await;

    tokio::spawn(async move {
        loop {
            match tx.send(String::from("hellol啊啦啦啦啦")).await {
                Ok(()) => {
                    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
                }
                Err(e) => {
                    log::warn!("hello: error={:?}", e.to_string());
                    break;
                }
            }
        }
    });

    rx
}