Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions examples/cron/api/cron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,31 @@ impl<T: SlackClientHttpConnector + Send + Sync> Lambda<'_, T> {
}

pub async fn handler(&self, req: Request) -> Result<Response<Body>, Error> {
let parsed_url = Url::parse(&req.uri().to_string()).unwrap();
let hash_query: HashMap<String, String> = parsed_url.query_pairs().into_owned().collect();
let secret = hash_query.get("secret").map(|x| &**x);

// https://vercel.com/docs/cron-jobs#how-to-secure-cron-jobs
if secret != Some("geheim") {
return Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(().into())?);
}
let token_value = std::env::var("CRON_SECRET")?;
let headers = req.headers();

match headers.get("authorization") {
None => {
return Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(().into())?);
}
Some(authorization_header) => {
let authorization_string = authorization_header.to_str().unwrap();

let message = SlackMessage {};
if authorization_string != format!("Bearer {}", token_value) {
return Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(().into())?);
}

self.post_message(&message, "#general").await?;
let message = SlackMessage {};

Ok(Response::builder().status(StatusCode::OK).body(().into())?)
self.post_message(&message, "#general").await?;

Ok(Response::builder().status(StatusCode::OK).body(().into())?)
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/cron/vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
},
"crons": [
{
"path": "/api/cron?secret=geheim",
"schedule": "30 9 * * *"
"path": "/api/cron",
"schedule": "0 10 * * *"
}
]
}