* move api logic into separate files

* move routes logic into own directory
* api: add some date in human readable format
This commit is contained in:
Mylloon 2025-01-10 19:14:51 +01:00
parent 09a1bb6603
commit a624761182
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
17 changed files with 110 additions and 67 deletions

1
src/logic/api/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod v1;

View file

@ -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()
},
}
}
}

33
src/logic/api/v1/love.rs Normal file
View file

@ -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()
},
}
}

3
src/logic/api/v1/mod.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod backtofrance;
pub mod love;
pub mod websites;

View file

@ -0,0 +1,6 @@
pub fn tuple() -> (&'static str, &'static str) {
(
"http://www.bocal.cs.univ-paris8.fr/~akennel/",
"https://anri.up8.site/",
)
}

View file

@ -1,3 +1,4 @@
pub mod api;
pub mod blog;
pub mod contact;
pub mod contrib;

View file

@ -13,6 +13,7 @@ use crate::routes::{
mod config;
mod template;
mod logic;
mod routes;
mod utils;

View file

@ -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())
}

View file

@ -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},
},
};

View file

@ -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},
},
};

View file

@ -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;

View file

@ -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},
},
};

View file

@ -3,4 +3,3 @@ pub mod github;
pub mod markdown;
pub mod metadata;
pub mod misc;
pub mod routes;