Skip to content

Commit

Permalink
Make it possible to provide custom connector to SyncedDatabase
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Jastrzebski <[email protected]>
  • Loading branch information
haaawk committed Nov 25, 2024
1 parent 9547648 commit e4784d7
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion libsql/src/database/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cfg_core! {
use crate::EncryptionConfig;
}

use crate::{Database, Result};

use super::DbType;
Expand Down Expand Up @@ -99,6 +100,7 @@ impl Builder<()> {
connector: None,
version: None,
},
connector:None,
},
}
}
Expand Down Expand Up @@ -399,6 +401,7 @@ cfg_sync! {
path: std::path::PathBuf,
flags: crate::OpenFlags,
remote: Remote,
connector: Option<crate::util::ConnectorService>,
}

impl Builder<SyncedDatabase> {
Expand All @@ -408,6 +411,18 @@ cfg_sync! {
self
}

/// Provide a custom http connector that will be used to create http connections.
pub fn connector<C>(mut self, connector: C) -> Builder<SyncedDatabase>
where
C: tower::Service<http::Uri> + Send + Clone + Sync + 'static,
C::Response: crate::util::Socket,
C::Future: Send + 'static,
C::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
self.inner.connector = Some(wrap_connector(connector));
self
}

/// Build a connection to a local database that can be synced to remote server.
pub async fn build(self) -> Result<Database> {
let SyncedDatabase {
Expand All @@ -420,11 +435,16 @@ cfg_sync! {
connector: _,
version: _,
},
connector,
} = self.inner;

let path = path.to_str().ok_or(crate::Error::InvalidUTF8Path)?.to_owned();

let https = super::connector()?;
let https = if let Some(connector) = connector {
connector
} else {
wrap_connector(super::connector()?)
};
use tower::ServiceExt;

let svc = https
Expand Down

0 comments on commit e4784d7

Please sign in to comment.