From a7aec1b94e6d203b29d0d9387d06594fab4ddbf5 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 29 Aug 2024 23:31:35 +0200 Subject: [PATCH] add countdown --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 5 ++++- src/routes/api_v1.rs | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a8c81f..5aaf798 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -865,6 +865,12 @@ dependencies = [ "syn 2.0.72", ] +[[package]] +name = "cyborgtime" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "817fa642fb0ee7fe42e95783e00e0969927b96091bdd4b9b1af082acd943913b" + [[package]] name = "darling" version = "0.20.10" @@ -1069,6 +1075,7 @@ dependencies = [ "chrono", "chrono-tz", "comrak", + "cyborgtime", "glob", "lol_html", "mime_guess", diff --git a/Cargo.toml b/Cargo.toml index 62892aa..f838950 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ base64 = "0.22" mime_guess = "2.0" urlencoding = "2.1" regex = "1.10" +cyborgtime = "2.1.1" [lints.clippy] pedantic = "warn" diff --git a/src/main.rs b/src/main.rs index e62d48d..342d117 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,10 @@ async fn main() -> Result<()> { .add(("Server", format!("ewp/{}", env!("CARGO_PKG_VERSION")))) .add(("Permissions-Policy", "interest-cohort=()")), ) - .service(web::scope("/api").service(web::scope("v1").service(api_v1::love))) + .service( + web::scope("/api") + .service(web::scope("v1").service(api_v1::love).service(api_v1::btf)), + ) .service(index::page) .service(agreements::security) .service(agreements::humans) diff --git a/src/routes/api_v1.rs b/src/routes/api_v1.rs index 60daaeb..22eeeac 100644 --- a/src/routes/api_v1.rs +++ b/src/routes/api_v1.rs @@ -1,15 +1,45 @@ +use std::time::Duration; + use actix_web::{get, HttpResponse, Responder}; +use chrono::Utc; +use cyborgtime::format_duration; use serde::Serialize; -/// Response +/// Response for /love #[derive(Serialize)] -struct Info { +struct InfoLove { unix_epoch: u32, } #[get("/love")] pub async fn love() -> impl Responder { - HttpResponse::Ok().json(Info { + HttpResponse::Ok().json(InfoLove { unix_epoch: 1_605_576_600, }) } + +/// Response for /backtofrance +#[derive(Serialize)] +struct InfoBTF { + unix_epoch: u64, + countdown: String, +} + +#[get("/backtofrance")] +pub async fn btf() -> impl Responder { + let target = 1_736_618_100; + let current_time: u64 = Utc::now().timestamp().try_into().unwrap(); + + let info = InfoBTF { + unix_epoch: target, + countdown: if current_time > target { + "Already happened".to_owned() + } else { + let duration_epoch = target - current_time; + let duration = Duration::from_secs(duration_epoch); + format_duration(duration).to_string() + }, + }; + + HttpResponse::Ok().json(info) +}