Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
feat: set TIMEOUT as env var, default to 10 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
tippfehlr committed Feb 26, 2024
1 parent 57567b9 commit f3937e4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use chrono::{DateTime, Duration, Local};

struct State {
currently_updating: Arc<Mutex<HashMap<String, DateTime<Local>>>>,
timeout: Duration,
}

async fn webhook(path: web::Path<(String, String)>, state: web::Data<State>) -> impl Responder {
Expand Down Expand Up @@ -38,7 +39,7 @@ async fn webhook(path: web::Path<(String, String)>, state: web::Data<State>) ->
}
};

if Local::now().signed_duration_since(*last_update) < Duration::seconds(10) {
if Local::now().signed_duration_since(*last_update) < state.timeout {
eprintln!(
"Last update was {} seconds ago, skipping update",
Local::now()
Expand Down Expand Up @@ -138,13 +139,20 @@ async fn webhook(path: web::Path<(String, String)>, state: web::Data<State>) ->
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let currently_updating = Arc::new(Mutex::new(HashMap::<String, DateTime<Local>>::new()));
let timeout = Duration::seconds(
std::env::var("TIMEOUT")
.unwrap_or(10.to_string())
.parse::<i64>()
.unwrap_or(10),
);

HttpServer::new(move || {
App::new()
.route("/{project}/{container}", web::get().to(webhook))
.route("/{project}/{container}", web::post().to(webhook))
.app_data(web::Data::new(State {
currently_updating: currently_updating.clone(),
timeout,
}))
})
.bind(("0.0.0.0", 8537))?
Expand Down

0 comments on commit f3937e4

Please sign in to comment.