forked from cloudflare/workers-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Duplicate test harness using
axum
router (cloudflare#481)
* Prepare to remove global Fetch for http * clippy * Refactor worker test harness * Axum test harness * Get more tests working * Macro for marking future as Send * Remaining axum routes * More documentation * Cleanup
- Loading branch information
1 parent
81e9179
commit 3454cf6
Showing
30 changed files
with
1,751 additions
and
852 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
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,26 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, ItemFn}; | ||
|
||
pub fn expand_macro(_attr: TokenStream, stream: TokenStream) -> TokenStream { | ||
let stream_clone = stream.clone(); | ||
let input = parse_macro_input!(stream_clone as ItemFn); | ||
|
||
let ItemFn { | ||
attrs, | ||
vis, | ||
sig, | ||
block, | ||
} = input; | ||
let stmts = &block.stmts; | ||
|
||
let tokens = quote! { | ||
#(#attrs)* #vis #sig { | ||
worker::send::SendFuture::new(async { | ||
#(#stmts)* | ||
}).await | ||
} | ||
}; | ||
|
||
TokenStream::from(tokens) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use super::SomeSharedData; | ||
use futures_util::stream::StreamExt; | ||
use rand::Rng; | ||
use std::time::Duration; | ||
use worker::{console_log, Cache, Date, Delay, Env, Request, Response, Result}; | ||
|
||
fn key(req: &Request) -> Result<Option<String>> { | ||
let uri = req.url()?; | ||
let mut segments = uri.path_segments().unwrap(); | ||
Ok(segments.nth(2).map(|s| s.to_owned())) | ||
} | ||
|
||
#[worker::send] | ||
pub async fn handle_cache_example( | ||
req: Request, | ||
_env: Env, | ||
_data: SomeSharedData, | ||
) -> Result<Response> { | ||
console_log!("url: {}", req.url()?.to_string()); | ||
let cache = Cache::default(); | ||
let key = req.url()?.to_string(); | ||
if let Some(resp) = cache.get(&key, true).await? { | ||
console_log!("Cache HIT!"); | ||
Ok(resp) | ||
} else { | ||
console_log!("Cache MISS!"); | ||
let mut resp = | ||
Response::from_json(&serde_json::json!({ "timestamp": Date::now().as_millis() }))?; | ||
|
||
// Cache API respects Cache-Control headers. Setting s-max-age to 10 | ||
// will limit the response to be in cache for 10 seconds max | ||
resp.headers_mut().set("cache-control", "s-maxage=10")?; | ||
cache.put(key, resp.cloned()?).await?; | ||
Ok(resp) | ||
} | ||
} | ||
|
||
#[worker::send] | ||
pub async fn handle_cache_api_get( | ||
req: Request, | ||
_env: Env, | ||
_data: SomeSharedData, | ||
) -> Result<Response> { | ||
if let Some(key) = key(&req)? { | ||
let cache = Cache::default(); | ||
if let Some(resp) = cache.get(format!("https://{key}"), true).await? { | ||
return Ok(resp); | ||
} else { | ||
return Response::ok("cache miss"); | ||
} | ||
} | ||
Response::error("key missing", 400) | ||
} | ||
|
||
#[worker::send] | ||
pub async fn handle_cache_api_put( | ||
req: Request, | ||
_env: Env, | ||
_data: SomeSharedData, | ||
) -> Result<Response> { | ||
if let Some(key) = key(&req)? { | ||
let cache = Cache::default(); | ||
|
||
let mut resp = | ||
Response::from_json(&serde_json::json!({ "timestamp": Date::now().as_millis() }))?; | ||
|
||
// Cache API respects Cache-Control headers. Setting s-max-age to 10 | ||
// will limit the response to be in cache for 10 seconds max | ||
resp.headers_mut().set("cache-control", "s-maxage=10")?; | ||
cache.put(format!("https://{key}"), resp.cloned()?).await?; | ||
return Ok(resp); | ||
} | ||
Response::error("key missing", 400) | ||
} | ||
|
||
#[worker::send] | ||
pub async fn handle_cache_api_delete( | ||
req: Request, | ||
_env: Env, | ||
_data: SomeSharedData, | ||
) -> Result<Response> { | ||
if let Some(key) = key(&req)? { | ||
let cache = Cache::default(); | ||
|
||
let res = cache.delete(format!("https://{key}"), true).await?; | ||
return Response::ok(serde_json::to_string(&res)?); | ||
} | ||
Response::error("key missing", 400) | ||
} | ||
|
||
#[worker::send] | ||
pub async fn handle_cache_stream( | ||
req: Request, | ||
_env: Env, | ||
_data: SomeSharedData, | ||
) -> Result<Response> { | ||
console_log!("url: {}", req.url()?.to_string()); | ||
let cache = Cache::default(); | ||
let key = req.url()?.to_string(); | ||
if let Some(resp) = cache.get(&key, true).await? { | ||
console_log!("Cache HIT!"); | ||
Ok(resp) | ||
} else { | ||
console_log!("Cache MISS!"); | ||
let mut rng = rand::thread_rng(); | ||
let count = rng.gen_range(0..10); | ||
let stream = futures_util::stream::repeat("Hello, world!\n") | ||
.take(count) | ||
.then(|text| async move { | ||
Delay::from(Duration::from_millis(50)).await; | ||
Result::Ok(text.as_bytes().to_vec()) | ||
}); | ||
|
||
let mut resp = Response::from_stream(stream)?; | ||
console_log!("resp = {:?}", resp); | ||
// Cache API respects Cache-Control headers. Setting s-max-age to 10 | ||
// will limit the response to be in cache for 10 seconds max | ||
resp.headers_mut().set("cache-control", "s-maxage=10")?; | ||
|
||
cache.put(key, resp.cloned()?).await?; | ||
Ok(resp) | ||
} | ||
} |
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
Oops, something went wrong.