diff --git a/src/logic/api/mod.rs b/src/logic/api/mod.rs new file mode 100644 index 0000000..a3a6d96 --- /dev/null +++ b/src/logic/api/mod.rs @@ -0,0 +1 @@ +pub mod v1; diff --git a/src/logic/api/v1/backtofrance.rs b/src/logic/api/v1/backtofrance.rs new file mode 100644 index 0000000..14928e2 --- /dev/null +++ b/src/logic/api/v1/backtofrance.rs @@ -0,0 +1,56 @@ +use std::time::Duration; + +use chrono::{DateTime, Utc}; +use chrono_tz::Europe; +use cyborgtime::format_duration; +use serde::Serialize; + +#[derive(Serialize)] +pub struct Info { + unix_epoch: i64, + departure: String, + arrival: String, + countdown: String, + since: String, +} + +pub fn json() -> Info { + let target = 1_736_618_100; + let start = 1_724_832_000; + let current_time = Utc::now().timestamp(); + + let departure = DateTime::from_timestamp(start, 0) + .unwrap() + .with_timezone(&Europe::Paris) + .to_rfc2822(); + let arrival = DateTime::from_timestamp(target, 0) + .unwrap() + .with_timezone(&Europe::Paris) + .to_rfc2822(); + + if current_time > target { + Info { + unix_epoch: target, + departure, + arrival, + countdown: "Already happened".to_owned(), + since: "Not relevant anymore".to_owned(), + } + } else { + Info { + unix_epoch: target, + departure, + arrival, + countdown: { + let duration_epoch = target - current_time; + let duration = Duration::from_secs(duration_epoch.try_into().unwrap()); + format_duration(duration).to_string() + }, + since: { + let duration_epoch = current_time - start; + let duration = Duration::from_secs(duration_epoch.try_into().unwrap()); + format_duration(duration).to_string() + }, + } + } +} diff --git a/src/logic/api/v1/love.rs b/src/logic/api/v1/love.rs new file mode 100644 index 0000000..1c2ee3b --- /dev/null +++ b/src/logic/api/v1/love.rs @@ -0,0 +1,33 @@ +use std::time::Duration; + +use chrono::{DateTime, Utc}; +use chrono_tz::Europe; +use cyborgtime::format_duration; +use serde::Serialize; + +#[derive(Serialize)] +pub struct Info { + unix_epoch: i64, + date: String, + since: String, +} + +pub fn json() -> Info { + let target = 1_605_576_600; + let current_time = Utc::now().timestamp(); + + let date = DateTime::from_timestamp(target, 0) + .unwrap() + .with_timezone(&Europe::Paris) + .to_rfc2822(); + + Info { + unix_epoch: target, + date, + since: { + let duration_epoch = current_time - target; + let duration = Duration::from_secs(duration_epoch.try_into().unwrap()); + format_duration(duration).to_string() + }, + } +} diff --git a/src/logic/api/v1/mod.rs b/src/logic/api/v1/mod.rs new file mode 100644 index 0000000..37297f4 --- /dev/null +++ b/src/logic/api/v1/mod.rs @@ -0,0 +1,3 @@ +pub mod backtofrance; +pub mod love; +pub mod websites; diff --git a/src/logic/api/v1/websites.rs b/src/logic/api/v1/websites.rs new file mode 100644 index 0000000..9d24e36 --- /dev/null +++ b/src/logic/api/v1/websites.rs @@ -0,0 +1,6 @@ +pub fn tuple() -> (&'static str, &'static str) { + ( + "http://www.bocal.cs.univ-paris8.fr/~akennel/", + "https://anri.up8.site/", + ) +} diff --git a/src/utils/routes/blog.rs b/src/logic/blog.rs similarity index 100% rename from src/utils/routes/blog.rs rename to src/logic/blog.rs diff --git a/src/utils/routes/contact.rs b/src/logic/contact.rs similarity index 100% rename from src/utils/routes/contact.rs rename to src/logic/contact.rs diff --git a/src/utils/routes/contrib.rs b/src/logic/contrib.rs similarity index 100% rename from src/utils/routes/contrib.rs rename to src/logic/contrib.rs diff --git a/src/utils/routes/cours.rs b/src/logic/cours.rs similarity index 100% rename from src/utils/routes/cours.rs rename to src/logic/cours.rs diff --git a/src/utils/routes/mod.rs b/src/logic/mod.rs similarity index 82% rename from src/utils/routes/mod.rs rename to src/logic/mod.rs index e09082d..37dbe8f 100644 --- a/src/utils/routes/mod.rs +++ b/src/logic/mod.rs @@ -1,3 +1,4 @@ +pub mod api; pub mod blog; pub mod contact; pub mod contrib; diff --git a/src/main.rs b/src/main.rs index a3ef54e..d201bcd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use crate::routes::{ mod config; mod template; +mod logic; mod routes; mod utils; diff --git a/src/routes/api_v1.rs b/src/routes/api_v1.rs index aa90722..f9dcfb0 100644 --- a/src/routes/api_v1.rs +++ b/src/routes/api_v1.rs @@ -1,73 +1,18 @@ -use std::time::Duration; - use actix_web::{get, HttpResponse, Responder}; -use chrono::Utc; -use cyborgtime::format_duration; -use serde::Serialize; -/// Response for /love -#[derive(Serialize)] -struct InfoLove { - unix_epoch: u64, - since: String, -} +use crate::logic::api::v1; #[get("/love")] pub async fn love() -> impl Responder { - let target = 1_605_576_600; - let current_time: u64 = Utc::now().timestamp().try_into().unwrap(); - - HttpResponse::Ok().json(InfoLove { - unix_epoch: target, - since: { - let duration_epoch = current_time - target; - let duration = Duration::from_secs(duration_epoch); - format_duration(duration).to_string() - }, - }) -} - -/// Response for /backtofrance -#[derive(Serialize)] -struct InfoBTF { - unix_epoch: u64, - countdown: String, - since: String, + HttpResponse::Ok().json(v1::love::json()) } #[get("/backtofrance")] pub async fn btf() -> impl Responder { - let target = 1_736_618_100; - let start = 1_724_832_000; - let current_time: u64 = Utc::now().timestamp().try_into().unwrap(); - - if current_time > target { - HttpResponse::Ok().json(InfoBTF { - unix_epoch: target, - countdown: "Already happened".to_owned(), - since: "Not relevant anymore".to_owned(), - }) - } else { - HttpResponse::Ok().json(InfoBTF { - unix_epoch: target, - countdown: { - let duration_epoch = target - current_time; - let duration = Duration::from_secs(duration_epoch); - format_duration(duration).to_string() - }, - since: { - let duration_epoch = current_time - start; - let duration = Duration::from_secs(duration_epoch); - format_duration(duration).to_string() - }, - }) - } + HttpResponse::Ok().json(v1::backtofrance::json()) } #[get("/websites")] pub async fn websites() -> impl Responder { - HttpResponse::Ok().json(( - "http://www.bocal.cs.univ-paris8.fr/~akennel/", - "https://anri.up8.site/", - )) + HttpResponse::Ok().json(v1::websites::tuple()) } diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 0d58799..8a60526 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -6,12 +6,12 @@ use ramhorns::Content; use crate::{ config::Config, + logic::blog::{build_rss, get_post, get_posts, Post, BLOG_DIR, MIME_TYPE_RSS, POST_DIR}, template::{InfosPage, NavBar}, utils::{ markdown::{File, FilePath}, metadata::MType, misc::{lang, make_kw, read_file_fallback, Html, Lang}, - routes::blog::{build_rss, get_post, get_posts, Post, BLOG_DIR, MIME_TYPE_RSS, POST_DIR}, }, }; diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 0fca8b1..bbf11db 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -4,12 +4,12 @@ use ramhorns::Content; use crate::{ config::Config, + logic::contact::{find_links, read, remove_paragraphs}, template::{InfosPage, NavBar}, utils::{ markdown::{File, FilePath}, metadata::MType, misc::{lang, make_kw, read_file_fallback, Html, Lang}, - routes::contact::{find_links, read, remove_paragraphs}, }, }; diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index d7b03ff..1e9d0dc 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -1,10 +1,8 @@ use crate::{ config::Config, + logic::contrib::{fetch, Project}, template::{InfosPage, NavBar}, - utils::{ - misc::{make_kw, Html}, - routes::contrib::{fetch, Project}, - }, + utils::misc::{make_kw, Html}, }; use actix_web::{get, web, Responder}; use cached::proc_macro::once; diff --git a/src/routes/cours.rs b/src/routes/cours.rs index 6c64bdc..d5200d6 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -6,12 +6,12 @@ use serde::Deserialize; use crate::{ config::Config, + logic::cours::{excluded, get_filetree}, template::{InfosPage, NavBar}, utils::{ markdown::{File, FilePath}, metadata::MType, misc::{make_kw, read_file, Html}, - routes::cours::{excluded, get_filetree}, }, }; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index a3d0fb8..edaeb0a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,4 +3,3 @@ pub mod github; pub mod markdown; pub mod metadata; pub mod misc; -pub mod routes;