supabase_config.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. }
  22. impl SupabaseConfiguration {
  23. pub fn from_env() -> Result<Self, FlowyError> {
  24. Ok(Self {
  25. url: std::env::var(SUPABASE_URL)
  26. .map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_URL"))?,
  27. anon_key: std::env::var(SUPABASE_ANON_KEY)
  28. .map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_ANON_KEY"))?,
  29. jwt_secret: std::env::var(SUPABASE_JWT_SECRET).map_err(|_| {
  30. FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_JWT_SECRET")
  31. })?,
  32. })
  33. }
  34. /// Write the configuration to the environment variables.
  35. pub fn write_env(&self) {
  36. std::env::set_var(SUPABASE_URL, &self.url);
  37. std::env::set_var(SUPABASE_ANON_KEY, &self.anon_key);
  38. std::env::set_var(SUPABASE_JWT_SECRET, &self.jwt_secret);
  39. }
  40. }