Add image support

This commit is contained in:
2025-10-25 21:28:24 +08:00
parent 5928f7d13c
commit a2edf22954
2 changed files with 14 additions and 11 deletions

View File

@@ -8,4 +8,6 @@ This is a sub-heading.
## Image ## Image
![A picture of a cat](./a-new-post/photo-1608848461950-0fe51dfc41cb.jpg) <img src="./a-new-post/photo-1608848461950-0fe51dfc41cb.jpg" alt="drawing" width="200"/>

View File

@@ -1,6 +1,6 @@
mod git_tracker;
mod post_manager; mod post_manager;
mod template_engine; mod template_engine;
mod git_tracker;
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
@@ -28,11 +28,14 @@ async fn main() {
post_manager: post_manager.clone(), post_manager: post_manager.clone(),
}; };
let p_router = Router::new()
.route("/:post_name", get(post_handler))
.fallback_service(ServeDir::new("posts"));
let app = Router::new() let app = Router::new()
.route("/", get(index_handler)) .route("/", get(index_handler))
.nest_service("/posts", ServeDir::new("posts"))
.route("/:page", get(all_handler)) .route("/:page", get(all_handler))
.route("/p/:post_name", get(post_handler)) .nest("/p", p_router)
.with_state(app_state); .with_state(app_state);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
@@ -67,11 +70,11 @@ async fn post_handler(
State(state): State<AppState>, State(state): State<AppState>,
Path(post_name): Path<String>, Path(post_name): Path<String>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let manager = state.post_manager.read().await; if post_name.contains("..") {
if post_name.contains("..") || post_name.contains("/") {
return (StatusCode::NOT_FOUND, "Invalid path").into_response(); 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) {
Some(_post) => { Some(_post) => {
drop(manager); drop(manager);
@@ -84,10 +87,8 @@ async fn post_handler(
} }
} }
async fn render_template(
template_name: &str, async fn render_template(template_name: &str, state: &AppState) -> Result<String, String> {
state: &AppState,
) -> Result<String, String> {
let template_path = format!("templates/{}", template_name); let template_path = format!("templates/{}", template_name);
let template_content = std::fs::read_to_string(&template_path) let template_content = std::fs::read_to_string(&template_path)
.map_err(|e| format!("Failed to read template: {}", e))?; .map_err(|e| format!("Failed to read template: {}", e))?;