1use axum::extract::{Path, Request, State};
2use axum::http;
3use axum::{
4 middleware,
5 response::{self, IntoResponse},
6};
7use std::time;
8
9#[derive(Clone)]
10pub struct Unlock {
11 start: time::Instant,
12 interval: time::Duration,
13}
14
15impl Unlock {
16 pub fn new(start: time::Instant, interval: time::Duration) -> Self {
17 Self { start, interval }
18 }
19}
20
21pub async fn unlock(
22 Path(day): Path<u32>,
23 State(unlocker): State<Unlock>,
24 request: Request,
25 next: middleware::Next,
26) -> response::Response {
27 let deadline = unlocker.start + unlocker.interval * day;
28 let now = time::Instant::now();
29 if now >= deadline {
30 return next.run(request).await;
31 }
32
33 let time_remaining = deadline.saturating_duration_since(now);
34 let error_response = axum::Json(serde_json::json!({
35 "error": "Route Locked",
36 "time_remaining_seconds": time_remaining.as_secs(),
37 }));
38
39 (http::StatusCode::FORBIDDEN, error_response).into_response()
40}