1
0
Fork 0

cargo fmt

This commit is contained in:
Adrian Hedqvist 2023-04-03 23:33:43 +02:00
parent bfaa06fe5e
commit 4ed0341a69
5 changed files with 32 additions and 23 deletions

View file

@ -1,12 +1,10 @@
use serde::Serialize; use color_eyre::Result;
use serde_derive::Serialize;
use tera::Tera; use tera::Tera;
use tracing::instrument; use tracing::instrument;
use serde_derive::Serialize;
use color_eyre::Result;
use crate::{post::Post, tag::Tag}; use crate::{post::Post, tag::Tag};
#[derive(Serialize)] #[derive(Serialize)]
struct FeedContext<'a> { struct FeedContext<'a> {
feed_url: &'a str, feed_url: &'a str,
@ -38,7 +36,7 @@ pub fn render_atom_tag_feed(tag: &Tag, posts: &[&Post], tera: &Tera) -> Result<S
feed_url: &format!("https://tollyx.net/tags/{slug}/atom.xml"), feed_url: &format!("https://tollyx.net/tags/{slug}/atom.xml"),
last_updated: &updated.map_or_else(String::default, |d| d.to_rfc3339()), last_updated: &updated.map_or_else(String::default, |d| d.to_rfc3339()),
tag: Some(tag), tag: Some(tag),
posts posts,
}; };
let ctx = tera::Context::from_serialize(&feed)?; let ctx = tera::Context::from_serialize(&feed)?;

View file

@ -9,8 +9,8 @@ use axum::{
use hyper::{header::CONTENT_TYPE, Request, StatusCode}; use hyper::{header::CONTENT_TYPE, Request, StatusCode};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use prometheus::{opts, Encoder, IntCounterVec, TextEncoder}; use prometheus::{opts, Encoder, IntCounterVec, TextEncoder};
use tower_http::services::ServeFile;
use std::sync::Arc; use std::sync::Arc;
use tower_http::services::ServeFile;
use tracing::{instrument, log::*}; use tracing::{instrument, log::*};
use crate::{AppState, WebsiteError}; use crate::{AppState, WebsiteError};
@ -42,7 +42,10 @@ pub fn routes(state: &Arc<AppState>) -> Router<Arc<AppState>> {
"/posts/:slug/*path", "/posts/:slug/*path",
tower_http::services::ServeDir::new("./").fallback(ServeFile::new("./404.html")), tower_http::services::ServeDir::new("./").fallback(ServeFile::new("./404.html")),
) )
.route_service("/static/*path", tower_http::services::ServeDir::new("./").fallback(ServeFile::new("./404.html"))) .route_service(
"/static/*path",
tower_http::services::ServeDir::new("./").fallback(ServeFile::new("./404.html")),
)
.fallback_service(ServeFile::new("./404.html")) .fallback_service(ServeFile::new("./404.html"))
} }

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
response::{Html, Redirect, IntoResponse}, response::{Html, IntoResponse, Redirect},
routing::get, routing::get,
Router, Router,
}; };
@ -82,21 +82,18 @@ pub async fn view(
Ok(Html(res)) Ok(Html(res))
} }
pub async fn feed( pub async fn feed(State(state): State<Arc<AppState>>) -> Result<impl IntoResponse, WebsiteError> {
State(state): State<Arc<AppState>>, let mut posts: Vec<&Post> = state.posts.values().filter(|p| p.is_published()).collect();
) -> Result<impl IntoResponse, WebsiteError> {
let mut posts: Vec<&Post> = state
.posts
.values()
.filter(|p| p.is_published())
.collect();
posts.sort_by_key(|p| &p.date); posts.sort_by_key(|p| &p.date);
posts.reverse(); posts.reverse();
posts.truncate(10); posts.truncate(10);
Ok((StatusCode::OK, [(CONTENT_TYPE, "application/atom+xml")], crate::feed::render_atom_feed(&posts, &state.tera)?)) Ok((
StatusCode::OK,
[(CONTENT_TYPE, "application/atom+xml")],
crate::feed::render_atom_feed(&posts, &state.tera)?,
))
} }
#[instrument(skip(state))] #[instrument(skip(state))]

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
response::{Html, Redirect, IntoResponse}, response::{Html, IntoResponse, Redirect},
routing::get, routing::get,
Router, Router,
}; };
@ -83,7 +83,11 @@ pub async fn feed(
posts.reverse(); posts.reverse();
posts.truncate(10); posts.truncate(10);
Ok((StatusCode::OK, [(CONTENT_TYPE, "application/atom+xml")], crate::feed::render_atom_tag_feed(tag, &posts, &state.tera)?)) Ok((
StatusCode::OK,
[(CONTENT_TYPE, "application/atom+xml")],
crate::feed::render_atom_tag_feed(tag, &posts, &state.tera)?,
))
} }
#[instrument(skip(state))] #[instrument(skip(state))]

View file

@ -12,10 +12,10 @@ use tower_http::{compression::CompressionLayer, cors::CorsLayer};
use tracing::{info_span, log::*, Span}; use tracing::{info_span, log::*, Span};
use tracing_subscriber::{prelude::*, EnvFilter}; use tracing_subscriber::{prelude::*, EnvFilter};
mod feed;
mod handlers; mod handlers;
mod post; mod post;
mod tag; mod tag;
mod feed;
pub struct AppState { pub struct AppState {
base_url: String, base_url: String,
@ -31,11 +31,18 @@ async fn main() -> Result<()> {
info!("Starting server..."); info!("Starting server...");
let base_url = option_env!("SITE_BASE_URL").unwrap_or("http://localhost:8180").to_string(); let base_url = option_env!("SITE_BASE_URL")
.unwrap_or("http://localhost:8180")
.to_string();
let tera = Tera::new("templates/**/*")?; let tera = Tera::new("templates/**/*")?;
let posts = post::load_all().await?; let posts = post::load_all().await?;
let tags = tag::get_tags(posts.values()); let tags = tag::get_tags(posts.values());
let state = Arc::new(AppState { base_url, tera, posts, tags }); let state = Arc::new(AppState {
base_url,
tera,
posts,
tags,
});
let app = handlers::routes(&state) let app = handlers::routes(&state)
.layer(CorsLayer::permissive()) .layer(CorsLayer::permissive())