2023-05-21 18:53:59 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use postgrest::Postgrest;
|
2023-05-23 23:55:21 +08:00
|
|
|
use serde::Deserialize;
|
2023-05-21 18:53:59 +08:00
|
|
|
|
|
|
|
use flowy_error::{ErrorCode, FlowyError};
|
2023-05-23 23:55:21 +08:00
|
|
|
use flowy_folder2::deps::FolderCloudService;
|
2023-05-21 18:53:59 +08:00
|
|
|
use flowy_user::event_map::UserAuthService;
|
|
|
|
|
2023-05-23 23:55:21 +08:00
|
|
|
use crate::supabase::impls::PostgrestUserAuthServiceImpl;
|
2023-05-21 18:53:59 +08:00
|
|
|
use crate::AppFlowyServer;
|
|
|
|
|
2023-05-23 23:55:21 +08:00
|
|
|
pub const SUPABASE_URL: &str = "SUPABASE_URL";
|
|
|
|
pub const SUPABASE_ANON_KEY: &str = "SUPABASE_ANON_KEY";
|
|
|
|
pub const SUPABASE_KEY: &str = "SUPABASE_KEY";
|
|
|
|
pub const SUPABASE_JWT_SECRET: &str = "SUPABASE_JWT_SECRET";
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
2023-05-21 18:53:59 +08:00
|
|
|
pub struct SupabaseConfiguration {
|
|
|
|
/// The url of the supabase server.
|
|
|
|
pub url: String,
|
|
|
|
/// The key of the supabase server.
|
|
|
|
pub key: String,
|
|
|
|
/// The secret used to sign the JWT tokens.
|
|
|
|
pub jwt_secret: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SupabaseConfiguration {
|
2023-05-23 23:55:21 +08:00
|
|
|
/// Load the configuration from the environment variables.
|
|
|
|
/// SUPABASE_URL=https://<your-supabase-url>.supabase.co
|
|
|
|
/// SUPABASE_KEY=<your-supabase-key>
|
|
|
|
/// SUPABASE_JWT_SECRET=<your-supabase-jwt-secret>
|
|
|
|
///
|
2023-05-21 18:53:59 +08:00
|
|
|
pub fn from_env() -> Result<Self, FlowyError> {
|
|
|
|
Ok(Self {
|
|
|
|
url: std::env::var(SUPABASE_URL)
|
|
|
|
.map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_URL"))?,
|
|
|
|
key: std::env::var(SUPABASE_KEY)
|
|
|
|
.map_err(|_| FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_KEY"))?,
|
|
|
|
jwt_secret: std::env::var(SUPABASE_JWT_SECRET).map_err(|_| {
|
|
|
|
FlowyError::new(ErrorCode::InvalidAuthConfig, "Missing SUPABASE_JWT_SECRET")
|
|
|
|
})?,
|
|
|
|
})
|
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
|
|
|
pub fn write_env(&self) {
|
|
|
|
std::env::set_var(SUPABASE_URL, &self.url);
|
|
|
|
std::env::set_var(SUPABASE_KEY, &self.key);
|
|
|
|
std::env::set_var(SUPABASE_JWT_SECRET, &self.jwt_secret);
|
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SupabaseServer {
|
|
|
|
pub postgres: Arc<Postgrest>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SupabaseServer {
|
|
|
|
pub fn new(config: SupabaseConfiguration) -> Self {
|
|
|
|
let url = format!("{}/rest/v1/", config.url);
|
|
|
|
let auth = format!("Bearer {}", config.key);
|
|
|
|
let postgrest = Postgrest::new(url)
|
|
|
|
.insert_header("apikey", config.key)
|
|
|
|
.insert_header("Authorization", auth);
|
|
|
|
let postgres = Arc::new(postgrest);
|
|
|
|
Self { postgres }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AppFlowyServer for SupabaseServer {
|
|
|
|
fn user_service(&self) -> Arc<dyn UserAuthService> {
|
|
|
|
Arc::new(PostgrestUserAuthServiceImpl::new(self.postgres.clone()))
|
|
|
|
}
|
2023-05-23 23:55:21 +08:00
|
|
|
|
|
|
|
fn folder_service(&self) -> Arc<dyn FolderCloudService> {
|
|
|
|
todo!()
|
|
|
|
}
|
2023-05-21 18:53:59 +08:00
|
|
|
}
|