lib.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. mod deps_resolve;
  2. // mod flowy_server;
  3. pub mod module;
  4. use flowy_dispatch::prelude::*;
  5. use module::build_modules;
  6. pub use module::*;
  7. use std::sync::{
  8. atomic::{AtomicBool, Ordering},
  9. Arc,
  10. };
  11. static INIT_LOG: AtomicBool = AtomicBool::new(false);
  12. #[derive(Debug, Clone)]
  13. pub struct FlowySDKConfig {
  14. root: String,
  15. log_filter: String,
  16. }
  17. impl FlowySDKConfig {
  18. pub fn new(root: &str) -> Self {
  19. FlowySDKConfig {
  20. root: root.to_owned(),
  21. log_filter: crate_log_filter(None),
  22. }
  23. }
  24. pub fn log_filter(mut self, filter: &str) -> Self {
  25. self.log_filter = crate_log_filter(Some(filter.to_owned()));
  26. self
  27. }
  28. }
  29. fn crate_log_filter(level: Option<String>) -> String {
  30. let level = level.unwrap_or(std::env::var("RUST_LOG").unwrap_or("info".to_owned()));
  31. let mut filters = vec![];
  32. filters.push(format!("flowy_sdk={}", level));
  33. filters.push(format!("flowy_workspace={}", level));
  34. filters.push(format!("flowy_user={}", level));
  35. filters.push(format!("flowy_document={}", level));
  36. filters.push(format!("flowy_observable={}", level));
  37. filters.push(format!("flowy_ot={}", level));
  38. filters.push(format!("info"));
  39. filters.join(",")
  40. }
  41. #[derive(Clone)]
  42. pub struct FlowySDK {
  43. config: FlowySDKConfig,
  44. dispatch: Arc<EventDispatch>,
  45. }
  46. impl FlowySDK {
  47. pub fn new(config: FlowySDKConfig) -> Self {
  48. init_log(&config);
  49. init_kv(&config.root);
  50. tracing::debug!("🔥 {:?}", config);
  51. let dispatch = Arc::new(init_dispatch(&config.root));
  52. Self { config, dispatch }
  53. }
  54. pub fn dispatch(&self) -> Arc<EventDispatch> { self.dispatch.clone() }
  55. }
  56. fn init_kv(root: &str) {
  57. match flowy_infra::kv::KV::init(root) {
  58. Ok(_) => {},
  59. Err(e) => tracing::error!("Init kv store failedL: {}", e),
  60. }
  61. }
  62. fn init_log(config: &FlowySDKConfig) {
  63. if !INIT_LOG.load(Ordering::SeqCst) {
  64. INIT_LOG.store(true, Ordering::SeqCst);
  65. let _ = flowy_log::Builder::new("flowy")
  66. .local(&config.root)
  67. .env_filter(&config.log_filter)
  68. .build();
  69. }
  70. }
  71. fn init_dispatch(root: &str) -> EventDispatch {
  72. let config = ModuleConfig { root: root.to_owned() };
  73. let dispatch = EventDispatch::construct(|| build_modules(config));
  74. dispatch
  75. }