mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: integrate postgres storage (#2604)
* chore: env config * chore: get user workspace * feat: enable postgres storage * chore: add new env * chore: add set env ffi * chore: pass env before backend init * chore: update * fix: ci tests * chore: commit the generate env file * chore: remove unused import
This commit is contained in:
@ -14,6 +14,7 @@ bytes = { version = "1.4" }
|
||||
flowy-error = { path = "../flowy-error" }
|
||||
strum_macros = "0.21"
|
||||
appflowy-integrate = {version = "0.1.0" }
|
||||
flowy-server = { path = "../flowy-server" }
|
||||
|
||||
[build-dependencies]
|
||||
flowy-codegen = { path = "../../../shared-lib/flowy-codegen"}
|
||||
|
@ -1,4 +1,8 @@
|
||||
use appflowy_integrate::config::AWSDynamoDBConfig;
|
||||
use appflowy_integrate::{CollabTableConfig, SupabaseDBConfig};
|
||||
use flowy_derive::ProtoBuf;
|
||||
use flowy_error::FlowyError;
|
||||
use flowy_server::supabase::SupabaseConfiguration;
|
||||
|
||||
#[derive(Default, ProtoBuf)]
|
||||
pub struct KeyValuePB {
|
||||
@ -15,11 +19,6 @@ pub struct KeyPB {
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
pub const SUPABASE_URL: &str = "SUPABASE_URL";
|
||||
pub const SUPABASE_ANON_KEY: &str = "SUPABASE_ANON_KEY";
|
||||
pub const SUPABASE_KEY: &str = "SUPABASE_KEY";
|
||||
pub const SUPABASE_JWT_SECRET: &str = "SUPABASE_JWT_SECRET";
|
||||
|
||||
#[derive(Default, ProtoBuf)]
|
||||
pub struct SupabaseConfigPB {
|
||||
#[pb(index = 1)]
|
||||
@ -35,28 +34,97 @@ pub struct SupabaseConfigPB {
|
||||
jwt_secret: String,
|
||||
}
|
||||
|
||||
impl SupabaseConfigPB {
|
||||
pub(crate) fn write_to_env(self) {
|
||||
std::env::set_var(SUPABASE_URL, self.supabase_url);
|
||||
std::env::set_var(SUPABASE_ANON_KEY, self.anon_key);
|
||||
std::env::set_var(SUPABASE_KEY, self.key);
|
||||
std::env::set_var(SUPABASE_JWT_SECRET, self.jwt_secret);
|
||||
impl TryFrom<SupabaseConfigPB> for SupabaseConfiguration {
|
||||
type Error = FlowyError;
|
||||
|
||||
fn try_from(value: SupabaseConfigPB) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
url: value.supabase_url,
|
||||
key: value.key,
|
||||
jwt_secret: value.jwt_secret,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, ProtoBuf)]
|
||||
pub struct AppFlowyCollabConfigPB {
|
||||
pub struct CollabPluginConfigPB {
|
||||
#[pb(index = 1, one_of)]
|
||||
aws_config: Option<AWSDynamoDBConfigPB>,
|
||||
pub aws_config: Option<AWSDynamoDBConfigPB>,
|
||||
|
||||
#[pb(index = 2, one_of)]
|
||||
pub supabase_config: Option<SupabaseDBConfigPB>,
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
impl TryFrom<AWSDynamoDBConfigPB> for AWSDynamoDBConfig {
|
||||
type Error = FlowyError;
|
||||
|
||||
fn try_from(config: AWSDynamoDBConfigPB) -> Result<Self, Self::Error> {
|
||||
Ok(AWSDynamoDBConfig {
|
||||
access_key_id: config.access_key_id,
|
||||
secret_access_key: config.secret_access_key,
|
||||
region: config.region,
|
||||
enable: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, ProtoBuf)]
|
||||
pub struct SupabaseDBConfigPB {
|
||||
#[pb(index = 1)]
|
||||
pub supabase_url: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub key: String,
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub jwt_secret: String,
|
||||
|
||||
#[pb(index = 4)]
|
||||
pub collab_table_config: CollabTableConfigPB,
|
||||
}
|
||||
|
||||
impl TryFrom<SupabaseDBConfigPB> for SupabaseDBConfig {
|
||||
type Error = FlowyError;
|
||||
|
||||
fn try_from(config: SupabaseDBConfigPB) -> Result<Self, Self::Error> {
|
||||
let update_table_config = CollabTableConfig::try_from(config.collab_table_config)?;
|
||||
Ok(SupabaseDBConfig {
|
||||
url: config.supabase_url,
|
||||
key: config.key,
|
||||
jwt_secret: config.jwt_secret,
|
||||
collab_table_config: update_table_config,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, ProtoBuf)]
|
||||
pub struct CollabTableConfigPB {
|
||||
#[pb(index = 1)]
|
||||
pub table_name: String,
|
||||
}
|
||||
|
||||
impl TryFrom<CollabTableConfigPB> for CollabTableConfig {
|
||||
type Error = FlowyError;
|
||||
|
||||
fn try_from(config: CollabTableConfigPB) -> Result<Self, Self::Error> {
|
||||
if config.table_name.is_empty() {
|
||||
return Err(FlowyError::internal().context("table_name is empty"));
|
||||
}
|
||||
Ok(CollabTableConfig {
|
||||
table_name: config.table_name,
|
||||
enable: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
use appflowy_integrate::config::AWSDynamoDBConfig;
|
||||
use appflowy_integrate::SupabaseDBConfig;
|
||||
use flowy_error::{FlowyError, FlowyResult};
|
||||
use flowy_server::supabase::SupabaseConfiguration;
|
||||
use flowy_sqlite::kv::KV;
|
||||
use lib_dispatch::prelude::{data_result_ok, AFPluginData, DataResult};
|
||||
|
||||
use crate::entities::{KeyPB, KeyValuePB, SupabaseConfigPB};
|
||||
use crate::entities::{CollabPluginConfigPB, KeyPB, KeyValuePB, SupabaseConfigPB};
|
||||
|
||||
pub(crate) async fn set_key_value_handler(data: AFPluginData<KeyValuePB>) -> FlowyResult<()> {
|
||||
let data = data.into_inner();
|
||||
@ -35,7 +38,24 @@ pub(crate) async fn remove_key_value_handler(data: AFPluginData<KeyPB>) -> Flowy
|
||||
pub(crate) async fn set_supabase_config_handler(
|
||||
data: AFPluginData<SupabaseConfigPB>,
|
||||
) -> FlowyResult<()> {
|
||||
let config = data.into_inner();
|
||||
config.write_to_env();
|
||||
let config = SupabaseConfiguration::try_from(data.into_inner())?;
|
||||
config.write_env();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn set_collab_plugin_config_handler(
|
||||
data: AFPluginData<CollabPluginConfigPB>,
|
||||
) -> FlowyResult<()> {
|
||||
let config = data.into_inner();
|
||||
if let Some(aws_config_pb) = config.aws_config {
|
||||
if let Ok(aws_config) = AWSDynamoDBConfig::try_from(aws_config_pb) {
|
||||
aws_config.write_env();
|
||||
}
|
||||
}
|
||||
if let Some(supabase_config_pb) = config.supabase_config {
|
||||
if let Ok(supabase_config) = SupabaseDBConfig::try_from(supabase_config_pb) {
|
||||
supabase_config.write_env();
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -12,6 +12,10 @@ pub fn init() -> AFPlugin {
|
||||
.event(ConfigEvent::GetKeyValue, get_key_value_handler)
|
||||
.event(ConfigEvent::RemoveKeyValue, remove_key_value_handler)
|
||||
.event(ConfigEvent::SetSupabaseConfig, set_supabase_config_handler)
|
||||
.event(
|
||||
ConfigEvent::SetCollabPluginConfig,
|
||||
set_collab_plugin_config_handler,
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, ProtoBuf_Enum, Flowy_Event)]
|
||||
@ -30,4 +34,7 @@ pub enum ConfigEvent {
|
||||
/// Check out the `write_to_env` of [SupabaseConfigPB].
|
||||
#[event(input = "SupabaseConfigPB")]
|
||||
SetSupabaseConfig = 3,
|
||||
|
||||
#[event(input = "CollabPluginConfigPB")]
|
||||
SetCollabPluginConfig = 4,
|
||||
}
|
||||
|
Reference in New Issue
Block a user