Skip to content

Commit c913e53

Browse files
committed
chore: pull in new rustfmt
1 parent 14355bb commit c913e53

File tree

2 files changed

+107
-85
lines changed

2 files changed

+107
-85
lines changed

cloudflare/src/handle.rs

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,78 @@ lazy_static! {
2020
///
2121
/// The handler which handles requests on cloudflare
2222
///
23-
pub async fn fetch(req: worker::Request, env: worker::Env, _: worker::Context) -> anyhow::Result<worker::Response> {
24-
log::info!("{} {:?}", req.method().to_string(), req.url().map(|u| u.to_string()));
25-
let req = to_request(req).await?;
26-
let env = Rc::new(env);
27-
let app_ctx = match get_app_ctx(env, &req).await? {
28-
Ok(app_ctx) => app_ctx,
29-
Err(e) => return Ok(to_response(e).await?),
30-
};
31-
let resp = handle_request::<GraphQLRequest, CloudflareHttp, CloudflareEnv>(req, app_ctx).await?;
32-
Ok(to_response(resp).await?)
23+
pub async fn fetch(
24+
req: worker::Request,
25+
env: worker::Env,
26+
_: worker::Context,
27+
) -> anyhow::Result<worker::Response> {
28+
log::info!(
29+
"{} {:?}",
30+
req.method().to_string(),
31+
req.url().map(|u| u.to_string())
32+
);
33+
let req = to_request(req).await?;
34+
let env = Rc::new(env);
35+
let app_ctx = match get_app_ctx(env, &req).await? {
36+
Ok(app_ctx) => app_ctx,
37+
Err(e) => return Ok(to_response(e).await?),
38+
};
39+
let resp =
40+
handle_request::<GraphQLRequest, CloudflareHttp, CloudflareEnv>(req, app_ctx).await?;
41+
Ok(to_response(resp).await?)
3342
}
3443

3544
///
3645
/// Initializes the worker once and caches the app context
3746
/// for future requests.
3847
///
3948
async fn get_app_ctx(
40-
env: Rc<worker::Env>,
41-
req: &Request<Body>,
49+
env: Rc<worker::Env>,
50+
req: &Request<Body>,
4251
) -> anyhow::Result<Result<Arc<CloudFlareAppContext>, Response<Body>>> {
43-
// Read context from cache
44-
let file_path = req
45-
.uri()
46-
.query()
47-
.and_then(|x| serde_qs::from_str::<HashMap<String, String>>(x).ok())
48-
.and_then(|x| x.get("config").cloned());
52+
// Read context from cache
53+
let file_path = req
54+
.uri()
55+
.query()
56+
.and_then(|x| serde_qs::from_str::<HashMap<String, String>>(x).ok())
57+
.and_then(|x| x.get("config").cloned());
4958

50-
if let Some(file_path) = &file_path {
51-
if let Some(app_ctx) = read_app_ctx() {
52-
if app_ctx.0 == file_path.borrow() {
53-
log::info!("Using cached application context");
54-
return Ok(Ok(app_ctx.clone().1));
55-
}
59+
if let Some(file_path) = &file_path {
60+
if let Some(app_ctx) = read_app_ctx() {
61+
if app_ctx.0 == file_path.borrow() {
62+
log::info!("Using cached application context");
63+
return Ok(Ok(app_ctx.clone().1));
64+
}
65+
}
5666
}
57-
}
5867

59-
// Create new context
60-
let env_io = CloudflareEnv::init(env.clone());
61-
let bucket_id = env_io.get("BUCKET").ok_or(anyhow!("CONFIG var is not set"))?;
62-
log::debug!("R2 Bucket ID: {}", bucket_id);
68+
// Create new context
69+
let env_io = CloudflareEnv::init(env.clone());
70+
let bucket_id = env_io
71+
.get("BUCKET")
72+
.ok_or(anyhow!("CONFIG var is not set"))?;
73+
log::debug!("R2 Bucket ID: {}", bucket_id);
6374

64-
let file = init_file(env.clone(), bucket_id)?;
65-
let http = init_http();
66-
let cache = init_cache(env);
75+
let file = init_file(env.clone(), bucket_id)?;
76+
let http = init_http();
77+
let cache = init_cache(env);
6778

68-
match showcase_get_app_ctx::<GraphQLRequest, _, _, _>(req, (http, env_io, Some(file), Arc::new(cache))).await? {
69-
Ok(app_ctx) => {
70-
let app_ctx = Arc::new(app_ctx);
71-
if let Some(file_path) = file_path {
72-
*APP_CTX.write().unwrap() = Some((file_path, app_ctx.clone()));
73-
}
74-
log::info!("Initialized new application context");
75-
Ok(Ok(app_ctx))
79+
match showcase_get_app_ctx::<GraphQLRequest, _, _, _>(
80+
req,
81+
(http, env_io, Some(file), Arc::new(cache)),
82+
)
83+
.await?
84+
{
85+
Ok(app_ctx) => {
86+
let app_ctx = Arc::new(app_ctx);
87+
if let Some(file_path) = file_path {
88+
*APP_CTX.write().unwrap() = Some((file_path, app_ctx.clone()));
89+
}
90+
log::info!("Initialized new application context");
91+
Ok(Ok(app_ctx))
92+
}
93+
Err(e) => Ok(Err(e)),
7694
}
77-
Err(e) => Ok(Err(e)),
78-
}
7995
}
8096

8197
fn read_app_ctx() -> Option<(String, Arc<AppContext>)> {

src/http/showcase.rs

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ impl FileIO for DummyFileIO {
2727
pub struct DummyEnvIO;
2828

2929
impl EnvIO for DummyEnvIO {
30-
fn get(&self, _key: &str) -> Option<String> {
31-
None
32-
}
30+
fn get(&self, _key: &str) -> Option<String> {
31+
None
32+
}
3333
}
3434

3535
pub async fn showcase_get_app_ctx<T: DeserializeOwned + GraphQLRequestLike>(
@@ -39,49 +39,55 @@ pub async fn showcase_get_app_ctx<T: DeserializeOwned + GraphQLRequestLike>(
3939
let url = Url::parse(&req.uri().to_string())?;
4040
let mut query = url.query_pairs();
4141

42-
let config_url = if let Some(pair) = query.find(|x| x.0 == "config") {
43-
pair.1
44-
} else {
45-
let mut response = async_graphql::Response::default();
46-
let server_error = ServerError::new("No Config URL specified", None);
47-
response.errors = vec![server_error];
48-
return Ok(Err(GraphQLResponse::from(response).to_response()?));
49-
};
42+
let config_url = if let Some(pair) = query.find(|x| x.0 == "config") {
43+
pair.1
44+
} else {
45+
let mut response = async_graphql::Response::default();
46+
let server_error = ServerError::new("No Config URL specified", None);
47+
response.errors = vec![server_error];
48+
return Ok(Err(GraphQLResponse::from(response).to_response()?));
49+
};
5050

51-
let config = if let Some(file) = file {
52-
let reader = ConfigReader::init(file, http.clone());
53-
reader.read(&[config_url]).await
54-
} else {
55-
let reader = ConfigReader::init(DummyFileIO, http.clone());
56-
reader.read(&[config_url]).await
57-
};
51+
let config = if let Some(file) = file {
52+
let reader = ConfigReader::init(file, http.clone());
53+
reader.read(&[config_url]).await
54+
} else {
55+
let reader = ConfigReader::init(DummyFileIO, http.clone());
56+
reader.read(&[config_url]).await
57+
};
5858

59-
let config = match config {
60-
Ok(config) => config,
61-
Err(e) => {
62-
let mut response = async_graphql::Response::default();
63-
let server_error = if e.to_string() == "DummyFileIO" {
64-
ServerError::new("Invalid Config URL specified", None)
65-
} else {
66-
ServerError::new(format!("{}", e), None)
67-
};
68-
response.errors = vec![server_error];
69-
return Ok(Err(GraphQLResponse::from(response).to_response()?));
70-
}
71-
};
59+
let config = match config {
60+
Ok(config) => config,
61+
Err(e) => {
62+
let mut response = async_graphql::Response::default();
63+
let server_error = if e.to_string() == "DummyFileIO" {
64+
ServerError::new("Invalid Config URL specified", None)
65+
} else {
66+
ServerError::new(format!("{}", e), None)
67+
};
68+
response.errors = vec![server_error];
69+
return Ok(Err(GraphQLResponse::from(response).to_response()?));
70+
}
71+
};
7272

73-
let blueprint = match Blueprint::try_from(&config) {
74-
Ok(blueprint) => blueprint,
75-
Err(e) => {
76-
let mut response = async_graphql::Response::default();
77-
let server_error = ServerError::new(format!("{}", e), None);
78-
response.errors = vec![server_error];
79-
return Ok(Err(GraphQLResponse::from(response).to_response()?));
80-
}
81-
};
73+
let blueprint = match Blueprint::try_from(&config) {
74+
Ok(blueprint) => blueprint,
75+
Err(e) => {
76+
let mut response = async_graphql::Response::default();
77+
let server_error = ServerError::new(format!("{}", e), None);
78+
response.errors = vec![server_error];
79+
return Ok(Err(GraphQLResponse::from(response).to_response()?));
80+
}
81+
};
8282

83-
let http = Arc::new(http);
84-
let env = Arc::new(env);
83+
let http = Arc::new(http);
84+
let env = Arc::new(env);
8585

86-
Ok(Ok(AppContext::new(blueprint, http.clone(), http, env, cache)))
86+
Ok(Ok(AppContext::new(
87+
blueprint,
88+
http.clone(),
89+
http,
90+
env,
91+
cache,
92+
)))
8793
}

0 commit comments

Comments
 (0)