context.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use crate::service::{
  2. doc::doc::DocBiz,
  3. ws::{WsBizHandlers, WsServer},
  4. };
  5. use actix::Addr;
  6. use actix_web::web::Data;
  7. use flowy_ws::WsModule;
  8. use sqlx::PgPool;
  9. use std::{io, sync::Arc};
  10. pub type FlowyRuntime = tokio::runtime::Runtime;
  11. #[derive(Clone)]
  12. pub struct AppContext {
  13. pub ws_server: Data<Addr<WsServer>>,
  14. pub pg_pool: Data<PgPool>,
  15. pub ws_bizs: Data<WsBizHandlers>,
  16. pub doc_biz: Data<Arc<DocBiz>>,
  17. pub runtime: Data<FlowyRuntime>,
  18. }
  19. impl AppContext {
  20. pub fn new(ws_server: Addr<WsServer>, db_pool: PgPool) -> Self {
  21. let ws_server = Data::new(ws_server);
  22. let pg_pool = Data::new(db_pool);
  23. let runtime = Data::new(runtime().unwrap());
  24. let mut ws_bizs = WsBizHandlers::new();
  25. let doc_biz = Arc::new(DocBiz::new(pg_pool.clone()));
  26. ws_bizs.register(WsModule::Doc, doc_biz.clone());
  27. AppContext {
  28. ws_server,
  29. pg_pool,
  30. ws_bizs: Data::new(ws_bizs),
  31. doc_biz: Data::new(doc_biz),
  32. runtime,
  33. }
  34. }
  35. }
  36. fn runtime() -> io::Result<tokio::runtime::Runtime> {
  37. tokio::runtime::Builder::new_multi_thread()
  38. .thread_name("flowy-server-rt")
  39. .enable_io()
  40. .enable_time()
  41. .build()
  42. }