Skip to content

Commit dd7a6f5

Browse files
authored
update cron example (#97)
1 parent cf9b326 commit dd7a6f5

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

examples/cron/api/cron.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,31 @@ impl<T: SlackClientHttpConnector + Send + Sync> Lambda<'_, T> {
3131
}
3232

3333
pub async fn handler(&self, req: Request) -> Result<Response<Body>, Error> {
34-
let parsed_url = Url::parse(&req.uri().to_string()).unwrap();
35-
let hash_query: HashMap<String, String> = parsed_url.query_pairs().into_owned().collect();
36-
let secret = hash_query.get("secret").map(|x| &**x);
37-
38-
// https://vercel.com/docs/cron-jobs#how-to-secure-cron-jobs
39-
if secret != Some("geheim") {
40-
return Ok(Response::builder()
41-
.status(StatusCode::NOT_FOUND)
42-
.body(().into())?);
43-
}
34+
let token_value = std::env::var("CRON_SECRET")?;
35+
let headers = req.headers();
36+
37+
match headers.get("authorization") {
38+
None => {
39+
return Ok(Response::builder()
40+
.status(StatusCode::NOT_FOUND)
41+
.body(().into())?);
42+
}
43+
Some(authorization_header) => {
44+
let authorization_string = authorization_header.to_str().unwrap();
4445

45-
let message = SlackMessage {};
46+
if authorization_string != format!("Bearer {}", token_value) {
47+
return Ok(Response::builder()
48+
.status(StatusCode::NOT_FOUND)
49+
.body(().into())?);
50+
}
4651

47-
self.post_message(&message, "#general").await?;
52+
let message = SlackMessage {};
4853

49-
Ok(Response::builder().status(StatusCode::OK).body(().into())?)
54+
self.post_message(&message, "#general").await?;
55+
56+
Ok(Response::builder().status(StatusCode::OK).body(().into())?)
57+
}
58+
}
5059
}
5160
}
5261

examples/cron/vercel.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
},
88
"crons": [
99
{
10-
"path": "/api/cron?secret=geheim",
11-
"schedule": "30 9 * * *"
10+
"path": "/api/cron",
11+
"schedule": "0 10 * * *"
1212
}
1313
]
1414
}

0 commit comments

Comments
 (0)