Update server post and fix post titles

This commit is contained in:
2025-10-29 19:02:55 +08:00
parent 185d9d4d63
commit c584d39607
8 changed files with 493 additions and 61 deletions

View File

@@ -5,15 +5,24 @@ mod template_engine;
use axum::{
extract::{Path, State},
http::StatusCode,
response::{Html, IntoResponse},
response::{Html, IntoResponse, Response},
routing::get,
Router,
};
use clap::Parser;
use std::sync::Arc;
use tokio::process::Command;
use tokio::sync::RwLock;
use tower_http::services::ServeDir;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Disable caching of markdown rendering
#[arg(long)]
no_cache: bool,
}
#[derive(Clone)]
struct AppState {
post_manager: Arc<RwLock<post_manager::PostManager>>,
@@ -21,14 +30,20 @@ struct AppState {
#[tokio::main]
async fn main() {
let args = Args::parse();
let post_manager = Arc::new(RwLock::new(
post_manager::PostManager::new(".").expect("Failed to initialize post manager"),
post_manager::PostManager::new(".", args.no_cache).expect("Failed to initialize post manager"),
));
let app_state = AppState {
post_manager: post_manager.clone(),
};
if args.no_cache {
println!("Running with caching disabled");
}
let p_router = Router::new()
.route("/:post_name", get(post_handler))
.fallback_service(ServeDir::new("posts"));
@@ -51,14 +66,14 @@ async fn main() {
.expect("Failed to start server");
}
async fn index_handler(State(state): State<AppState>) -> impl IntoResponse {
async fn index_handler(State(state): State<AppState>) -> Response {
match render_template("index.html", &state).await {
Ok(html) => Html(html).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(),
}
}
async fn all_handler(State(state): State<AppState>, Path(page): Path<String>) -> impl IntoResponse {
async fn all_handler(State(state): State<AppState>, Path(page): Path<String>) -> Response {
if page.contains("..") {
return (StatusCode::NOT_FOUND, "Invalid path").into_response();
}
@@ -71,13 +86,13 @@ async fn all_handler(State(state): State<AppState>, Path(page): Path<String>) ->
async fn post_handler(
State(state): State<AppState>,
Path(post_name): Path<String>,
) -> impl IntoResponse {
) -> Response {
if post_name.contains("..") {
return (StatusCode::NOT_FOUND, "Invalid path").into_response();
}
let manager = state.post_manager.read().await;
match manager.get_post(&post_name) {
match manager.get_post(&post_name).await {
Some(_post) => {
drop(manager);
match render_post_template(&state, post_name).await {
@@ -148,7 +163,7 @@ async fn render_template(template_name: &str, state: &AppState) -> Result<String
.map_err(|e| format!("Failed to read template: {}", e))?;
let manager = state.post_manager.read().await;
template_engine::render_template(&template_content, &*manager, None)
template_engine::render_template(&template_content, &*manager, None).await
}
async fn render_post_template(state: &AppState, post_name: String) -> Result<String, String> {
@@ -156,5 +171,5 @@ async fn render_post_template(state: &AppState, post_name: String) -> Result<Str
.map_err(|e| format!("Failed to read post template: {}", e))?;
let manager = state.post_manager.read().await;
template_engine::render_template(&template_content, &*manager, Some(&post_name))
template_engine::render_template(&template_content, &*manager, Some(&post_name)).await
}