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.
- Loading branch information
1 parent
7c8cd49
commit 71c3a74
Showing
8 changed files
with
178 additions
and
2 deletions.
There are no files selected for viewing
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,86 @@ | ||
use { | ||
super::{AttestationStore, Result}, | ||
async_trait::async_trait, | ||
serde::Serialize, | ||
std::time::Duration, | ||
}; | ||
|
||
const ATTESTATION_TTL_SECS: usize = 300; | ||
|
||
pub struct CloudflareKv { | ||
pub account_id: String, | ||
pub namespace_id: String, | ||
pub bearer_token: String, | ||
pub http_client: reqwest::Client, | ||
} | ||
|
||
impl CloudflareKv { | ||
pub fn new(account_id: String, namespace_id: String, bearer_token: String) -> Self { | ||
Self { | ||
account_id, | ||
namespace_id, | ||
bearer_token, | ||
http_client: reqwest::Client::new(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct SetBulkBody<'a> { | ||
expiration: usize, | ||
key: &'a str, | ||
value: &'a str, | ||
} | ||
|
||
#[async_trait] | ||
impl AttestationStore for CloudflareKv { | ||
async fn set_attestation(&self, id: &str, origin: &str) -> Result<()> { | ||
let url = format!( | ||
"https://api.cloudflare.com/client/v4/accounts/{account_id}/storage/kv/namespaces/{namespace_id}/bulk", | ||
account_id = self.account_id, namespace_id = self.namespace_id | ||
); | ||
let res = self | ||
.http_client | ||
.put(&url) | ||
.bearer_auth(&self.bearer_token) | ||
.json(&vec![SetBulkBody { | ||
expiration: ATTESTATION_TTL_SECS, | ||
key: id, | ||
value: 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 = format!( | ||
"https://api.cloudflare.com/client/v4/accounts/{account_id}/storage/kv/namespaces/{namespace_id}/values/{id}", | ||
account_id = self.account_id, namespace_id = self.namespace_id | ||
); | ||
let response = self | ||
.http_client | ||
.get(&url) | ||
.bearer_auth(&self.bearer_token) | ||
.timeout(Duration::from_secs(1)) | ||
.send() | ||
.await?; | ||
// TODO what is the status code for a key not found? | ||
// TODO for not-key not found errors throw error instead of None | ||
if response.status().is_success() { | ||
let value = response.text().await?; | ||
Ok(Some(value)) | ||
} else { | ||
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use { | ||
super::{cf_kv::CloudflareKv, AttestationStore, Result}, | ||
crate::util::redis, | ||
async_trait::async_trait, | ||
}; | ||
|
||
pub struct MigrationStore { | ||
redis: redis::Adapter, | ||
cf_kv: CloudflareKv, | ||
} | ||
|
||
impl MigrationStore { | ||
pub fn new(redis: redis::Adapter, cf_kv: CloudflareKv) -> Self { | ||
Self { redis, cf_kv } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl AttestationStore for MigrationStore { | ||
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); | ||
tokio::try_join!(redis_fut, cf_kv_fut).map(|_| ()) | ||
} | ||
|
||
async fn get_attestation(&self, id: &str) -> Result<Option<String>> { | ||
if let Some(attestation) = self.redis.get_attestation(id).await? { | ||
Ok(Some(attestation)) | ||
} else { | ||
self.cf_kv.get_attestation(id).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 |
---|---|---|
@@ -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