diff --git a/app_flowy/.vscode/tasks.json b/app_flowy/.vscode/tasks.json index f14c8da465..1baace0f14 100644 --- a/app_flowy/.vscode/tasks.json +++ b/app_flowy/.vscode/tasks.json @@ -22,17 +22,5 @@ // ], "label": "build_flowy_sdk" }, - { - "type": "shell", - "command": "sh ./scripts/code_gen.sh", - "group": "build", - "options": { - "cwd": "${workspaceFolder}/../" - }, - "problemMatcher": [ - "$rustc" - ], - "label": "generate events" - } ] } \ No newline at end of file diff --git a/app_flowy/lib/workspace/application/app/app_bloc.dart b/app_flowy/lib/workspace/application/app/app_bloc.dart index 04648820a3..061b3cec5d 100644 --- a/app_flowy/lib/workspace/application/app/app_bloc.dart +++ b/app_flowy/lib/workspace/application/app/app_bloc.dart @@ -1,4 +1,5 @@ import 'package:app_flowy/workspace/domain/i_app.dart'; +import 'package:flowy_infra/flowy_logger.dart'; import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; @@ -30,7 +31,10 @@ class AppBloc extends Bloc { final viewsOrFailed = await iAppImpl.getViews(); yield viewsOrFailed.fold( (apps) => state.copyWith(views: apps), - (error) => state.copyWith(successOrFailure: right(error)), + (error) { + Log.error(error); + return state.copyWith(successOrFailure: right(error)); + }, ); } } diff --git a/app_flowy/lib/workspace/application/menu/menu_bloc.dart b/app_flowy/lib/workspace/application/menu/menu_bloc.dart index ebea2953a8..351d1eddaa 100644 --- a/app_flowy/lib/workspace/application/menu/menu_bloc.dart +++ b/app_flowy/lib/workspace/application/menu/menu_bloc.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:app_flowy/workspace/domain/i_workspace.dart'; import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart'; import 'package:dartz/dartz.dart'; +import 'package:flowy_infra/flowy_logger.dart'; import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart'; import 'package:flutter/material.dart'; @@ -11,8 +12,8 @@ import 'package:flutter_bloc/flutter_bloc.dart'; part 'menu_bloc.freezed.dart'; class MenuBloc extends Bloc { - final IWorkspace iWorkspaceImpl; - MenuBloc(this.iWorkspaceImpl) : super(MenuState.initial()); + final IWorkspace workspace; + MenuBloc(this.workspace) : super(MenuState.initial()); @override Stream mapEventToState( @@ -40,21 +41,25 @@ class MenuBloc extends Bloc { } Stream _performActionOnCreateApp(CreateApp event) async* { - iWorkspaceImpl.createApp(name: event.name, desc: event.desc).then((result) { - result.fold( - (app) => {}, - (error) async* { - yield state.copyWith(successOrFailure: right(error)); - }, - ); - }); + final result = + await workspace.createApp(name: event.name, desc: event.desc); + yield result.fold( + (app) => state.copyWith(apps: some([app])), + (error) { + Log.error(error); + return state.copyWith(successOrFailure: right(error)); + }, + ); } Stream _fetchApps() async* { - final appsOrFail = await iWorkspaceImpl.getApps(); + final appsOrFail = await workspace.getApps(); yield appsOrFail.fold( (apps) => state.copyWith(apps: some(apps)), - (error) => state.copyWith(successOrFailure: right(error)), + (error) { + Log.error(error); + return state.copyWith(successOrFailure: right(error)); + }, ); } } diff --git a/app_flowy/lib/workspace/application/menu/menu_watch.dart b/app_flowy/lib/workspace/application/menu/menu_watch.dart index 2e0f5f2f93..122fd9c8af 100644 --- a/app_flowy/lib/workspace/application/menu/menu_watch.dart +++ b/app_flowy/lib/workspace/application/menu/menu_watch.dart @@ -1,4 +1,5 @@ import 'package:app_flowy/workspace/domain/i_workspace.dart'; +import 'package:flowy_infra/flowy_logger.dart'; import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart'; import 'package:flutter/material.dart'; @@ -38,7 +39,10 @@ class MenuWatchBloc extends Bloc { void _handleAppsOrFail(Either, WorkspaceError> appsOrFail) { appsOrFail.fold( (apps) => add(MenuWatchEvent.appsReceived(left(apps))), - (error) => add(MenuWatchEvent.appsReceived(right(error))), + (error) { + Log.error(error); + add(MenuWatchEvent.appsReceived(right(error))); + }, ); } } diff --git a/app_flowy/lib/workspace/application/workspace/workspace_list_bloc.dart b/app_flowy/lib/workspace/application/workspace/workspace_list_bloc.dart index b12f1c9b63..da23a0ca71 100644 --- a/app_flowy/lib/workspace/application/workspace/workspace_list_bloc.dart +++ b/app_flowy/lib/workspace/application/workspace/workspace_list_bloc.dart @@ -1,4 +1,5 @@ import 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart'; +import 'package:flowy_infra/flowy_logger.dart'; import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-workspace/workspace_create.pb.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; @@ -31,7 +32,10 @@ class WorkspaceListBloc extends Bloc { yield workspacesOrFailed.fold( (workspaces) => state.copyWith(workspaces: workspaces, successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), + (error) { + Log.error(error); + return state.copyWith(successOrFailure: right(error)); + }, ); } @@ -39,7 +43,10 @@ class WorkspaceListBloc extends Bloc { final result = await repo.openWorkspace(workspace.id); yield result.fold( (workspaces) => state.copyWith(successOrFailure: left(unit)), - (error) => state.copyWith(successOrFailure: right(error)), + (error) { + Log.error(error); + return state.copyWith(successOrFailure: right(error)); + }, ); } @@ -50,7 +57,10 @@ class WorkspaceListBloc extends Bloc { add(const WorkspaceListEvent.fetchWorkspaces()); return state.copyWith(successOrFailure: left(unit)); }, - (error) => state.copyWith(successOrFailure: right(error)), + (error) { + Log.error(error); + return state.copyWith(successOrFailure: right(error)); + }, ); } } diff --git a/app_flowy/macos/Runner.xcodeproj/project.pbxproj b/app_flowy/macos/Runner.xcodeproj/project.pbxproj index 05498ff484..3b621cf215 100644 --- a/app_flowy/macos/Runner.xcodeproj/project.pbxproj +++ b/app_flowy/macos/Runner.xcodeproj/project.pbxproj @@ -159,7 +159,6 @@ 7D41C30A3910C3A40B6085E3 /* Pods-Runner.release.xcconfig */, 1823EB6E74189944EAA69652 /* Pods-Runner.profile.xcconfig */, ); - name = Pods; path = Pods; sourceTree = ""; }; @@ -427,6 +426,7 @@ "@executable_path/../Frameworks", ); PROVISIONING_PROFILE_SPECIFIER = ""; + STRIP_STYLE = "non-global"; SWIFT_VERSION = 5.0; }; name = Profile; @@ -553,6 +553,7 @@ "@executable_path/../Frameworks", ); PROVISIONING_PROFILE_SPECIFIER = ""; + STRIP_STYLE = "non-global"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; }; @@ -573,6 +574,7 @@ "@executable_path/../Frameworks", ); PROVISIONING_PROFILE_SPECIFIER = ""; + STRIP_STYLE = "non-global"; SWIFT_VERSION = 5.0; }; name = Release; diff --git a/app_flowy/macos/Runner/Info.plist b/app_flowy/macos/Runner/Info.plist index 4789daa6a4..c527d81c85 100644 --- a/app_flowy/macos/Runner/Info.plist +++ b/app_flowy/macos/Runner/Info.plist @@ -2,6 +2,11 @@ + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable diff --git a/rust-lib/dart-ffi/Cargo.toml b/rust-lib/dart-ffi/Cargo.toml index a0279e37eb..dbaf8eb045 100644 --- a/rust-lib/dart-ffi/Cargo.toml +++ b/rust-lib/dart-ffi/Cargo.toml @@ -35,5 +35,6 @@ flowy-observable = {path = "../flowy-observable"} [features] observable = ["flowy-observable/dart"] +http_server = ["flowy-sdk/http_server", "flowy-sdk/use_bunyan"] #use_serde = ["bincode"] #use_protobuf= ["protobuf"] \ No newline at end of file diff --git a/rust-lib/dart-ffi/src/lib.rs b/rust-lib/dart-ffi/src/lib.rs index 03ce33e826..f1c98e4738 100644 --- a/rust-lib/dart-ffi/src/lib.rs +++ b/rust-lib/dart-ffi/src/lib.rs @@ -23,8 +23,9 @@ fn dispatch() -> Arc { FLOWY_SDK.read().as_ref().unwrap().dispatc pub extern "C" fn init_sdk(path: *mut c_char) -> i64 { let c_str: &CStr = unsafe { CStr::from_ptr(path) }; let path: &str = c_str.to_str().unwrap(); - log::info!("🔥 FlowySDK start running"); - *FLOWY_SDK.write() = Some(Arc::new(FlowySDK::new(path))); + + let config = FlowySDKConfig::new(path).log_filter("info"); + *FLOWY_SDK.write() = Some(Arc::new(FlowySDK::new(config))); return 1; } diff --git a/rust-lib/flowy-dispatch/src/response/response.rs b/rust-lib/flowy-dispatch/src/response/response.rs index 6892ced68f..2f3f2e7a61 100644 --- a/rust-lib/flowy-dispatch/src/response/response.rs +++ b/rust-lib/flowy-dispatch/src/response/response.rs @@ -47,6 +47,8 @@ impl EventResponse { }, } } + + pub(crate) fn is_success(&self) -> bool { self.status_code == StatusCode::Ok } } impl std::fmt::Display for EventResponse { diff --git a/rust-lib/flowy-sdk/Cargo.toml b/rust-lib/flowy-sdk/Cargo.toml index 20171b36d0..63fade82fa 100644 --- a/rust-lib/flowy-sdk/Cargo.toml +++ b/rust-lib/flowy-sdk/Cargo.toml @@ -8,7 +8,6 @@ edition = "2018" [dependencies] flowy-dispatch = { path = "../flowy-dispatch", features = ["use_tracing"]} flowy-log = { path = "../flowy-log" } -#flowy-log = { path = "../flowy-log", features = ["use_bunyan"] } flowy-user = { path = "../flowy-user" } flowy-infra = { path = "../flowy-infra" } flowy-workspace = { path = "../flowy-workspace" } @@ -25,4 +24,8 @@ bincode = { version = "1.3"} protobuf = {version = "2.24.1"} claim = "0.5.0" tokio = { version = "1", features = ["full"]} -futures-util = "0.3.15" \ No newline at end of file +futures-util = "0.3.15" + +[features] +http_server = ["flowy-user/http_server", "flowy-workspace/http_server"] +use_bunyan = ["flowy-log/use_bunyan"] \ No newline at end of file diff --git a/rust-lib/flowy-sdk/src/deps_resolve/editor_deps_impl.rs b/rust-lib/flowy-sdk/src/deps_resolve/editor_deps_impl.rs index ca2d6023e5..1c6b5cbbbe 100644 --- a/rust-lib/flowy-sdk/src/deps_resolve/editor_deps_impl.rs +++ b/rust-lib/flowy-sdk/src/deps_resolve/editor_deps_impl.rs @@ -13,7 +13,7 @@ pub struct EditorDatabaseImpl { impl DocumentDatabase for EditorDatabaseImpl { fn db_connection(&self) -> Result { self.user_session - .db() + .db_conn() .map_err(|e| ErrorBuilder::new(DocErrorCode::EditorDBConnFailed).error(e).build()) } } diff --git a/rust-lib/flowy-sdk/src/deps_resolve/workspace_deps_impl.rs b/rust-lib/flowy-sdk/src/deps_resolve/workspace_deps_impl.rs index 39bf6bfd62..3023818a97 100644 --- a/rust-lib/flowy-sdk/src/deps_resolve/workspace_deps_impl.rs +++ b/rust-lib/flowy-sdk/src/deps_resolve/workspace_deps_impl.rs @@ -32,7 +32,7 @@ pub struct WorkspaceDatabaseImpl { impl WorkspaceDatabase for WorkspaceDatabaseImpl { fn db_connection(&self) -> Result { self.user_session - .db() + .db_conn() .map_err(|e| ErrorBuilder::new(ErrorCode::DatabaseConnectionFail).error(e).build()) } } diff --git a/rust-lib/flowy-sdk/src/lib.rs b/rust-lib/flowy-sdk/src/lib.rs index 9d6cdac234..38cfec9332 100644 --- a/rust-lib/flowy-sdk/src/lib.rs +++ b/rust-lib/flowy-sdk/src/lib.rs @@ -11,21 +11,42 @@ use std::sync::{ }; 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 { - root: String, + config: FlowySDKConfig, dispatch: Arc, } impl FlowySDK { - pub fn new(root: &str) -> Self { - init_log(root); - init_kv(root); + pub fn new(config: FlowySDKConfig) -> Self { + init_log(&config); + init_kv(&config.root); - tracing::info!("🔥 user folder: {}", root); - let dispatch = Arc::new(init_dispatch(root)); - let root = root.to_owned(); - Self { root, dispatch } + tracing::debug!("🔥 {:?}", config); + let dispatch = Arc::new(init_dispatch(&config.root)); + + Self { config, dispatch } } pub fn dispatch(&self) -> Arc { self.dispatch.clone() } @@ -38,11 +59,14 @@ fn init_kv(root: &str) { } } -fn init_log(directory: &str) { +fn init_log(config: &FlowySDKConfig) { if !INIT_LOG.load(Ordering::SeqCst) { INIT_LOG.store(true, Ordering::SeqCst); - let _ = flowy_log::Builder::new("flowy").local(directory).env_filter("info").build(); + let _ = flowy_log::Builder::new("flowy") + .local(&config.root) + .env_filter(&config.log_filter) + .build(); } } diff --git a/rust-lib/flowy-test/src/lib.rs b/rust-lib/flowy-test/src/lib.rs index bbb59e8772..451059bfe7 100644 --- a/rust-lib/flowy-test/src/lib.rs +++ b/rust-lib/flowy-test/src/lib.rs @@ -2,7 +2,7 @@ pub mod builder; mod helper; use crate::helper::*; -use flowy_sdk::FlowySDK; +use flowy_sdk::{FlowySDK, FlowySDKConfig}; use flowy_user::entities::UserProfile; pub mod prelude { @@ -35,6 +35,6 @@ impl FlowyEnv { } pub fn init_test_sdk() -> FlowyTestSDK { - let root_dir = root_dir(); - FlowySDK::new(&root_dir) + let config = FlowySDKConfig::new(&root_dir()).log_filter("debug"); + FlowySDK::new(config) } diff --git a/rust-lib/flowy-user/src/services/helper.rs b/rust-lib/flowy-user/src/services/helper.rs index 083f746046..b6214b7914 100644 --- a/rust-lib/flowy-user/src/services/helper.rs +++ b/rust-lib/flowy-user/src/services/helper.rs @@ -1,9 +1,12 @@ use std::future::Future; -pub fn spawn(f: F) +pub async fn spawn(f: F) where F: Future + Send + 'static, F::Output: Send + 'static, { - tokio::spawn(f); + match tokio::spawn(f).await { + Ok(_) => {}, + Err(e) => log::error!("{:?}", e), + } } diff --git a/rust-lib/flowy-user/src/services/user/user_session.rs b/rust-lib/flowy-user/src/services/user/user_session.rs index ef61d01ad4..0c665f26f8 100644 --- a/rust-lib/flowy-user/src/services/user/user_session.rs +++ b/rust-lib/flowy-user/src/services/user/user_session.rs @@ -54,7 +54,7 @@ impl UserSession { } } - pub fn db(&self) -> Result { + pub fn db_conn(&self) -> Result { let user_id = self.get_session()?.user_id; self.database.get_connection(&user_id) } @@ -98,7 +98,7 @@ impl UserSession { pub async fn sign_out(&self) -> Result<(), UserError> { let session = self.get_session()?; - let _ = diesel::delete(dsl::user_table.filter(dsl::id.eq(&session.user_id))).execute(&*(self.db()?))?; + let _ = diesel::delete(dsl::user_table.filter(dsl::id.eq(&session.user_id))).execute(&*(self.db_conn()?))?; let _ = self.database.close_user_db(&session.user_id)?; let _ = self.set_session(None)?; let _ = self.sign_out_on_server(&session.token).await?; @@ -109,7 +109,7 @@ impl UserSession { pub async fn update_user(&self, params: UpdateUserParams) -> Result<(), UserError> { let session = self.get_session()?; let changeset = UserTableChangeset::new(params.clone()); - diesel_update_table!(user_table, changeset, self.db()?); + diesel_update_table!(user_table, changeset, self.db_conn()?); let _ = self.update_user_on_server(&session.token, params).await?; Ok(()) @@ -120,7 +120,7 @@ impl UserSession { let token = session.token; let user = dsl::user_table .filter(user_table::id.eq(&session.user_id)) - .first::(&*(self.db()?))?; + .first::(&*(self.db_conn()?))?; let _ = self.read_user_profile_on_server(&token).await?; Ok(UserProfile::from(user)) @@ -140,7 +140,7 @@ impl UserSession { async fn read_user_profile_on_server(&self, token: &str) -> Result<(), UserError> { let server = self.server.clone(); let token = token.to_owned(); - spawn(async move { + let _ = spawn(async move { match server.get_user(&token).await { Ok(profile) => { // @@ -158,7 +158,7 @@ impl UserSession { async fn update_user_on_server(&self, token: &str, params: UpdateUserParams) -> Result<(), UserError> { let server = self.server.clone(); let token = token.to_owned(); - spawn(async move { + let _ = spawn(async move { match server.update_user(&token, params).await { Ok(_) => {}, Err(e) => { @@ -166,26 +166,27 @@ impl UserSession { log::error!("update user profile failed: {:?}", e); }, } - }); + }) + .await; Ok(()) } async fn sign_out_on_server(&self, token: &str) -> Result<(), UserError> { let server = self.server.clone(); let token = token.to_owned(); - spawn(async move { + let _ = spawn(async move { match server.sign_out(&token).await { Ok(_) => {}, Err(e) => log::error!("Sign out failed: {:?}", e), } - }); + }) + .await; Ok(()) } async fn save_user(&self, user: UserTable) -> Result { - let conn = self.db()?; + let conn = self.db_conn()?; let _ = diesel::insert_into(user_table::table).values(user.clone()).execute(&*conn)?; - Ok(user) } @@ -233,7 +234,7 @@ pub async fn update_user(_server: Server, pool: Arc, params: Upd } impl UserDatabaseConnection for UserSession { - fn get_connection(&self) -> Result { self.db().map_err(|e| format!("{:?}", e)) } + fn get_connection(&self) -> Result { self.db_conn().map_err(|e| format!("{:?}", e)) } } const SESSION_CACHE_KEY: &str = "session_cache_key"; diff --git a/rust-lib/flowy-workspace/src/entities/app/app_create.rs b/rust-lib/flowy-workspace/src/entities/app/app_create.rs index 2bab53985f..8713a338e8 100644 --- a/rust-lib/flowy-workspace/src/entities/app/app_create.rs +++ b/rust-lib/flowy-workspace/src/entities/app/app_create.rs @@ -32,7 +32,7 @@ pub struct ColorStyle { pub theme_color: String, } -#[derive(ProtoBuf, Default)] +#[derive(ProtoBuf, Default, Debug)] pub struct CreateAppParams { #[pb(index = 1)] pub workspace_id: String, diff --git a/rust-lib/flowy-workspace/src/entities/app/app_query.rs b/rust-lib/flowy-workspace/src/entities/app/app_query.rs index 958d450960..242d4a5ea1 100644 --- a/rust-lib/flowy-workspace/src/entities/app/app_query.rs +++ b/rust-lib/flowy-workspace/src/entities/app/app_query.rs @@ -34,7 +34,7 @@ impl QueryAppRequest { } } -#[derive(ProtoBuf, Default, Clone)] +#[derive(ProtoBuf, Default, Clone, Debug)] pub struct QueryAppParams { #[pb(index = 1)] pub app_id: String, diff --git a/rust-lib/flowy-workspace/src/entities/app/app_update.rs b/rust-lib/flowy-workspace/src/entities/app/app_update.rs index aac857504c..60880b3b6a 100644 --- a/rust-lib/flowy-workspace/src/entities/app/app_update.rs +++ b/rust-lib/flowy-workspace/src/entities/app/app_update.rs @@ -26,7 +26,7 @@ pub struct UpdateAppRequest { pub is_trash: Option, } -#[derive(ProtoBuf, Default, Clone)] +#[derive(ProtoBuf, Default, Clone, Debug)] pub struct UpdateAppParams { #[pb(index = 1)] pub app_id: String, diff --git a/rust-lib/flowy-workspace/src/entities/view/view_create.rs b/rust-lib/flowy-workspace/src/entities/view/view_create.rs index 3b736871b0..6d0619217e 100644 --- a/rust-lib/flowy-workspace/src/entities/view/view_create.rs +++ b/rust-lib/flowy-workspace/src/entities/view/view_create.rs @@ -47,7 +47,7 @@ pub struct CreateViewRequest { pub view_type: ViewType, } -#[derive(Default, ProtoBuf)] +#[derive(Default, ProtoBuf, Debug)] pub struct CreateViewParams { #[pb(index = 1)] pub belong_to_id: String, diff --git a/rust-lib/flowy-workspace/src/entities/view/view_query.rs b/rust-lib/flowy-workspace/src/entities/view/view_query.rs index 222a72f753..13a375575b 100644 --- a/rust-lib/flowy-workspace/src/entities/view/view_query.rs +++ b/rust-lib/flowy-workspace/src/entities/view/view_query.rs @@ -32,7 +32,7 @@ impl QueryViewRequest { } } -#[derive(Default, ProtoBuf, Clone)] +#[derive(Default, ProtoBuf, Clone, Debug)] pub struct QueryViewParams { #[pb(index = 1)] pub view_id: String, diff --git a/rust-lib/flowy-workspace/src/entities/view/view_update.rs b/rust-lib/flowy-workspace/src/entities/view/view_update.rs index 7eacd1e575..27e0fdaf06 100644 --- a/rust-lib/flowy-workspace/src/entities/view/view_update.rs +++ b/rust-lib/flowy-workspace/src/entities/view/view_update.rs @@ -23,7 +23,7 @@ pub struct UpdateViewRequest { pub is_trash: Option, } -#[derive(Default, ProtoBuf, Clone)] +#[derive(Default, ProtoBuf, Clone, Debug)] pub struct UpdateViewParams { #[pb(index = 1)] pub view_id: String, diff --git a/rust-lib/flowy-workspace/src/entities/workspace/workspace_create.rs b/rust-lib/flowy-workspace/src/entities/workspace/workspace_create.rs index b594f5b132..cab0652db8 100644 --- a/rust-lib/flowy-workspace/src/entities/workspace/workspace_create.rs +++ b/rust-lib/flowy-workspace/src/entities/workspace/workspace_create.rs @@ -15,7 +15,7 @@ pub struct CreateWorkspaceRequest { pub desc: String, } -#[derive(Clone, ProtoBuf, Default)] +#[derive(Clone, ProtoBuf, Default, Debug)] pub struct CreateWorkspaceParams { #[pb(index = 1)] pub name: String, diff --git a/rust-lib/flowy-workspace/src/entities/workspace/workspace_query.rs b/rust-lib/flowy-workspace/src/entities/workspace/workspace_query.rs index 5eb26ec4aa..6514a6fe58 100644 --- a/rust-lib/flowy-workspace/src/entities/workspace/workspace_query.rs +++ b/rust-lib/flowy-workspace/src/entities/workspace/workspace_query.rs @@ -19,7 +19,7 @@ impl QueryWorkspaceRequest { } // Read all workspaces if the workspace_id is None -#[derive(Clone, ProtoBuf, Default)] +#[derive(Clone, ProtoBuf, Default, Debug)] pub struct QueryWorkspaceParams { #[pb(index = 1, one_of)] pub workspace_id: Option, diff --git a/rust-lib/flowy-workspace/src/entities/workspace/workspace_update.rs b/rust-lib/flowy-workspace/src/entities/workspace/workspace_update.rs index 53248afb57..e78e5ad41f 100644 --- a/rust-lib/flowy-workspace/src/entities/workspace/workspace_update.rs +++ b/rust-lib/flowy-workspace/src/entities/workspace/workspace_update.rs @@ -16,7 +16,7 @@ pub struct UpdateWorkspaceRequest { desc: Option, } -#[derive(Clone, ProtoBuf, Default)] +#[derive(Clone, ProtoBuf, Default, Debug)] pub struct UpdateWorkspaceParams { #[pb(index = 1)] pub id: String, diff --git a/rust-lib/flowy-workspace/src/errors.rs b/rust-lib/flowy-workspace/src/errors.rs index 2b994b135f..52e8a192c0 100644 --- a/rust-lib/flowy-workspace/src/errors.rs +++ b/rust-lib/flowy-workspace/src/errors.rs @@ -3,7 +3,7 @@ use derive_more::Display; use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; use flowy_dispatch::prelude::{EventResponse, ResponseBuilder}; use flowy_net::errors::ErrorCode as NetworkErrorCode; -use std::convert::TryInto; +use std::{convert::TryInto, fmt}; #[derive(Debug, Default, Clone, ProtoBuf)] pub struct WorkspaceError { @@ -15,12 +15,7 @@ pub struct WorkspaceError { } impl WorkspaceError { - pub fn new(code: ErrorCode, msg: &str) -> Self { - Self { - code, - msg: msg.to_owned(), - } - } + pub fn new(code: ErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned() } } } #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)] @@ -89,23 +84,15 @@ impl std::default::Default for ErrorCode { impl std::convert::From for WorkspaceError { fn from(error: flowy_net::errors::ServerError) -> Self { match error.code { - NetworkErrorCode::RecordNotFound => ErrorBuilder::new(ErrorCode::RecordNotFound) - .error(error.msg) - .build(), + NetworkErrorCode::RecordNotFound => ErrorBuilder::new(ErrorCode::RecordNotFound).error(error.msg).build(), - _ => ErrorBuilder::new(ErrorCode::ServerError) - .error(error.msg) - .build(), + _ => ErrorBuilder::new(ErrorCode::ServerError).error(error.msg).build(), } } } impl std::convert::From for WorkspaceError { - fn from(error: flowy_database::result::Error) -> Self { - ErrorBuilder::new(ErrorCode::WorkspaceDatabaseError) - .error(error) - .build() - } + fn from(error: flowy_database::result::Error) -> Self { ErrorBuilder::new(ErrorCode::WorkspaceDatabaseError).error(error).build() } } impl flowy_dispatch::Error for WorkspaceError { @@ -115,6 +102,10 @@ impl flowy_dispatch::Error for WorkspaceError { } } +impl fmt::Display for WorkspaceError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}: {}", &self.code, &self.msg) } +} + pub type ErrorBuilder = flowy_infra::errors::Builder; impl flowy_infra::errors::Build for WorkspaceError { diff --git a/rust-lib/flowy-workspace/src/handlers/app_handler.rs b/rust-lib/flowy-workspace/src/handlers/app_handler.rs index 7c1eefb5b6..8d6928cc80 100644 --- a/rust-lib/flowy-workspace/src/handlers/app_handler.rs +++ b/rust-lib/flowy-workspace/src/handlers/app_handler.rs @@ -19,29 +19,33 @@ use crate::{ use flowy_dispatch::prelude::{data_result, Data, DataResult, Unit}; use std::{convert::TryInto, sync::Arc}; -#[tracing::instrument(name = "create_app", skip(data, controller))] -pub(crate) async fn create_app(data: Data, controller: Unit>) -> DataResult { +#[tracing::instrument(skip(data, controller), err)] +pub(crate) async fn create_app_handler( + data: Data, + controller: Unit>, +) -> DataResult { let params: CreateAppParams = data.into_inner().try_into()?; let detail = controller.create_app(params).await?; + data_result(detail) } -#[tracing::instrument(name = "delete_app", skip(data, controller))] -pub(crate) async fn delete_app(data: Data, controller: Unit>) -> Result<(), WorkspaceError> { +#[tracing::instrument(skip(data, controller))] +pub(crate) async fn delete_app_handler(data: Data, controller: Unit>) -> Result<(), WorkspaceError> { let params: DeleteAppParams = data.into_inner().try_into()?; let _ = controller.delete_app(¶ms.app_id).await?; Ok(()) } -#[tracing::instrument(name = "update_app", skip(data, controller))] -pub(crate) async fn update_app(data: Data, controller: Unit>) -> Result<(), WorkspaceError> { +#[tracing::instrument(skip(data, controller))] +pub(crate) async fn update_app_handler(data: Data, controller: Unit>) -> Result<(), WorkspaceError> { let params: UpdateAppParams = data.into_inner().try_into()?; let _ = controller.update_app(params).await?; Ok(()) } -#[tracing::instrument(name = "read_app", skip(data, app_controller, view_controller))] -pub(crate) async fn read_app( +#[tracing::instrument(skip(data, app_controller, view_controller))] +pub(crate) async fn read_app_handler( data: Data, app_controller: Unit>, view_controller: Unit>, diff --git a/rust-lib/flowy-workspace/src/handlers/view_handler.rs b/rust-lib/flowy-workspace/src/handlers/view_handler.rs index 0da670222e..f54bd25416 100644 --- a/rust-lib/flowy-workspace/src/handlers/view_handler.rs +++ b/rust-lib/flowy-workspace/src/handlers/view_handler.rs @@ -17,15 +17,21 @@ use crate::{ use flowy_dispatch::prelude::{data_result, Data, DataResult, Unit}; use std::{convert::TryInto, sync::Arc}; -#[tracing::instrument(name = "create_view", skip(data, controller))] -pub(crate) async fn create_view(data: Data, controller: Unit>) -> DataResult { +#[tracing::instrument(skip(data, controller), err)] +pub(crate) async fn create_view_handler( + data: Data, + controller: Unit>, +) -> DataResult { let params: CreateViewParams = data.into_inner().try_into()?; let view = controller.create_view(params).await?; data_result(view) } -#[tracing::instrument(name = "read_view", skip(data, controller))] -pub(crate) async fn read_view(data: Data, controller: Unit>) -> DataResult { +#[tracing::instrument(skip(data, controller), err)] +pub(crate) async fn read_view_handler( + data: Data, + controller: Unit>, +) -> DataResult { let params: QueryViewParams = data.into_inner().try_into()?; let mut view = controller.read_view(params.clone()).await?; @@ -37,16 +43,22 @@ pub(crate) async fn read_view(data: Data, controller: Unit, controller: Unit>) -> Result<(), WorkspaceError> { +#[tracing::instrument(skip(data, controller), err)] +pub(crate) async fn update_view_handler( + data: Data, + controller: Unit>, +) -> Result<(), WorkspaceError> { let params: UpdateViewParams = data.into_inner().try_into()?; let _ = controller.update_view(params).await?; Ok(()) } -#[tracing::instrument(name = "delete_view", skip(data, controller))] -pub(crate) async fn delete_view(data: Data, controller: Unit>) -> Result<(), WorkspaceError> { +#[tracing::instrument(skip(data, controller), err)] +pub(crate) async fn delete_view_handler( + data: Data, + controller: Unit>, +) -> Result<(), WorkspaceError> { let params: DeleteViewParams = data.into_inner().try_into()?; let _ = controller.delete_view(¶ms.view_id).await?; Ok(()) diff --git a/rust-lib/flowy-workspace/src/handlers/workspace_handler.rs b/rust-lib/flowy-workspace/src/handlers/workspace_handler.rs index c0e2afc27f..7a7105a5ed 100644 --- a/rust-lib/flowy-workspace/src/handlers/workspace_handler.rs +++ b/rust-lib/flowy-workspace/src/handlers/workspace_handler.rs @@ -2,8 +2,8 @@ use crate::{entities::workspace::*, errors::WorkspaceError, services::WorkspaceC use flowy_dispatch::prelude::{data_result, Data, DataResult, Unit}; use std::{convert::TryInto, sync::Arc}; -#[tracing::instrument(name = "create_workspace", skip(data, controller))] -pub(crate) async fn create_workspace( +#[tracing::instrument(skip(data, controller), err)] +pub(crate) async fn create_workspace_handler( data: Data, controller: Unit>, ) -> DataResult { @@ -13,14 +13,14 @@ pub(crate) async fn create_workspace( data_result(detail) } -#[tracing::instrument(name = "read_cur_workspace", skip(controller))] -pub(crate) async fn read_cur_workspace(controller: Unit>) -> DataResult { +#[tracing::instrument(skip(controller), err)] +pub(crate) async fn read_cur_workspace_handler(controller: Unit>) -> DataResult { let workspace = controller.read_cur_workspace().await?; data_result(workspace) } -#[tracing::instrument(name = "read_workspace", skip(data, controller))] -pub(crate) async fn read_workspaces( +#[tracing::instrument(skip(data, controller), err)] +pub(crate) async fn read_workspaces_handler( data: Data, controller: Unit>, ) -> DataResult { @@ -29,8 +29,8 @@ pub(crate) async fn read_workspaces( data_result(workspaces) } -#[tracing::instrument(name = "open_workspace", skip(data, controller))] -pub(crate) async fn open_workspace( +#[tracing::instrument(skip(data, controller), err)] +pub(crate) async fn open_workspace_handler( data: Data, controller: Unit>, ) -> DataResult { diff --git a/rust-lib/flowy-workspace/src/module.rs b/rust-lib/flowy-workspace/src/module.rs index 8dccbbfc0a..61f0be34f5 100644 --- a/rust-lib/flowy-workspace/src/module.rs +++ b/rust-lib/flowy-workspace/src/module.rs @@ -49,22 +49,22 @@ pub fn create(user: Arc, database: Arc .data(view_controller); module = module - .event(WorkspaceEvent::CreateWorkspace, create_workspace) - .event(WorkspaceEvent::ReadCurWorkspace, read_cur_workspace) - .event(WorkspaceEvent::ReadWorkspaces, read_workspaces) - .event(WorkspaceEvent::OpenWorkspace, open_workspace); + .event(WorkspaceEvent::CreateWorkspace, create_workspace_handler) + .event(WorkspaceEvent::ReadCurWorkspace, read_cur_workspace_handler) + .event(WorkspaceEvent::ReadWorkspaces, read_workspaces_handler) + .event(WorkspaceEvent::OpenWorkspace, open_workspace_handler); module = module - .event(WorkspaceEvent::CreateApp, create_app) - .event(WorkspaceEvent::ReadApp, read_app) - .event(WorkspaceEvent::UpdateApp, update_app) - .event(WorkspaceEvent::DeleteApp, delete_app); + .event(WorkspaceEvent::CreateApp, create_app_handler) + .event(WorkspaceEvent::ReadApp, read_app_handler) + .event(WorkspaceEvent::UpdateApp, update_app_handler) + .event(WorkspaceEvent::DeleteApp, delete_app_handler); module = module - .event(WorkspaceEvent::CreateView, create_view) - .event(WorkspaceEvent::ReadView, read_view) - .event(WorkspaceEvent::UpdateView, update_view) - .event(WorkspaceEvent::DeleteView, delete_view); + .event(WorkspaceEvent::CreateView, create_view_handler) + .event(WorkspaceEvent::ReadView, read_view_handler) + .event(WorkspaceEvent::UpdateView, update_view_handler) + .event(WorkspaceEvent::DeleteView, delete_view_handler); module } diff --git a/rust-lib/flowy-workspace/src/services/app_controller.rs b/rust-lib/flowy-workspace/src/services/app_controller.rs index 0b4af02f44..24411ba99f 100644 --- a/rust-lib/flowy-workspace/src/services/app_controller.rs +++ b/rust-lib/flowy-workspace/src/services/app_controller.rs @@ -33,6 +33,7 @@ impl AppController { } } + #[tracing::instrument(level = "debug", skip(self), err)] pub(crate) async fn create_app(&self, params: CreateAppParams) -> Result { let app = self.create_app_on_server(params).await?; let app_table = AppTable::new(app.clone()); @@ -65,12 +66,15 @@ impl AppController { } impl AppController { + #[tracing::instrument(level = "debug", skip(self), err)] async fn create_app_on_server(&self, params: CreateAppParams) -> Result { let token = self.user.token()?; let app = self.server.create_app(&token, params).await?; + log::info!("😁 {:?}", app); Ok(app) } + #[tracing::instrument(level = "debug", skip(self), err)] async fn update_app_on_server(&self, params: UpdateAppParams) -> Result<(), WorkspaceError> { let token = self.user.token()?; let server = self.server.clone(); @@ -86,6 +90,7 @@ impl AppController { Ok(()) } + #[tracing::instrument(level = "debug", skip(self), err)] async fn delete_app_on_server(&self, app_id: &str) -> Result<(), WorkspaceError> { let token = self.user.token()?; let server = self.server.clone(); @@ -104,6 +109,7 @@ impl AppController { Ok(()) } + #[tracing::instrument(level = "debug", skip(self), err)] async fn read_app_on_server(&self, params: QueryAppParams) -> Result<(), WorkspaceError> { let token = self.user.token()?; let server = self.server.clone(); diff --git a/rust-lib/flowy-workspace/src/services/helper.rs b/rust-lib/flowy-workspace/src/services/helper.rs index 083f746046..74624bb10e 100644 --- a/rust-lib/flowy-workspace/src/services/helper.rs +++ b/rust-lib/flowy-workspace/src/services/helper.rs @@ -1,9 +1,10 @@ -use std::future::Future; - -pub fn spawn(f: F) +pub async fn spawn(f: F) where - F: Future + Send + 'static, + F: std::future::Future + Send + 'static, F::Output: Send + 'static, { - tokio::spawn(f); + match tokio::spawn(f).await { + Ok(_) => {}, + Err(e) => log::error!("{:?}", e), + } } diff --git a/rust-lib/flowy-workspace/src/services/server/mod.rs b/rust-lib/flowy-workspace/src/services/server/mod.rs index 85c64fcb54..c51d0d05dc 100644 --- a/rust-lib/flowy-workspace/src/services/server/mod.rs +++ b/rust-lib/flowy-workspace/src/services/server/mod.rs @@ -45,8 +45,11 @@ pub trait WorkspaceServerAPI { // App fn create_app(&self, token: &str, params: CreateAppParams) -> ResultFuture; + fn read_app(&self, token: &str, params: QueryAppParams) -> ResultFuture, WorkspaceError>; + fn update_app(&self, token: &str, params: UpdateAppParams) -> ResultFuture<(), WorkspaceError>; + fn delete_app(&self, token: &str, params: DeleteAppParams) -> ResultFuture<(), WorkspaceError>; } diff --git a/rust-lib/flowy-workspace/src/services/view_controller.rs b/rust-lib/flowy-workspace/src/services/view_controller.rs index aa6d140341..c58b6ee3ee 100644 --- a/rust-lib/flowy-workspace/src/services/view_controller.rs +++ b/rust-lib/flowy-workspace/src/services/view_controller.rs @@ -72,12 +72,14 @@ impl ViewController { } impl ViewController { + #[tracing::instrument(skip(self), err)] async fn create_view_on_server(&self, params: CreateViewParams) -> Result { let token = self.user.token()?; let view = self.server.create_view(&token, params).await?; Ok(view) } + #[tracing::instrument(skip(self), err)] async fn update_view_on_server(&self, params: UpdateViewParams) -> Result<(), WorkspaceError> { let token = self.user.token()?; let server = self.server.clone(); @@ -93,6 +95,7 @@ impl ViewController { Ok(()) } + #[tracing::instrument(skip(self), err)] async fn delete_view_on_server(&self, view_id: &str) -> Result<(), WorkspaceError> { let token = self.user.token()?; let server = self.server.clone(); @@ -111,6 +114,7 @@ impl ViewController { Ok(()) } + #[tracing::instrument(skip(self), err)] async fn read_view_on_server(&self, params: QueryViewParams) -> Result<(), WorkspaceError> { let token = self.user.token()?; let server = self.server.clone(); diff --git a/rust-lib/flowy-workspace/src/services/workspace_controller.rs b/rust-lib/flowy-workspace/src/services/workspace_controller.rs index 66842234a9..87e83feac1 100644 --- a/rust-lib/flowy-workspace/src/services/workspace_controller.rs +++ b/rust-lib/flowy-workspace/src/services/workspace_controller.rs @@ -142,12 +142,14 @@ impl WorkspaceController { Ok((token, server)) } + #[tracing::instrument(skip(self), err)] async fn create_workspace_on_server(&self, params: CreateWorkspaceParams) -> Result { let token = self.user.token()?; let workspace = self.server.create_workspace(&token, params).await?; Ok(workspace) } + #[tracing::instrument(skip(self), err)] async fn update_workspace_on_server(&self, params: UpdateWorkspaceParams) -> Result<(), WorkspaceError> { let (token, server) = self.token_with_server()?; spawn(async move { @@ -162,6 +164,7 @@ impl WorkspaceController { Ok(()) } + #[tracing::instrument(skip(self), err)] async fn delete_workspace_on_server(&self, workspace_id: &str) -> Result<(), WorkspaceError> { let params = DeleteWorkspaceParams { workspace_id: workspace_id.to_string(), @@ -179,6 +182,7 @@ impl WorkspaceController { Ok(()) } + #[tracing::instrument(skip(self), err)] async fn read_workspaces_on_server(&self, params: QueryWorkspaceParams) -> Result<(), WorkspaceError> { let (token, server) = self.token_with_server()?; spawn(async move { diff --git a/scripts/build_sdk.sh b/scripts/build_sdk.sh index 69e8445df5..6591eed8d2 100755 --- a/scripts/build_sdk.sh +++ b/scripts/build_sdk.sh @@ -12,4 +12,5 @@ rustup show # 2. ~/.bashrc # 3. ~/.profile # 4. ~/.zshrc -cargo make desktop \ No newline at end of file +cargo make desktop +#cargo make gen_dart_event \ No newline at end of file diff --git a/scripts/code_gen.sh b/scripts/code_gen.sh deleted file mode 100755 index 21207f6fca..0000000000 --- a/scripts/code_gen.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -#!/usr/bin/env fish -cargo make gen_dart_event \ No newline at end of file diff --git a/scripts/install_diesel.sh b/scripts/install_diesel.sh deleted file mode 100755 index d900911f09..0000000000 --- a/scripts/install_diesel.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -brew install sqlite3 -cargo install diesel_cli --no-default-features --features sqlite \ No newline at end of file diff --git a/scripts/install_rust.sh b/scripts/install_rust.sh deleted file mode 100755 index 0a778ac670..0000000000 --- a/scripts/install_rust.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -echo 'install rust' -sudo xcode-select -s /Applications/Xcode.app/Contents/Developer -curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly -y -echo 'export PATH="$$HOME/.cargo/bin:$$PATH"' >> ~/.bash_profile -source ~/.bash_profile \ No newline at end of file diff --git a/scripts/install_tool.sh b/scripts/install_tool.sh deleted file mode 100755 index b32b425a8e..0000000000 --- a/scripts/install_tool.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -#targets -rustup target add x86_64-apple-darwin - -#tools -echo 'install tools' -rustup component add rustfmt -cargo install cargo-expand -cargo install cargo-watch -cargo install cargo-cache -cargo install bunyan - -#protobuf code gen env -brew install protobuf@3.13 -brew tap dart-lang/dart -brew install dart -pub global activate protoc_plugin - -cargo install --version 2.20.0 protobuf-codegen \ No newline at end of file diff --git a/scripts/makefile/desktop.toml b/scripts/makefile/desktop.toml index 09a56c6156..45bfe70d92 100644 --- a/scripts/makefile/desktop.toml +++ b/scripts/makefile/desktop.toml @@ -25,7 +25,7 @@ description = "Build desktop targets." script = [ """ cd rust-lib/ - cargo build --package=dart-ffi --target ${DESKTOP_TARGET} --features="observable" + cargo build --package=dart-ffi --target ${DESKTOP_TARGET} --features="observable","http_server" cd ../ """, ] diff --git a/scripts/makefile/env.toml b/scripts/makefile/env.toml new file mode 100644 index 0000000000..1defbfc1c3 --- /dev/null +++ b/scripts/makefile/env.toml @@ -0,0 +1,43 @@ +[tasks.env_setup] +script = """ +brew install sqlite3 +cargo install diesel_cli --no-default-features --features sqlite +""" + +[tasks.install_sqlite3] +script = """ +brew install sqlite3 +cargo install diesel_cli --no-default-features --features sqlite +""" + +[tasks.install_rust] +script = """ +echo 'install rust' +sudo xcode-select -s /Applications/Xcode.app/Contents/Developer +curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly -y +echo 'export PATH="$$HOME/.cargo/bin:$$PATH"' >> ~/.bash_profile +source ~/.bash_profile +""" + +[tasks.install_tools] +script = """ +#targets +rustup target add x86_64-apple-darwin + +#tools +echo 'install tools' +rustup component add rustfmt +cargo install cargo-expand +cargo install cargo-watch +cargo install cargo-cache +cargo install bunyan + +#protobuf code gen env +brew install protobuf@3.13 +brew tap dart-lang/dart +brew install dart +pub global activate protoc_plugin + +cargo install --version 2.20.0 protobuf-codegen +""" +