-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
105 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::extract::{Query, Request, State}; | ||
use axum::Json; | ||
|
||
use crate::db_client::{RepoFilter, RepoTotals}; | ||
use crate::helpers::get_filtered_repos; | ||
use crate::types::JsonRes; | ||
use crate::AppState; | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
pub struct ReposList { | ||
total_count: i32, | ||
total_stars: i32, | ||
total_forks: i32, | ||
total_views: i32, | ||
total_clones: i32, | ||
items: Vec<RepoTotals>, | ||
} | ||
|
||
pub async fn api_get_repos(State(state): State<Arc<AppState>>, req: Request) -> JsonRes<ReposList> { | ||
let db = &state.db; | ||
let qs: Query<RepoFilter> = Query::try_from_uri(req.uri())?; | ||
let repos = get_filtered_repos(&db, &qs).await?; | ||
|
||
let repos_list = ReposList { | ||
total_count: repos.len() as i32, | ||
total_stars: repos.iter().map(|r| r.stars).sum(), | ||
total_forks: repos.iter().map(|r| r.forks).sum(), | ||
total_views: repos.iter().map(|r| r.views_count).sum(), | ||
total_clones: repos.iter().map(|r| r.clones_count).sum(), | ||
items: repos, | ||
}; | ||
|
||
Ok(Json(repos_list)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ use thousands::Separable; | |
use crate::db_client::{ | ||
DbClient, Direction, PopularFilter, PopularKind, PopularSort, RepoFilter, RepoSort, RepoTotals, | ||
}; | ||
use crate::helpers::is_repo_included; | ||
use crate::helpers::{get_filtered_repos, is_repo_included}; | ||
use crate::types::{AppError, HtmlRes}; | ||
use crate::AppState; | ||
|
||
|
@@ -19,10 +19,7 @@ struct TablePopularItem { | |
} | ||
|
||
fn get_hx_target(req: &Request) -> Option<&str> { | ||
match req.headers().get("hx-target") { | ||
Some(x) => Some(x.to_str().unwrap_or_default()), | ||
None => None, | ||
} | ||
crate::helpers::get_header(req, "hx-target") | ||
} | ||
|
||
fn maybe_url(item: &(String, Option<String>)) -> Markup { | ||
|
@@ -68,7 +65,7 @@ fn base(state: &Arc<AppState>, navs: Vec<(String, Option<String>)>, inner: Marku | |
_ => &format!("{} · {}", navs.last().unwrap().0, app_name), | ||
}; | ||
|
||
let favicon = include_str!("../assets/favicon.svg") | ||
let favicon = include_str!("../../assets/favicon.svg") | ||
.replace("\n", "") | ||
.replace("\"", "%22") | ||
.replace("#", "%23"); | ||
|
@@ -87,7 +84,7 @@ fn base(state: &Arc<AppState>, navs: Vec<(String, Option<String>)>, inner: Marku | |
script src="https://unpkg.com/[email protected]" {} | ||
script src="https://unpkg.com/[email protected]" {} | ||
script src="https://unpkg.com/[email protected]" {} | ||
style { (PreEscaped(include_str!("../assets/app.css"))) } | ||
style { (PreEscaped(include_str!("../../assets/app.css"))) } | ||
} | ||
body { | ||
main class="container-fluid pt-0 main-box" { | ||
|
@@ -327,7 +324,7 @@ pub async fn repo_page( | |
} | ||
} | ||
|
||
script { (PreEscaped(include_str!("../assets/app.js"))) } | ||
script { (PreEscaped(include_str!("../../assets/app.js"))) } | ||
script { | ||
"const Metrics = "(PreEscaped(serde_json::to_string(&metrics)?))";" | ||
"const Stars = "(PreEscaped(serde_json::to_string(&stars)?))";" | ||
|
@@ -351,11 +348,9 @@ pub async fn repo_page( | |
// https://docs.rs/axum/latest/axum/extract/index.html#common-extractors | ||
pub async fn index(State(state): State<Arc<AppState>>, req: Request) -> HtmlRes { | ||
// let qs: Query<HashMap<String, String>> = Query::try_from_uri(req.uri())?; | ||
let qs: Query<RepoFilter> = Query::try_from_uri(req.uri())?; | ||
|
||
let db = &state.db; | ||
let repos = db.get_repos(&qs).await?; | ||
let repos = repos.into_iter().filter(|x| is_repo_included(&x.name)).collect::<Vec<_>>(); | ||
let qs: Query<RepoFilter> = Query::try_from_uri(req.uri())?; | ||
let repos = get_filtered_repos(&db, &qs).await?; | ||
|
||
let cols: Vec<(&str, Box<dyn Fn(&RepoTotals) -> Markup>, RepoSort)> = vec![ | ||
("Name", Box::new(|x| html!(a href=(format!("/{}", x.name)) { (x.name) })), RepoSort::Name), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
mod api; | ||
mod html; | ||
|
||
use std::sync::Arc; | ||
|
||
use axum::http::StatusCode; | ||
use axum::{extract::Request, middleware::Next, response::IntoResponse, routing::get, Router}; | ||
use reqwest::Method; | ||
use tower_http::cors::{Any, CorsLayer}; | ||
|
||
use crate::AppState; | ||
|
||
async fn check_api_token( | ||
req: Request, | ||
next: Next, | ||
) -> Result<impl IntoResponse, (StatusCode, String)> { | ||
let ghs_token = std::env::var("GHS_API_TOKEN").unwrap_or_default(); | ||
let req_token = crate::helpers::get_header(&req, "x-api-token").unwrap_or_default(); | ||
if ghs_token.is_empty() || req_token != ghs_token { | ||
return Err((StatusCode::UNAUTHORIZED, "unauthorized".to_string())); | ||
} | ||
|
||
let res = next.run(req).await; | ||
Ok(res) | ||
} | ||
|
||
pub fn api_routes() -> Router<Arc<AppState>> { | ||
let cors = CorsLayer::new().allow_methods([Method::GET]).allow_origin(Any); | ||
|
||
let router = Router::new() | ||
.route("/repos", get(api::api_get_repos)) | ||
.layer(axum::middleware::from_fn(check_api_token)) | ||
.layer(cors); | ||
|
||
router | ||
} | ||
|
||
pub fn html_routes() -> Router<Arc<AppState>> { | ||
Router::new().route("/", get(html::index)).route("/:owner/:repo", get(html::repo_page)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters