Skip to content

Commit 5f1f3f0

Browse files
committed
implement strategies for fetching turso config
1 parent e682819 commit 5f1f3f0

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

src/auth.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::{future::Future, pin::Pin};
2+
3+
use crate::utils::TursoConfig;
4+
5+
pub trait DbAuthStrategy {
6+
fn resolve<'a>(
7+
&'a self,
8+
db_name: &'a str,
9+
client: &'a reqwest::Client,
10+
) -> Pin<Box<dyn Future<Output = Result<TursoConfig, Box<dyn std::error::Error>>> + Send + 'a>>;
11+
}
12+
13+
pub struct GlobeStrategy;
14+
15+
impl DbAuthStrategy for GlobeStrategy {
16+
fn resolve<'a>(
17+
&'a self,
18+
db_name: &'a str,
19+
client: &'a reqwest::Client,
20+
) -> Pin<Box<dyn Future<Output = Result<TursoConfig, Box<dyn std::error::Error>>> + Send + 'a>>
21+
{
22+
Box::pin(async move {
23+
let globe_auth_api = std::env::var("GLOBE_DS_API")?;
24+
25+
let request_body = serde_json::json!({ "db_name": db_name });
26+
27+
let response = client
28+
.post(format!("{}/db/auth", globe_auth_api))
29+
.body(request_body.to_string())
30+
.send()
31+
.await
32+
.map_err(|_| "Failed to fetch auth credentials for database")?;
33+
34+
if !response.status().is_success() {
35+
return Err(format!("Failed to get Auth Token: {}", response.status()).into());
36+
}
37+
38+
let json = response.json().await?;
39+
let config = serde_json::from_value(json)?;
40+
Ok(config)
41+
})
42+
}
43+
}
44+
45+
pub struct EnvVarStrategy;
46+
47+
impl DbAuthStrategy for EnvVarStrategy {
48+
fn resolve<'a>(
49+
&'a self,
50+
_: &'a str,
51+
_client: &'a reqwest::Client,
52+
) -> Pin<Box<dyn Future<Output = Result<TursoConfig, Box<dyn std::error::Error>>> + Send + 'a>>
53+
{
54+
Box::pin(async move {
55+
let url = std::env::var("TURSO_DB_URL")?;
56+
let token = std::env::var("TURSO_DB_TOKEN")?;
57+
58+
Ok(TursoConfig {
59+
db_url: url,
60+
db_token: token,
61+
})
62+
})
63+
}
64+
}

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ use sqlite::{
1515
use utils::execute_async_task;
1616

1717
use crate::{
18-
proxy::get_turso_db,
18+
auth::{DbAuthStrategy, GlobeStrategy},
1919
utils::{
2020
count_parameters, extract_column_names, get_tokio, sql_is_begin_transaction, sql_is_commit,
2121
sql_is_pragma, sql_is_rollback,
2222
},
2323
};
2424

25+
mod auth;
2526
mod proxy;
2627
mod sqlite;
2728
mod utils;
@@ -70,8 +71,8 @@ pub unsafe extern "C" fn sqlite3_open_v2(
7071
.build()
7172
.unwrap();
7273

73-
// get turso config or return SQLITE_ERROR
74-
let turso_config = get_tokio().block_on(get_turso_db(&reqwest_client, filename));
74+
let auth_strategy = Box::new(GlobeStrategy);
75+
let turso_config = get_tokio().block_on(auth_strategy.resolve(filename, &reqwest_client));
7576
if turso_config.is_err() {
7677
eprintln!("LibSqlite3_Turso Error: {}", turso_config.unwrap_err());
7778
return SQLITE_ERROR;

src/proxy.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -230,26 +230,3 @@ pub fn get_execution_result<'a>(
230230

231231
Ok(first_execution_result)
232232
}
233-
234-
pub async fn get_turso_db(client: &Client, db_name: &str) -> Result<TursoConfig, Box<dyn Error>> {
235-
let globe_auth_api = std::env::var("GLOBE_DS_API")?;
236-
237-
let request_body = serde_json::json!({
238-
"db_name": db_name,
239-
});
240-
241-
let response = client
242-
.post(format!("{}/db/auth", globe_auth_api))
243-
.body(request_body.to_string())
244-
.send()
245-
.await
246-
.map_err(|_| "Failed to fetch auth credentials for database")?;
247-
248-
if !response.status().is_success() {
249-
return Err(format!("Failed to get Auth Token: {}", response.status()).into());
250-
}
251-
252-
let json = response.json().await?;
253-
let db_info = serde_json::from_value(json)?;
254-
Ok(db_info)
255-
}

0 commit comments

Comments
 (0)