zero4rs 2.0.0

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

use std::time::Duration;

use futures::stream::StreamExt;

use actix::Actor;
use actix_web::web;

use crate::server::AppContext;
use crate::websocket::webchat::actions::ServerTime;
use crate::websocket::webchat::output::OutputMessage;
use crate::websocket::webchat::process::process1;
use crate::websocket::webchat::process::process2;
use crate::websocket::webchat::server::Server;

lazy_static! {
    pub static ref TIMER_INTERVAL: Duration = Duration::from_millis(1000);
    pub static ref WEBCHAT_SERVER_TIME_STR: &'static str = "0/1 * * * * * *";
    pub static ref WEBCHAT_BROADCAST_CHANNEL: &'static str = "_webchat_broadcast_channel";
    #[rustfmt::skip]
    pub static ref WEBCHAT_CLOSE_CONNECTION_CHANNEL: &'static str = "_webchat_close_connection_channel";
}

pub struct WebChatContext {
    pub server: actix::Addr<Server>,
}

impl WebChatContext {
    pub fn build(ctx: web::Data<AppContext>) -> Self {
        let s = Self {
            server: Server::build(ctx.clone()).start(),
        };

        Self::server_time(s.server.clone());

        #[rustfmt::skip]
        Self::subscribe_channel(s.server.clone(), ctx.clone(), (*WEBCHAT_BROADCAST_CHANNEL).to_string(), process1);
        #[rustfmt::skip]
        Self::subscribe_channel(s.server.clone(), ctx.clone(), (*WEBCHAT_CLOSE_CONNECTION_CHANNEL).to_string(), process2);

        s
    }

    pub fn server_time(svr: actix::Addr<Server>) {
        tokio::spawn(async move {
            crate::core::looop::running(*WEBCHAT_SERVER_TIME_STR, 10, move || {
                svr.do_send(ServerTime {
                    time: OutputMessage::ts(),
                });
            })
            .await;
        });
    }

    pub fn subscribe_channel<F>(
        svr: actix::Addr<Server>,
        ctx: web::Data<AppContext>,
        channel: String,
        f: F,
    ) where
        F: Fn(&actix::Addr<Server>, &str, &str) + Send + 'static,
    {
        tokio::spawn(async move {
            let mut rx = ctx.rudis().subscribe(&channel).await.unwrap();

            while let Some(message) = rx.next().await {
                f(&svr, &channel, &message);
            }
        });
    }
}