lib.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(Clone)]
  13. pub struct FlowySDK {
  14. root: String,
  15. dispatch: Arc<EventDispatch>,
  16. }
  17. impl FlowySDK {
  18. pub fn new(root: &str) -> Self {
  19. init_log(root);
  20. init_kv(root);
  21. tracing::info!("🔥 user folder: {}", root);
  22. let dispatch = Arc::new(init_dispatch(root));
  23. let root = root.to_owned();
  24. Self { root, dispatch }
  25. }
  26. pub fn dispatch(&self) -> Arc<EventDispatch> { self.dispatch.clone() }
  27. }
  28. fn init_kv(root: &str) {
  29. match flowy_infra::kv::KV::init(root) {
  30. Ok(_) => {},
  31. Err(e) => tracing::error!("Init kv store failedL: {}", e),
  32. }
  33. }
  34. fn init_log(directory: &str) {
  35. if !INIT_LOG.load(Ordering::SeqCst) {
  36. INIT_LOG.store(true, Ordering::SeqCst);
  37. let _ = flowy_log::Builder::new("flowy").local(directory).env_filter("info").build();
  38. }
  39. }
  40. fn init_dispatch(root: &str) -> EventDispatch {
  41. let config = ModuleConfig { root: root.to_owned() };
  42. let dispatch = EventDispatch::construct(|| build_modules(config));
  43. dispatch
  44. }