supabase_config.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_JWT_SECRET: &str = "SUPABASE_JWT_SECRET";
  7. pub const SUPABASE_DB: &str = "SUPABASE_DB";
  8. pub const SUPABASE_DB_USER: &str = "SUPABASE_DB_USER";
  9. pub const SUPABASE_DB_PASSWORD: &str = "SUPABASE_DB_PASSWORD";
  10. pub const SUPABASE_DB_PORT: &str = "SUPABASE_DB_PORT";
  11. /// The configuration for the postgres database. It supports deserializing from the json string that
  12. /// passed from the frontend application. [AppFlowyEnv::parser]
  13. #[derive(Debug, Serialize, Deserialize, Clone, Default)]
  14. pub struct SupabaseConfiguration {
  15. /// The url of the supabase server.
  16. pub url: String,
  17. /// The key of the supabase server.
  18. pub anon_key: String,
  19. /// The secret used to sign the JWT tokens.
  20. pub jwt_secret: String,
  21. /// Whether to enable the supabase sync.
  22. /// User can disable it by injecting the environment variable ENABLE_SUPABASE_SYNC=false
  23. pub enable_sync: bool,
  24. }
  25. impl SupabaseConfiguration {
  26. pub fn from_env() -> Result<Self, FlowyError> {
  27. Ok(Self {
  28. enable_sync: std::env::var(ENABLE_SUPABASE_SYNC)
  29. .map(|v| v == "true")
  30. .unwrap_or(false),
  31. url: std::env::var(SUPABASE_URL)
  32. .map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_URL"))?,
  33. anon_key: std::env::var(SUPABASE_ANON_KEY)
  34. .map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_ANON_KEY"))?,
  35. jwt_secret: std::env::var(SUPABASE_JWT_SECRET).map_err(|_| {
  36. FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_JWT_SECRET")
  37. })?,
  38. })
  39. }
  40. /// Write the configuration to the environment variables.
  41. pub fn write_env(&self) {
  42. if self.enable_sync {
  43. std::env::set_var(ENABLE_SUPABASE_SYNC, "true");
  44. } else {
  45. std::env::set_var(ENABLE_SUPABASE_SYNC, "false");
  46. }
  47. std::env::set_var(SUPABASE_URL, &self.url);
  48. std::env::set_var(SUPABASE_ANON_KEY, &self.anon_key);
  49. std::env::set_var(SUPABASE_JWT_SECRET, &self.jwt_secret);
  50. }
  51. }