split files
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-11 22:45:44 +02:00
parent 097cf018fc
commit 3f4657b0cf
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 55 additions and 45 deletions

View file

@ -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
View 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
}

View file

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