cargo fmt
This commit is contained in:
parent
bfaa06fe5e
commit
4ed0341a69
5 changed files with 32 additions and 23 deletions
|
@ -1,12 +1,10 @@
|
|||
use serde::Serialize;
|
||||
use color_eyre::Result;
|
||||
use serde_derive::Serialize;
|
||||
use tera::Tera;
|
||||
use tracing::instrument;
|
||||
use serde_derive::Serialize;
|
||||
use color_eyre::Result;
|
||||
|
||||
use crate::{post::Post, tag::Tag};
|
||||
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct FeedContext<'a> {
|
||||
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"),
|
||||
last_updated: &updated.map_or_else(String::default, |d| d.to_rfc3339()),
|
||||
tag: Some(tag),
|
||||
posts
|
||||
posts,
|
||||
};
|
||||
|
||||
let ctx = tera::Context::from_serialize(&feed)?;
|
||||
|
|
|
@ -9,8 +9,8 @@ use axum::{
|
|||
use hyper::{header::CONTENT_TYPE, Request, StatusCode};
|
||||
use lazy_static::lazy_static;
|
||||
use prometheus::{opts, Encoder, IntCounterVec, TextEncoder};
|
||||
use tower_http::services::ServeFile;
|
||||
use std::sync::Arc;
|
||||
use tower_http::services::ServeFile;
|
||||
use tracing::{instrument, log::*};
|
||||
|
||||
use crate::{AppState, WebsiteError};
|
||||
|
@ -42,7 +42,10 @@ pub fn routes(state: &Arc<AppState>) -> Router<Arc<AppState>> {
|
|||
"/posts/:slug/*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")))
|
||||
.route_service(
|
||||
"/static/*path",
|
||||
tower_http::services::ServeDir::new("./").fallback(ServeFile::new("./404.html")),
|
||||
)
|
||||
.fallback_service(ServeFile::new("./404.html"))
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
|||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
response::{Html, Redirect, IntoResponse},
|
||||
response::{Html, IntoResponse, Redirect},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
|
@ -82,21 +82,18 @@ pub async fn view(
|
|||
Ok(Html(res))
|
||||
}
|
||||
|
||||
pub async fn feed(
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<impl IntoResponse, WebsiteError> {
|
||||
|
||||
let mut posts: Vec<&Post> = state
|
||||
.posts
|
||||
.values()
|
||||
.filter(|p| p.is_published())
|
||||
.collect();
|
||||
pub async fn feed(State(state): State<Arc<AppState>>) -> 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.reverse();
|
||||
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))]
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
|||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
response::{Html, Redirect, IntoResponse},
|
||||
response::{Html, IntoResponse, Redirect},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
|
@ -83,7 +83,11 @@ pub async fn feed(
|
|||
posts.reverse();
|
||||
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))]
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -12,10 +12,10 @@ use tower_http::{compression::CompressionLayer, cors::CorsLayer};
|
|||
use tracing::{info_span, log::*, Span};
|
||||
use tracing_subscriber::{prelude::*, EnvFilter};
|
||||
|
||||
mod feed;
|
||||
mod handlers;
|
||||
mod post;
|
||||
mod tag;
|
||||
mod feed;
|
||||
|
||||
pub struct AppState {
|
||||
base_url: String,
|
||||
|
@ -31,11 +31,18 @@ async fn main() -> Result<()> {
|
|||
|
||||
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 posts = post::load_all().await?;
|
||||
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)
|
||||
.layer(CorsLayer::permissive())
|
||||
|
|
Loading…
Reference in a new issue