2023-05-23 15:55:21 +00:00
|
|
|
use serde::Deserialize;
|
2023-11-17 07:38:56 +00:00
|
|
|
use serde_repr::Deserialize_repr;
|
2023-05-23 15:55:21 +00:00
|
|
|
|
2023-10-02 09:22:22 +00:00
|
|
|
use flowy_server_config::af_cloud_config::AFCloudConfiguration;
|
2023-07-14 05:37:13 +00:00
|
|
|
use flowy_server_config::supabase_config::SupabaseConfiguration;
|
2023-07-05 12:57:09 +00:00
|
|
|
|
2023-05-23 15:55:21 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
pub struct AppFlowyEnv {
|
2023-11-17 07:38:56 +00:00
|
|
|
cloud_type: CloudType,
|
2023-05-23 15:55:21 +00:00
|
|
|
supabase_config: SupabaseConfiguration,
|
2023-10-02 09:22:22 +00:00
|
|
|
appflowy_cloud_config: AFCloudConfiguration,
|
2023-05-23 15:55:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 07:38:56 +00:00
|
|
|
const CLOUT_TYPE_STR: &str = "APPFLOWY_CLOUD_ENV_CLOUD_TYPE";
|
|
|
|
|
|
|
|
#[derive(Deserialize_repr, Debug, Clone)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum CloudType {
|
|
|
|
Local = 0,
|
|
|
|
Supabase = 1,
|
|
|
|
AppFlowyCloud = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CloudType {
|
|
|
|
fn write_env(&self) {
|
|
|
|
let s = self.clone() as u8;
|
|
|
|
std::env::set_var(CLOUT_TYPE_STR, s.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn from_str(s: &str) -> Self {
|
|
|
|
match s {
|
|
|
|
"0" => CloudType::Local,
|
|
|
|
"1" => CloudType::Supabase,
|
|
|
|
"2" => CloudType::AppFlowyCloud,
|
|
|
|
_ => CloudType::Local,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn from_env() -> Self {
|
|
|
|
let cloud_type_str = std::env::var(CLOUT_TYPE_STR).unwrap_or_default();
|
|
|
|
CloudType::from_str(&cloud_type_str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 15:55:21 +00:00
|
|
|
impl AppFlowyEnv {
|
2023-08-17 15:46:39 +00:00
|
|
|
/// Parse the environment variable from the frontend application. The frontend will
|
|
|
|
/// pass the environment variable as a json string after launching.
|
2023-11-12 10:00:07 +00:00
|
|
|
pub fn write_env_from(env_str: &str) {
|
2023-05-23 15:55:21 +00:00
|
|
|
if let Ok(env) = serde_json::from_str::<AppFlowyEnv>(env_str) {
|
2023-11-17 07:38:56 +00:00
|
|
|
let _ = env.cloud_type.write_env();
|
|
|
|
let is_valid = env.appflowy_cloud_config.write_env().is_ok();
|
|
|
|
// Note on Configuration Priority:
|
|
|
|
// If both Supabase config and AppFlowy cloud config are provided in the '.env' file,
|
|
|
|
// the AppFlowy cloud config will be prioritized and the Supabase config ignored.
|
|
|
|
// Ensure only one of these configurations is active at any given time.
|
|
|
|
if !is_valid {
|
|
|
|
let _ = env.supabase_config.write_env();
|
|
|
|
}
|
2023-05-23 15:55:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|