generated from WalletConnect/rust-http-starter
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: v2 migration Cloudflare KV #81
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
71c3a74
feat: v2 migration Cloudflare KV
chris13524 7314ce4
fix: nightly build: https://github.com/time-rs/time/issues/681
chris13524 fec7ca9
fix: expiration
chris13524 1559bc5
fix: KV 404
chris13524 4fd6199
chore: handle errors reading and writing from Cloudflare KV
chris13524 5c63eb1
fix: bypass rate limits
chris13524 8e87e0a
chore: MigrationStore -> migration::Store
chris13524 a8a2c55
chore: if -> match
chris13524 9c3e5a0
Merge branch 'main' into feat/v2-migration-cf-kv
chris13524 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attestation_store
->http_server
dependency really stinks, but oh well it's going to be deprecated anyway 😄