supabase_config.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use serde::{Deserialize, Serialize};
  2. use flowy_error::{ErrorCode, FlowyError};
  3. pub const ENABLE_SUPABASE_SYNC: &str = "ENABLE_SUPABASE_SYNC";
  4. pub const SUPABASE_URL: &str = "SUPABASE_URL";
  5. pub const SUPABASE_ANON_KEY: &str = "SUPABASE_ANON_KEY";
  6. pub const SUPABASE_DB: &str = "SUPABASE_DB";
  7. pub const SUPABASE_DB_USER: &str = "SUPABASE_DB_USER";
  8. pub const SUPABASE_DB_PASSWORD: &str = "SUPABASE_DB_PASSWORD";
  9. pub const SUPABASE_DB_PORT: &str = "SUPABASE_DB_PORT";
  10. /// The configuration for the postgres database. It supports deserializing from the json string that
  11. /// passed from the frontend application. [AppFlowyEnv::parser]
  12. #[derive(Debug, Serialize, Deserialize, Clone, Default)]
  13. pub struct SupabaseConfiguration {
  14. /// The url of the supabase server.
  15. pub url: String,
  16. /// The key of the supabase server.
  17. pub anon_key: String,
  18. }
  19. impl SupabaseConfiguration {
  20. pub fn from_env() -> Result<Self, FlowyError> {
  21. let url = std::env::var(SUPABASE_URL)
  22. .map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_URL"))?;
  23. let anon_key = std::env::var(SUPABASE_ANON_KEY)
  24. .map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_ANON_KEY"))?;
  25. if url.is_empty() || anon_key.is_empty() {
  26. return Err(FlowyError::new(
  27. ErrorCode::InvalidAuthConfig,
  28. "Missing SUPABASE_URL or SUPABASE_ANON_KEY",
  29. ));
  30. }
  31. Ok(Self { url, anon_key })
  32. }
  33. /// Write the configuration to the environment variables.
  34. pub fn write_env(&self) {
  35. std::env::set_var(SUPABASE_URL, &self.url);
  36. std::env::set_var(SUPABASE_ANON_KEY, &self.anon_key);
  37. }
  38. }