diff --git a/src/main.rs b/src/main.rs index 036b697..2ee2348 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,10 @@ use actix_web::{middleware::DefaultHeaders, web, App, HttpServer}; mod config; mod template; +mod utils; + +#[path = "misc/github.rs"] +mod github; #[path = "routes/agreements.rs"] mod agreements; diff --git a/src/misc/github.rs b/src/misc/github.rs new file mode 100644 index 0000000..81f78a7 --- /dev/null +++ b/src/misc/github.rs @@ -0,0 +1,29 @@ +use reqwest::{header::ACCEPT, Error}; +use serde::Deserialize; + +use crate::utils::get_reqwest_client; + +#[derive(Deserialize)] +pub struct GithubResponse { + pub total_count: u32, + pub items: Vec, +} + +#[derive(Deserialize)] +pub struct GithubProject { + pub title: String, + pub html_url: String, + pub number: u32, +} + +pub async fn fetch_pr() -> Result { + let client = get_reqwest_client(); + + client + .get("https://api.github.com/search/issues?q=is:pr%20author:Mylloon") + .header(ACCEPT, "application/vnd.github.text-match+json") + .send() + .await? + .json::() + .await +} diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index d9d9124..a9e9a2a 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -1,65 +1,32 @@ -use crate::{config::Config, template::Infos}; +use crate::{config::Config, github::fetch_pr, template::Infos}; use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; -use reqwest::header::ACCEPT; -use serde::Deserialize; #[get("/contrib")] pub async fn page(config: web::Data) -> impl Responder { - let page = get_page(config.get_ref().clone()).await; - - match page { - Ok(html) => HttpResponse::Ok().body(html), - Err(e) => HttpResponse::InternalServerError().body(e.to_string()), - } + HttpResponse::Ok().body(get_page(config.get_ref().clone()).await) } #[derive(Content)] struct PortfolioTemplate {} -#[derive(Deserialize)] -struct GithubResponse { - total_count: u32, - items: Vec, -} - -#[derive(Deserialize)] -struct GithubProject { - title: String, - html_url: String, - number: u32, -} - -// #[once(time = 60)] -pub async fn get_page(config: Config) -> Result { +#[once(time = 60)] +pub async fn get_page(config: Config) -> std::string::String { // Fetch latest data from github - let client = reqwest::Client::builder() - .user_agent(format!("EWP/{}", env!("CARGO_PKG_VERSION"))) - .build()?; + if let Ok(resp) = fetch_pr().await { + println!("Nb: {}", resp.total_count); + resp.items + .iter() + .for_each(|x| println!("[{0} #{2}]({1})", x.title, x.html_url, x.number)); + }; - let author = "Mylloon"; - let resp = client - .get(format!( - "https://api.github.com/search/issues?q=is:pr%20author:{author}" - )) - .header(ACCEPT, "application/vnd.github.text-match+json") - .send() - .await? - .json::() - .await?; - - println!("Nb: {}", resp.total_count); - resp.items - .iter() - .for_each(|x| println!("[{0} #{2}]({1})", x.title, x.html_url, x.number)); - - Ok(config.tmpl.render( + config.tmpl.render( "contrib.html", PortfolioTemplate {}, Infos { page_title: Some("Mes contributions".to_string()), page_desc: Some("Contributions à l'opensource par Anri".to_string()), }, - )) + ) } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..b740952 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,10 @@ +use cached::proc_macro::cached; +use reqwest::Client; + +#[cached] +pub fn get_reqwest_client() -> Client { + Client::builder() + .user_agent(format!("EWP/{}", env!("CARGO_PKG_VERSION"))) + .build() + .unwrap() +}