2023-07-29 09:46:24 +08:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
use anyhow::Error;
|
|
|
|
use parking_lot::RwLock;
|
2023-07-29 09:46:24 +08:00
|
|
|
use postgrest::Postgrest;
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
use flowy_error::{ErrorCode, FlowyError};
|
2023-07-29 09:46:24 +08:00
|
|
|
use flowy_server_config::supabase_config::SupabaseConfiguration;
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
use crate::AppFlowyEncryption;
|
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
/// Creates a wrapper for Postgrest, which allows us to extend the functionality of Postgrest.
|
2023-08-17 23:46:39 +08:00
|
|
|
pub struct PostgresWrapper {
|
|
|
|
inner: Postgrest,
|
|
|
|
pub encryption: Weak<dyn AppFlowyEncryption>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PostgresWrapper {
|
|
|
|
pub fn secret(&self) -> Option<String> {
|
|
|
|
match self.encryption.upgrade() {
|
|
|
|
None => None,
|
|
|
|
Some(encryption) => encryption.get_secret(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 09:46:24 +08:00
|
|
|
|
|
|
|
impl Deref for PostgresWrapper {
|
|
|
|
type Target = Postgrest;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2023-08-17 23:46:39 +08:00
|
|
|
&self.inner
|
2023-07-29 09:46:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RESTfulPostgresServer {
|
|
|
|
pub postgrest: Arc<PostgresWrapper>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RESTfulPostgresServer {
|
2023-08-17 23:46:39 +08:00
|
|
|
pub fn new(config: SupabaseConfiguration, encryption: Weak<dyn AppFlowyEncryption>) -> Self {
|
2023-07-29 09:46:24 +08:00
|
|
|
let url = format!("{}/rest/v1", config.url);
|
|
|
|
let auth = format!("Bearer {}", config.anon_key);
|
|
|
|
let postgrest = Postgrest::new(url)
|
|
|
|
.insert_header("apikey", config.anon_key)
|
|
|
|
.insert_header("Authorization", auth);
|
|
|
|
Self {
|
2023-08-17 23:46:39 +08:00
|
|
|
postgrest: Arc::new(PostgresWrapper {
|
|
|
|
inner: postgrest,
|
|
|
|
encryption,
|
|
|
|
}),
|
2023-07-29 09:46:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SupabaseServerService: Send + Sync + 'static {
|
|
|
|
fn get_postgrest(&self) -> Option<Arc<PostgresWrapper>>;
|
|
|
|
fn try_get_postgrest(&self) -> Result<Arc<PostgresWrapper>, Error>;
|
|
|
|
fn try_get_weak_postgrest(&self) -> Result<Weak<PostgresWrapper>, Error>;
|
|
|
|
}
|
|
|
|
|
2023-08-17 23:46:39 +08:00
|
|
|
impl<T> SupabaseServerService for Arc<T>
|
|
|
|
where
|
|
|
|
T: SupabaseServerService,
|
|
|
|
{
|
|
|
|
fn get_postgrest(&self) -> Option<Arc<PostgresWrapper>> {
|
|
|
|
(**self).get_postgrest()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_get_postgrest(&self) -> Result<Arc<PostgresWrapper>, Error> {
|
|
|
|
(**self).try_get_postgrest()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_get_weak_postgrest(&self) -> Result<Weak<PostgresWrapper>, Error> {
|
|
|
|
(**self).try_get_weak_postgrest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 09:46:24 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SupabaseServerServiceImpl(pub Arc<RwLock<Option<Arc<RESTfulPostgresServer>>>>);
|
|
|
|
|
|
|
|
impl SupabaseServerServiceImpl {
|
|
|
|
pub fn new(postgrest: Arc<RESTfulPostgresServer>) -> Self {
|
|
|
|
Self(Arc::new(RwLock::new(Some(postgrest))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SupabaseServerService for SupabaseServerServiceImpl {
|
|
|
|
fn get_postgrest(&self) -> Option<Arc<PostgresWrapper>> {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.read()
|
|
|
|
.as_ref()
|
|
|
|
.map(|server| server.postgrest.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_get_postgrest(&self) -> Result<Arc<PostgresWrapper>, Error> {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.read()
|
|
|
|
.as_ref()
|
|
|
|
.map(|server| server.postgrest.clone())
|
|
|
|
.ok_or_else(|| {
|
|
|
|
FlowyError::new(
|
2023-08-07 22:24:04 +08:00
|
|
|
ErrorCode::DataSyncRequired,
|
|
|
|
"Data Sync is disabled, please enable it first",
|
2023-07-29 09:46:24 +08:00
|
|
|
)
|
|
|
|
.into()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_get_weak_postgrest(&self) -> Result<Weak<PostgresWrapper>, Error> {
|
|
|
|
let postgrest = self.try_get_postgrest()?;
|
|
|
|
Ok(Arc::downgrade(&postgrest))
|
|
|
|
}
|
|
|
|
}
|