1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- mod deps_resolve;
- // mod flowy_server;
- pub mod module;
- use flowy_dispatch::prelude::*;
- use module::build_modules;
- pub use module::*;
- use std::sync::{
- atomic::{AtomicBool, Ordering},
- Arc,
- };
- static INIT_LOG: AtomicBool = AtomicBool::new(false);
- #[derive(Debug, Clone)]
- pub struct FlowySDKConfig {
- root: String,
- log_filter: String,
- }
- impl FlowySDKConfig {
- pub fn new(root: &str) -> Self {
- FlowySDKConfig {
- root: root.to_owned(),
- log_filter: std::env::var("RUST_LOG").unwrap_or("info".to_owned()),
- }
- }
- pub fn log_filter(mut self, filter: &str) -> Self {
- self.log_filter = filter.to_owned();
- self
- }
- }
- #[derive(Clone)]
- pub struct FlowySDK {
- config: FlowySDKConfig,
- dispatch: Arc<EventDispatch>,
- }
- impl FlowySDK {
- pub fn new(config: FlowySDKConfig) -> Self {
- init_log(&config);
- init_kv(&config.root);
- tracing::debug!("🔥 {:?}", config);
- let dispatch = Arc::new(init_dispatch(&config.root));
- Self { config, dispatch }
- }
- pub fn dispatch(&self) -> Arc<EventDispatch> { self.dispatch.clone() }
- }
- fn init_kv(root: &str) {
- match flowy_infra::kv::KV::init(root) {
- Ok(_) => {},
- Err(e) => tracing::error!("Init kv store failedL: {}", e),
- }
- }
- fn init_log(config: &FlowySDKConfig) {
- if !INIT_LOG.load(Ordering::SeqCst) {
- INIT_LOG.store(true, Ordering::SeqCst);
- let _ = flowy_log::Builder::new("flowy")
- .local(&config.root)
- .env_filter(&config.log_filter)
- .build();
- }
- }
- fn init_dispatch(root: &str) -> EventDispatch {
- let config = ModuleConfig { root: root.to_owned() };
- let dispatch = EventDispatch::construct(|| build_modules(config));
- dispatch
- }
|