2021-07-19 08:15:20 +00:00
|
|
|
mod deps_resolve;
|
2021-08-20 06:38:03 +00:00
|
|
|
// mod flowy_server;
|
2021-06-30 15:11:27 +00:00
|
|
|
pub mod module;
|
|
|
|
|
2021-07-08 13:23:44 +00:00
|
|
|
use flowy_dispatch::prelude::*;
|
2021-06-30 15:11:27 +00:00
|
|
|
use module::build_modules;
|
2021-07-14 13:12:52 +00:00
|
|
|
pub use module::*;
|
2021-09-04 07:12:53 +00:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
2021-07-09 15:31:44 +00:00
|
|
|
|
2021-07-13 05:14:49 +00:00
|
|
|
static INIT_LOG: AtomicBool = AtomicBool::new(false);
|
2021-09-05 05:50:23 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct FlowySDKConfig {
|
|
|
|
root: String,
|
|
|
|
log_filter: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FlowySDKConfig {
|
|
|
|
pub fn new(root: &str) -> Self {
|
|
|
|
FlowySDKConfig {
|
|
|
|
root: root.to_owned(),
|
2021-09-07 09:12:03 +00:00
|
|
|
log_filter: crate_log_filter(None),
|
2021-09-05 05:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_filter(mut self, filter: &str) -> Self {
|
2021-09-07 09:12:03 +00:00
|
|
|
self.log_filter = crate_log_filter(Some(filter.to_owned()));
|
2021-09-05 05:50:23 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:12:03 +00:00
|
|
|
fn crate_log_filter(level: Option<String>) -> String {
|
|
|
|
let level = level.unwrap_or(std::env::var("RUST_LOG").unwrap_or("info".to_owned()));
|
|
|
|
let mut filters = vec![];
|
|
|
|
filters.push(format!("flowy_sdk={}", level));
|
|
|
|
filters.push(format!("flowy_workspace={}", level));
|
|
|
|
filters.push(format!("flowy_user={}", level));
|
2021-09-09 09:34:01 +00:00
|
|
|
filters.push(format!("flowy_document={}", level));
|
2021-09-08 10:25:32 +00:00
|
|
|
filters.push(format!("flowy_observable={}", level));
|
2021-09-15 08:35:40 +00:00
|
|
|
filters.push(format!("flowy_ot={}", level));
|
2021-09-16 10:31:25 +00:00
|
|
|
filters.push(format!("flowy_ws={}", level));
|
2021-09-07 09:12:03 +00:00
|
|
|
filters.push(format!("info"));
|
|
|
|
filters.join(",")
|
|
|
|
}
|
|
|
|
|
2021-09-04 07:12:53 +00:00
|
|
|
#[derive(Clone)]
|
2021-07-14 13:12:52 +00:00
|
|
|
pub struct FlowySDK {
|
2021-09-05 05:50:23 +00:00
|
|
|
config: FlowySDKConfig,
|
2021-09-04 07:12:53 +00:00
|
|
|
dispatch: Arc<EventDispatch>,
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
2021-06-28 15:58:43 +00:00
|
|
|
|
|
|
|
impl FlowySDK {
|
2021-09-05 05:50:23 +00:00
|
|
|
pub fn new(config: FlowySDKConfig) -> Self {
|
|
|
|
init_log(&config);
|
|
|
|
init_kv(&config.root);
|
|
|
|
|
|
|
|
tracing::debug!("🔥 {:?}", config);
|
|
|
|
let dispatch = Arc::new(init_dispatch(&config.root));
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2021-09-05 05:50:23 +00:00
|
|
|
Self { config, dispatch }
|
2021-09-04 07:12:53 +00:00
|
|
|
}
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2021-09-04 07:12:53 +00:00
|
|
|
pub fn dispatch(&self) -> Arc<EventDispatch> { self.dispatch.clone() }
|
|
|
|
}
|
2021-07-17 02:26:05 +00:00
|
|
|
|
2021-09-04 07:12:53 +00:00
|
|
|
fn init_kv(root: &str) {
|
|
|
|
match flowy_infra::kv::KV::init(root) {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => tracing::error!("Init kv store failedL: {}", e),
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
2021-09-04 07:12:53 +00:00
|
|
|
}
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2021-09-05 05:50:23 +00:00
|
|
|
fn init_log(config: &FlowySDKConfig) {
|
2021-09-04 07:12:53 +00:00
|
|
|
if !INIT_LOG.load(Ordering::SeqCst) {
|
|
|
|
INIT_LOG.store(true, Ordering::SeqCst);
|
2021-08-19 06:08:24 +00:00
|
|
|
|
2021-09-05 05:50:23 +00:00
|
|
|
let _ = flowy_log::Builder::new("flowy")
|
|
|
|
.local(&config.root)
|
|
|
|
.env_filter(&config.log_filter)
|
|
|
|
.build();
|
2021-07-13 05:14:49 +00:00
|
|
|
}
|
2021-09-04 07:12:53 +00:00
|
|
|
}
|
2021-06-28 15:58:43 +00:00
|
|
|
|
2021-09-04 07:12:53 +00:00
|
|
|
fn init_dispatch(root: &str) -> EventDispatch {
|
|
|
|
let config = ModuleConfig { root: root.to_owned() };
|
|
|
|
let dispatch = EventDispatch::construct(|| build_modules(config));
|
|
|
|
dispatch
|
2021-06-28 15:58:43 +00:00
|
|
|
}
|