feat: config (#2552)

* feat: save project config

* feat: implement dart key value store
This commit is contained in:
Nathan.fooo 2023-05-17 16:33:44 +08:00 committed by GitHub
parent d01afab96e
commit f04d64a191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 250 additions and 19 deletions

View File

@ -0,0 +1,17 @@
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/protobuf/flowy-config/entities.pb.dart';
class Config {
static Future<void> setSupabaseConfig({
required String url,
required String key,
required String secret,
}) async {
await ConfigEventSetSupabaseConfig(
SupabaseConfigPB.create()
..supabaseUrl = url
..supabaseKey = key
..jwtSecret = secret,
).send();
}
}

View File

@ -0,0 +1,33 @@
import 'package:appflowy_backend/dispatch/dispatch.dart';
import 'package:appflowy_backend/protobuf/flowy-config/entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:dartz/dartz.dart';
/// Key-value store
/// The data is stored in the local storage of the device.
class KeyValue {
static Future<void> set(String key, String value) async {
await ConfigEventSetKeyValue(
KeyValuePB.create()
..key = key
..value = value,
).send();
}
static Future<Either<String, FlowyError>> get(String key) {
return ConfigEventGetKeyValue(
KeyPB.create()..key = key,
).send().then(
(result) => result.fold(
(pb) => left(pb.value),
(error) => right(error),
),
);
}
static Future<void> remove(String key) async {
await ConfigEventRemoveKeyValue(
KeyPB.create()..key = key,
).send();
}
}

View File

@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'package:appflowy/core/notification_helper.dart';
import 'package:appflowy/core/notification/notification_helper.dart';
import 'package:appflowy_backend/protobuf/flowy-document2/notification.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:dartz/dartz.dart';

View File

@ -1,4 +1,4 @@
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/notification.pb.dart';

View File

@ -1,4 +1,4 @@
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/notification.pb.dart';

View File

@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:flowy_infra/notifier.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/notification.pb.dart';

View File

@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:flowy_infra/notifier.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/notification.pb.dart';

View File

@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:flowy_infra/notifier.dart';
import 'package:appflowy_backend/protobuf/flowy-error/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';

View File

@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:flowy_infra/notifier.dart';
import 'package:appflowy_backend/protobuf/flowy-error/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';

View File

@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:dartz/dartz.dart';
import 'package:flowy_infra/notifier.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';

View File

@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:flowy_infra/notifier.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:dartz/dartz.dart';

View File

@ -1,6 +1,6 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/sort_entities.pb.dart';
import 'package:dartz/dartz.dart';
import 'package:flowy_infra/notifier.dart';

View File

@ -4,7 +4,7 @@ import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
import 'dart:typed_data';
import 'package:appflowy/core/grid_notification.dart';
import 'package:appflowy/core/notification/grid_notification.dart';
import 'package:flowy_infra/notifier.dart';
import 'package:dartz/dartz.dart';

View File

@ -1,6 +1,6 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:appflowy/core/folder_notification.dart';
import 'package:appflowy/core/notification/folder_notification.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/protobuf/flowy-notification/subject.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder2/notification.pb.dart';

View File

@ -25,6 +25,7 @@ class InitAppWidgetTask extends LaunchTask {
appearanceSetting: appearanceSetting,
child: widget,
);
Bloc.observer = ApplicationBlocObserver();
runApp(
EasyLocalization(

View File

@ -1,6 +1,6 @@
import 'dart:async';
import 'package:appflowy/core/folder_notification.dart';
import 'package:appflowy/core/user_notification.dart';
import 'package:appflowy/core/notification/folder_notification.dart';
import 'package:appflowy/core/notification/user_notification.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';

View File

@ -1,6 +1,6 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:appflowy/core/folder_notification.dart';
import 'package:appflowy/core/notification/folder_notification.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-notification/subject.pb.dart';

View File

@ -1,6 +1,6 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:appflowy/core/document_notification.dart';
import 'package:appflowy/core/notification/document_notification.dart';
import 'package:appflowy_backend/protobuf/flowy-document2/protobuf.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/protobuf/flowy-notification/subject.pb.dart';

View File

@ -1,6 +1,6 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:appflowy/core/folder_notification.dart';
import 'package:appflowy/core/notification/folder_notification.dart';
import 'package:dartz/dartz.dart';
import 'package:appflowy_backend/protobuf/flowy-notification/subject.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';

View File

@ -1,6 +1,6 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:appflowy/core/folder_notification.dart';
import 'package:appflowy/core/notification/folder_notification.dart';
import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
import 'package:dartz/dartz.dart';
import 'package:flowy_infra/notifier.dart';

View File

@ -22,6 +22,8 @@ import 'package:appflowy_backend/protobuf/flowy-document2/protobuf.dart';
// ignore: unused_import
import 'package:protobuf/protobuf.dart';
import 'dart:convert' show utf8;
import '../protobuf/flowy-config/entities.pb.dart';
import '../protobuf/flowy-config/event_map.pb.dart';
import '../protobuf/flowy-net/event_map.pb.dart';
import 'error.dart';
@ -30,6 +32,7 @@ part 'dart_event/flowy-net/dart_event.dart';
part 'dart_event/flowy-user/dart_event.dart';
part 'dart_event/flowy-database2/dart_event.dart';
part 'dart_event/flowy-document2/dart_event.dart';
part 'dart_event/flowy-config/dart_event.dart';
enum FFIException {
RequestIsEmpty,

View File

@ -1534,6 +1534,21 @@ dependencies = [
"walkdir",
]
[[package]]
name = "flowy-config"
version = "0.1.0"
dependencies = [
"appflowy-integrate",
"bytes",
"flowy-codegen",
"flowy-derive",
"flowy-error",
"flowy-sqlite",
"lib-dispatch",
"protobuf",
"strum_macros",
]
[[package]]
name = "flowy-core"
version = "0.1.0"
@ -1541,6 +1556,7 @@ dependencies = [
"appflowy-integrate",
"bytes",
"console-subscriber",
"flowy-config",
"flowy-database2",
"flowy-document2",
"flowy-error",

View File

@ -14,6 +14,7 @@ members = [
"flowy-error",
"flowy-database2",
"flowy-task",
"flowy-config",
]
[profile.dev]

View File

@ -0,0 +1,23 @@
[package]
name = "flowy-config"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
flowy-sqlite = { path = "../flowy-sqlite" }
flowy-derive = { path = "../../../shared-lib/flowy-derive" }
lib-dispatch = { path = "../lib-dispatch" }
protobuf = {version = "2.28.0"}
bytes = { version = "1.4" }
flowy-error = { path = "../flowy-error" }
strum_macros = "0.21"
appflowy-integrate = {version = "0.1.0" }
[build-dependencies]
flowy-codegen = { path = "../../../shared-lib/flowy-codegen"}
[features]
dart = ["flowy-codegen/dart"]
ts = ["flowy-codegen/ts"]

View File

@ -0,0 +1,3 @@
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/event_map.rs", "src/entities.rs"]
event_files = ["src/event_map.rs"]

View File

@ -0,0 +1,10 @@
fn main() {
let crate_name = env!("CARGO_PKG_NAME");
flowy_codegen::protobuf_file::gen(crate_name);
#[cfg(feature = "dart")]
flowy_codegen::dart_event::gen(crate_name);
#[cfg(feature = "ts")]
flowy_codegen::ts_event::gen(crate_name);
}

View File

@ -0,0 +1,45 @@
use flowy_derive::ProtoBuf;
#[derive(Default, ProtoBuf)]
pub struct KeyValuePB {
#[pb(index = 1)]
pub key: String,
#[pb(index = 2, one_of)]
pub value: Option<String>,
}
#[derive(Default, ProtoBuf)]
pub struct KeyPB {
#[pb(index = 1)]
pub key: String,
}
#[derive(Default, ProtoBuf)]
pub struct SupabaseConfigPB {
#[pb(index = 1)]
supabase_url: String,
#[pb(index = 2)]
supabase_key: String,
#[pb(index = 3)]
jwt_secret: String,
}
#[derive(Default, ProtoBuf)]
pub struct AppFlowyCollabConfigPB {
#[pb(index = 1, one_of)]
aws_config: Option<AWSDynamoDBConfigPB>,
}
#[derive(Default, ProtoBuf)]
pub struct AWSDynamoDBConfigPB {
#[pb(index = 1)]
pub access_key_id: String,
#[pb(index = 2)]
pub secret_access_key: String,
// Region list: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html
#[pb(index = 3)]
pub region: String,
}

View File

@ -0,0 +1,40 @@
use flowy_error::{FlowyError, FlowyResult};
use flowy_sqlite::kv::KV;
use lib_dispatch::prelude::{data_result_ok, AFPluginData, DataResult};
use crate::entities::{KeyPB, KeyValuePB, SupabaseConfigPB};
pub(crate) async fn set_key_value_handler(data: AFPluginData<KeyValuePB>) -> FlowyResult<()> {
let data = data.into_inner();
match data.value {
None => KV::remove(&data.key),
Some(value) => {
KV::set_str(&data.key, value);
},
}
Ok(())
}
pub(crate) async fn get_key_value_handler(
data: AFPluginData<KeyPB>,
) -> DataResult<KeyValuePB, FlowyError> {
let data = data.into_inner();
let value = KV::get_str(&data.key);
data_result_ok(KeyValuePB {
key: data.key,
value,
})
}
pub(crate) async fn remove_key_value_handler(data: AFPluginData<KeyPB>) -> FlowyResult<()> {
let data = data.into_inner();
KV::remove(&data.key);
Ok(())
}
pub(crate) async fn set_supabase_config_handler(
data: AFPluginData<SupabaseConfigPB>,
) -> FlowyResult<()> {
let _config = data.into_inner();
Ok(())
}

View File

@ -0,0 +1,31 @@
use strum_macros::Display;
use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
use lib_dispatch::prelude::AFPlugin;
use crate::event_handler::*;
pub fn init() -> AFPlugin {
AFPlugin::new()
.name(env!("CARGO_PKG_NAME"))
.event(ConfigEvent::SetKeyValue, set_key_value_handler)
.event(ConfigEvent::GetKeyValue, get_key_value_handler)
.event(ConfigEvent::RemoveKeyValue, remove_key_value_handler)
.event(ConfigEvent::SetSupabaseConfig, set_supabase_config_handler)
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, ProtoBuf_Enum, Flowy_Event)]
#[event_err = "FlowyError"]
pub enum ConfigEvent {
#[event(input = "KeyValuePB")]
SetKeyValue = 0,
#[event(input = "KeyPB", output = "KeyValuePB")]
GetKeyValue = 1,
#[event(input = "KeyPB")]
RemoveKeyValue = 2,
#[event(input = "SupabaseConfigPB")]
SetSupabaseConfig = 3,
}

View File

@ -0,0 +1,4 @@
mod entities;
mod event_handler;
pub mod event_map;
mod protobuf;

View File

@ -19,6 +19,7 @@ flowy-document2 = { path = "../flowy-document2" }
#flowy-revision = { path = "../flowy-revision" }
flowy-error = { path = "../flowy-error" }
flowy-task = { path = "../flowy-task" }
flowy-config = { path = "../flowy-config" }
appflowy-integrate = { version = "0.1.0" }
tracing = { version = "0.1", features = ["log"] }
@ -45,6 +46,7 @@ dart = [
"flowy-folder2/dart",
"flowy-database2/dart",
"flowy-document2/dart",
"flowy-config/dart",
]
ts = [
"flowy-user/ts",
@ -52,6 +54,7 @@ ts = [
"flowy-folder2/ts",
"flowy-database2/ts",
"flowy-document2/ts",
"flowy-config/ts",
]
rev-sqlite = [
"flowy-sqlite",

View File

@ -17,11 +17,13 @@ pub fn make_plugins(
let network_plugin = flowy_net::event_map::init();
let database_plugin = flowy_database2::event_map::init(database_manager.clone());
let document_plugin2 = flowy_document2::event_map::init(document_manager2.clone());
let config_plugin = flowy_config::event_map::init();
vec![
user_plugin,
folder_plugin,
network_plugin,
database_plugin,
document_plugin2,
config_plugin,
]
}

View File

@ -13,7 +13,6 @@ appflowy-integrate = {version = "0.1.0" }
flowy-derive = { path = "../../../shared-lib/flowy-derive" }
flowy-notification = { path = "../flowy-notification" }
flowy-error = { path = "../flowy-error", features = ["adaptor_serde", "adaptor_database", "adaptor_dispatch", "collab"] }
lib-dispatch = { path = "../lib-dispatch" }
protobuf = {version = "2.28.0"}