cargo fix & cargo fmt
This commit is contained in:
parent
6be5525bae
commit
0808fc8099
3 changed files with 49 additions and 47 deletions
|
@ -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<B>(request: Request<B>, next: Next<B>) -> 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
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Arc<AppState>> {
|
|||
.fallback_service(tower_http::services::ServeDir::new("./posts"))
|
||||
}
|
||||
|
||||
pub fn alias_router<'a>(posts: impl IntoIterator<Item=&'a Post>) -> Router<Arc<AppState>> {
|
||||
pub fn alias_router<'a>(posts: impl IntoIterator<Item = &'a Post>) -> Router<Arc<AppState>> {
|
||||
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<Arc<AppState>>) -> Result<Html<String>, 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in a new issue