From 0808fc80999c56884143880b563c5ba51b48be19 Mon Sep 17 00:00:00 2001 From: Adrian Hedqvist Date: Wed, 29 Mar 2023 18:20:51 +0200 Subject: [PATCH] cargo fix & cargo fmt --- src/handlers/mod.rs | 20 +++++++++---- src/handlers/posts.rs | 70 ++++++++++++++++++++----------------------- src/main.rs | 6 ++-- 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 97d1238..69a9da5 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,10 +1,14 @@ use axum::{ + body, + extract::State, + middleware::Next, response::{Html, IntoResponse, Response}, - Router, routing::get, extract::State, middleware::Next, body, + routing::get, + Router, }; -use hyper::{StatusCode, Request, header::CONTENT_TYPE}; +use hyper::{header::CONTENT_TYPE, Request, StatusCode}; use lazy_static::lazy_static; -use prometheus::{opts, IntCounterVec, TextEncoder, Encoder}; +use prometheus::{opts, Encoder, IntCounterVec, TextEncoder}; use std::sync::Arc; use tracing::{instrument, log::*}; @@ -14,7 +18,10 @@ pub mod posts; lazy_static! { pub static ref HIT_COUNTER: IntCounterVec = prometheus::register_int_counter_vec!( - opts!("http_requests_total", "Total amount of http requests received"), + opts!( + "http_requests_total", + "Total amount of http requests received" + ), &["route", "method", "status"] ) .unwrap(); @@ -43,7 +50,6 @@ pub async fn index( Ok(Html(res)) } - async fn healthcheck() -> &'static str { "OK" } @@ -69,7 +75,9 @@ pub async fn metrics_middleware(request: Request, next: Next) -> Respon let path = request.uri().path().to_string(); let method = request.method().to_string(); let response = next.run(request).await; - HIT_COUNTER.with_label_values(&[&path, &method, response.status().as_str()]).inc(); + HIT_COUNTER + .with_label_values(&[&path, &method, response.status().as_str()]) + .inc(); response } diff --git a/src/handlers/posts.rs b/src/handlers/posts.rs index f5652b0..a0e0253 100644 --- a/src/handlers/posts.rs +++ b/src/handlers/posts.rs @@ -1,6 +1,11 @@ use std::sync::Arc; -use axum::{extract::{Path, State}, response::{Html, Redirect}, routing::get, Extension, Router}; +use axum::{ + extract::{Path, State}, + response::{Html, Redirect}, + routing::get, + Router, +}; use serde_derive::Serialize; use tracing::{instrument, log::*}; @@ -17,16 +22,19 @@ pub fn router() -> Router> { .fallback_service(tower_http::services::ServeDir::new("./posts")) } -pub fn alias_router<'a>(posts: impl IntoIterator) -> Router> { +pub fn alias_router<'a>(posts: impl IntoIterator) -> Router> { let mut router = Router::new(); for post in posts { for alias in &post.aliases { let path = post.absolute_path.to_owned(); - router = router.route(alias, get(move || async { - let path = path; - Redirect::permanent(&path) - })); + router = router.route( + alias, + get(move || async { + let path = path; + Redirect::permanent(&path) + }), + ); } } router @@ -39,25 +47,18 @@ struct PageContext<'a> { #[instrument(skip(state))] pub async fn index(State(state): State>) -> Result, WebsiteError> { - let mut posts: Vec<&Post> = state - .posts - .values() - .filter(|p| p.is_published()) - .collect(); + let mut posts: Vec<&Post> = state.posts.values().filter(|p| p.is_published()).collect(); posts.sort_by_key(|p| &p.date); posts.reverse(); - let ctx = PageContext { - title: "Posts", - }; + let ctx = PageContext { title: "Posts" }; let mut c = tera::Context::new(); c.insert("page", &ctx); c.insert("posts", &posts); - let res = state.tera - .render("posts_index.html", &c)?; + let res = state.tera.render("posts_index.html", &c)?; Ok(Html(res)) } @@ -88,31 +89,26 @@ mod tests { #[test] fn render_index() { - let posts = vec![Post { - title: "test".into(), - slug: "test".into(), - date: Some(DateTime::parse_from_rfc3339("2023-03-26T13:04:01+02:00").unwrap()), - ..Default::default() - }, - Post { - title: "test2".into(), - slug: "test2".into(), - date: None, - ..Default::default() - }]; - let page = PageContext { - title: "Posts", - }; + let posts = vec![ + Post { + title: "test".into(), + slug: "test".into(), + date: Some(DateTime::parse_from_rfc3339("2023-03-26T13:04:01+02:00").unwrap()), + ..Default::default() + }, + Post { + title: "test2".into(), + slug: "test2".into(), + date: None, + ..Default::default() + }, + ]; + let page = PageContext { title: "Posts" }; let mut ctx = tera::Context::new(); ctx.insert("page", &page); ctx.insert("posts", &posts); let tera = tera::Tera::new("templates/**/*").unwrap(); - let _res = tera - .render( - "posts_index.html", - &ctx, - ) - .unwrap(); + let _res = tera.render("posts_index.html", &ctx).unwrap(); } } diff --git a/src/main.rs b/src/main.rs index 0123040..ef3943c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ use std::{collections::HashMap, sync::Arc}; -use axum::{body, response::Response, routing::get, Extension, Router, extract::State}; use color_eyre::eyre::{Error, Result}; -use hyper::header::CONTENT_TYPE; + use post::Post; -use prometheus::{Encoder, TextEncoder}; + use tera::Tera; use tower_http::{compression::CompressionLayer, trace::TraceLayer}; use tracing::log::*; @@ -27,7 +26,6 @@ async fn main() -> Result<()> { let posts = post::load_all().await?; let state = Arc::new(AppState { tera, posts }); - let app = handlers::routes(&state) .layer(TraceLayer::new_for_http()) .layer(CompressionLayer::new())