use crate::service::{ doc::doc::DocBiz, ws::{WsBizHandlers, WsServer}, }; use actix::Addr; use actix_web::web::Data; use flowy_ws::WsModule; use sqlx::PgPool; use std::{io, sync::Arc}; pub type FlowyRuntime = tokio::runtime::Runtime; #[derive(Clone)] pub struct AppContext { pub ws_server: Data>, pub pg_pool: Data, pub ws_bizs: Data, pub doc_biz: Data>, pub runtime: Data, } impl AppContext { pub fn new(ws_server: Addr, db_pool: PgPool) -> Self { let ws_server = Data::new(ws_server); let pg_pool = Data::new(db_pool); let runtime = Data::new(runtime().unwrap()); let mut ws_bizs = WsBizHandlers::new(); let doc_biz = Arc::new(DocBiz::new(pg_pool.clone())); ws_bizs.register(WsModule::Doc, doc_biz.clone()); AppContext { ws_server, pg_pool, ws_bizs: Data::new(ws_bizs), doc_biz: Data::new(doc_biz), runtime, } } } fn runtime() -> io::Result { tokio::runtime::Builder::new_multi_thread() .thread_name("flowy-server-rt") .enable_io() .enable_time() .build() }