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 template;
|
||||
mod utils;
|
||||
|
||||
#[path = "misc/github.rs"]
|
||||
mod github;
|
||||
|
||||
#[path = "routes/agreements.rs"]
|
||||
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 cached::proc_macro::once;
|
||||
use ramhorns::Content;
|
||||
use reqwest::header::ACCEPT;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[get("/contrib")]
|
||||
pub async fn page(config: web::Data<Config>) -> 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<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> {
|
||||
#[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::<GithubResponse>()
|
||||
.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()),
|
||||
},
|
||||
))
|
||||
)
|
||||
}
|
||||
|
|
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