2023-07-14 13:37:13 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-07-05 20:57:09 +08:00
|
|
|
|
|
|
|
use flowy_error::{ErrorCode, FlowyError};
|
|
|
|
|
2023-07-14 13:37:13 +08:00
|
|
|
pub const ENABLE_SUPABASE_SYNC: &str = "ENABLE_SUPABASE_SYNC";
|
2023-07-05 20:57:09 +08:00
|
|
|
pub const SUPABASE_URL: &str = "SUPABASE_URL";
|
|
|
|
pub const SUPABASE_ANON_KEY: &str = "SUPABASE_ANON_KEY";
|
|
|
|
|
|
|
|
pub const SUPABASE_DB: &str = "SUPABASE_DB";
|
|
|
|
pub const SUPABASE_DB_USER: &str = "SUPABASE_DB_USER";
|
|
|
|
pub const SUPABASE_DB_PASSWORD: &str = "SUPABASE_DB_PASSWORD";
|
|
|
|
pub const SUPABASE_DB_PORT: &str = "SUPABASE_DB_PORT";
|
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
/// The configuration for the postgres database. It supports deserializing from the json string that
|
|
|
|
/// passed from the frontend application. [AppFlowyEnv::parser]
|
2023-07-14 13:37:13 +08:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
|
2023-07-05 20:57:09 +08:00
|
|
|
pub struct SupabaseConfiguration {
|
|
|
|
/// The url of the supabase server.
|
|
|
|
pub url: String,
|
|
|
|
/// The key of the supabase server.
|
2023-07-29 09:46:24 +08:00
|
|
|
pub anon_key: String,
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SupabaseConfiguration {
|
|
|
|
pub fn from_env() -> Result<Self, FlowyError> {
|
2023-09-01 11:32:45 +08:00
|
|
|
let url = std::env::var(SUPABASE_URL)
|
|
|
|
.map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_URL"))?;
|
|
|
|
|
|
|
|
let anon_key = std::env::var(SUPABASE_ANON_KEY)
|
|
|
|
.map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_ANON_KEY"))?;
|
|
|
|
|
|
|
|
if url.is_empty() || anon_key.is_empty() {
|
|
|
|
return Err(FlowyError::new(
|
|
|
|
ErrorCode::InvalidAuthConfig,
|
|
|
|
"Missing SUPABASE_URL or SUPABASE_ANON_KEY",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self { url, anon_key })
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
/// Write the configuration to the environment variables.
|
2023-07-05 20:57:09 +08:00
|
|
|
pub fn write_env(&self) {
|
|
|
|
std::env::set_var(SUPABASE_URL, &self.url);
|
2023-07-29 09:46:24 +08:00
|
|
|
std::env::set_var(SUPABASE_ANON_KEY, &self.anon_key);
|
2023-07-05 20:57:09 +08:00
|
|
|
}
|
|
|
|
}
|