This commit is contained in:
parent
097cf018fc
commit
3f4657b0cf
4 changed files with 55 additions and 45 deletions
|
@ -3,6 +3,10 @@ use actix_web::{middleware::DefaultHeaders, web, App, HttpServer};
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod template;
|
mod template;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
#[path = "misc/github.rs"]
|
||||||
|
mod github;
|
||||||
|
|
||||||
#[path = "routes/agreements.rs"]
|
#[path = "routes/agreements.rs"]
|
||||||
mod agreements;
|
mod agreements;
|
||||||
|
|
29
src/misc/github.rs
Normal file
29
src/misc/github.rs
Normal file
|
@ -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<GithubProject>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct GithubProject {
|
||||||
|
pub title: String,
|
||||||
|
pub html_url: String,
|
||||||
|
pub number: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_pr() -> Result<GithubResponse, Error> {
|
||||||
|
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::<GithubResponse>()
|
||||||
|
.await
|
||||||
|
}
|
|
@ -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 actix_web::{get, web, HttpResponse, Responder};
|
||||||
use cached::proc_macro::once;
|
use cached::proc_macro::once;
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
use reqwest::header::ACCEPT;
|
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
#[get("/contrib")]
|
#[get("/contrib")]
|
||||||
pub async fn page(config: web::Data<Config>) -> impl Responder {
|
pub async fn page(config: web::Data<Config>) -> impl Responder {
|
||||||
let page = get_page(config.get_ref().clone()).await;
|
HttpResponse::Ok().body(get_page(config.get_ref().clone()).await)
|
||||||
|
|
||||||
match page {
|
|
||||||
Ok(html) => HttpResponse::Ok().body(html),
|
|
||||||
Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Content)]
|
#[derive(Content)]
|
||||||
struct PortfolioTemplate {}
|
struct PortfolioTemplate {}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[once(time = 60)]
|
||||||
struct GithubResponse {
|
pub async fn get_page(config: Config) -> std::string::String {
|
||||||
total_count: u32,
|
|
||||||
items: Vec<GithubProject>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct GithubProject {
|
|
||||||
title: String,
|
|
||||||
html_url: String,
|
|
||||||
number: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
// #[once(time = 60)]
|
|
||||||
pub async fn get_page(config: Config) -> Result<std::string::String, reqwest::Error> {
|
|
||||||
// Fetch latest data from github
|
// Fetch latest data from github
|
||||||
let client = reqwest::Client::builder()
|
if let Ok(resp) = fetch_pr().await {
|
||||||
.user_agent(format!("EWP/{}", env!("CARGO_PKG_VERSION")))
|
|
||||||
.build()?;
|
|
||||||
|
|
||||||
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::<GithubResponse>()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
println!("Nb: {}", resp.total_count);
|
println!("Nb: {}", resp.total_count);
|
||||||
resp.items
|
resp.items
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|x| println!("[{0} #{2}]({1})", x.title, x.html_url, x.number));
|
.for_each(|x| println!("[{0} #{2}]({1})", x.title, x.html_url, x.number));
|
||||||
|
};
|
||||||
|
|
||||||
Ok(config.tmpl.render(
|
config.tmpl.render(
|
||||||
"contrib.html",
|
"contrib.html",
|
||||||
PortfolioTemplate {},
|
PortfolioTemplate {},
|
||||||
Infos {
|
Infos {
|
||||||
page_title: Some("Mes contributions".to_string()),
|
page_title: Some("Mes contributions".to_string()),
|
||||||
page_desc: Some("Contributions à l'opensource par Anri".to_string()),
|
page_desc: Some("Contributions à l'opensource par Anri".to_string()),
|
||||||
},
|
},
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
10
src/utils.rs
Normal file
10
src/utils.rs
Normal file
|
@ -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()
|
||||||
|
}
|
Loading…
Reference in a new issue