generated from WalletConnect/rust-http-starter
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: v2 migration Cloudflare KV (#81)
- Loading branch information
1 parent
6871789
commit 527987e
Showing
10 changed files
with
177 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use { | ||
super::{AttestationStore, Result}, | ||
crate::http_server::{CsrfToken, TokenManager}, | ||
async_trait::async_trait, | ||
hyper::StatusCode, | ||
reqwest::Url, | ||
serde::Serialize, | ||
std::time::Duration, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct CloudflareKv { | ||
pub endpoint: Url, | ||
pub token_manager: TokenManager, | ||
pub http_client: reqwest::Client, | ||
} | ||
|
||
impl CloudflareKv { | ||
pub fn new(endpoint: Url, token_manager: TokenManager) -> Self { | ||
Self { | ||
endpoint, | ||
token_manager, | ||
http_client: reqwest::Client::new(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct SetAttestationCompatBody<'a> { | ||
attestation_id: &'a str, | ||
origin: &'a str, | ||
} | ||
|
||
#[async_trait] | ||
impl AttestationStore for CloudflareKv { | ||
async fn set_attestation(&self, id: &str, origin: &str) -> Result<()> { | ||
let url = self.endpoint.join("/attestation")?; | ||
let res = self | ||
.http_client | ||
.post(url) | ||
.header( | ||
CsrfToken::header_name(), | ||
self.token_manager | ||
.generate_csrf_token() | ||
.map_err(|e| anyhow::anyhow!("{e:?}"))?, | ||
) | ||
.json(&SetAttestationCompatBody { | ||
attestation_id: id, | ||
origin, | ||
}) | ||
.timeout(Duration::from_secs(1)) | ||
.send() | ||
.await?; | ||
if res.status().is_success() { | ||
Ok(()) | ||
} else { | ||
Err(anyhow::anyhow!( | ||
"Failed to set attestation: status:{} response body:{:?}", | ||
res.status(), | ||
res.text().await | ||
)) | ||
} | ||
} | ||
|
||
async fn get_attestation(&self, id: &str) -> Result<Option<String>> { | ||
let url = self | ||
.endpoint | ||
.join(&format!("/v1/compat-attestation/{id}"))?; | ||
let response = self | ||
.http_client | ||
.get(url) | ||
.timeout(Duration::from_secs(1)) | ||
.send() | ||
.await?; | ||
match response.status() { | ||
status if status.is_success() => { | ||
let value = response.text().await?; | ||
Ok(Some(value)) | ||
} | ||
StatusCode::NOT_FOUND => Ok(None), | ||
status => Err(anyhow::anyhow!( | ||
"Failed to get attestation: status:{status} response body:{:?}", | ||
response.text().await | ||
)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use { | ||
super::{cf_kv::CloudflareKv, AttestationStore, Result}, | ||
crate::util::redis, | ||
async_trait::async_trait, | ||
}; | ||
|
||
pub struct Store { | ||
redis: redis::Adapter, | ||
cf_kv: CloudflareKv, | ||
} | ||
|
||
impl Store { | ||
pub fn new(redis: redis::Adapter, cf_kv: CloudflareKv) -> Self { | ||
Self { redis, cf_kv } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl AttestationStore for Store { | ||
async fn set_attestation(&self, id: &str, origin: &str) -> Result<()> { | ||
let redis_fut = self.redis.set_attestation(id, origin); | ||
let cf_kv_fut = self.cf_kv.set_attestation(id, origin); | ||
let (redis_res, cf_kv_res) = tokio::join!(redis_fut, cf_kv_fut); | ||
if let Err(e) = cf_kv_res { | ||
log::error!("Failed to set attestation in Cloudflare KV: {e} {e:?}"); | ||
} | ||
redis_res | ||
} | ||
|
||
async fn get_attestation(&self, id: &str) -> Result<Option<String>> { | ||
if let Some(attestation) = self.redis.get_attestation(id).await? { | ||
Ok(Some(attestation)) | ||
} else { | ||
let res = self.cf_kv.get_attestation(id).await; | ||
match res { | ||
Ok(a) => Ok(a), | ||
Err(e) => { | ||
log::error!("Failed to get attestation from Cloudflare KV: {e} {e:?}"); | ||
Ok(None) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod cf_kv; | ||
pub mod migration; | ||
pub mod redis; | ||
|
||
use async_trait::async_trait; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters