lib.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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: std::env::var("RUST_LOG").unwrap_or("info".to_owned()),
  22. }
  23. }
  24. pub fn log_filter(mut self, filter: &str) -> Self {
  25. self.log_filter = filter.to_owned();
  26. self
  27. }
  28. }
  29. #[derive(Clone)]
  30. pub struct FlowySDK {
  31. config: FlowySDKConfig,
  32. dispatch: Arc<EventDispatch>,
  33. }
  34. impl FlowySDK {
  35. pub fn new(config: FlowySDKConfig) -> Self {
  36. init_log(&config);
  37. init_kv(&config.root);
  38. tracing::debug!("🔥 {:?}", config);
  39. let dispatch = Arc::new(init_dispatch(&config.root));
  40. Self { config, dispatch }
  41. }
  42. pub fn dispatch(&self) -> Arc<EventDispatch> { self.dispatch.clone() }
  43. }
  44. fn init_kv(root: &str) {
  45. match flowy_infra::kv::KV::init(root) {
  46. Ok(_) => {},
  47. Err(e) => tracing::error!("Init kv store failedL: {}", e),
  48. }
  49. }
  50. fn init_log(config: &FlowySDKConfig) {
  51. if !INIT_LOG.load(Ordering::SeqCst) {
  52. INIT_LOG.store(true, Ordering::SeqCst);
  53. let _ = flowy_log::Builder::new("flowy")
  54. .local(&config.root)
  55. .env_filter(&config.log_filter)
  56. .build();
  57. }
  58. }
  59. fn init_dispatch(root: &str) -> EventDispatch {
  60. let config = ModuleConfig { root: root.to_owned() };
  61. let dispatch = EventDispatch::construct(|| build_modules(config));
  62. dispatch
  63. }