An ATProtocol powered blogging engine.

feature: sitemap handler

Signed-off-by: Nick Gerakines <nick.gerakines@gmail.com>

Changed files
+39 -1
src
templates
+28 -1
src/http.rs
··· 9 9 Router, 10 10 body::Body, 11 11 extract::{Path, State}, 12 - http::{StatusCode, header}, 12 + http::{HeaderMap, HeaderValue, StatusCode, header}, 13 13 response::{IntoResponse, Response}, 14 14 routing::get, 15 15 }; ··· 83 83 title: String, 84 84 created_at: String, 85 85 created_at_date: String, 86 + updated_at_date: String, 86 87 aturi: String, 87 88 } 88 89 ··· 96 97 title: post.title, 97 98 created_at: post.created_at.format("%Y-%m-%d %H:%M UTC").to_string(), 98 99 created_at_date: post.created_at.format("%Y-%m-%d").to_string(), 100 + updated_at_date: post.updated_at.format("%Y-%m-%d").to_string(), 99 101 }) 100 102 } 101 103 } ··· 104 106 pub fn create_router(state: AppState) -> Router { 105 107 Router::new() 106 108 .route("/", get(handle_index)) 109 + .route("/sitemap.xml", get(hande_sitemap)) 107 110 .route("/posts/{full_slug}", get(handle_post)) 108 111 .route( 109 112 "/posts/{full_slug}/{collection}", ··· 112 115 .route("/content/{cid}", get(handle_content)) 113 116 .nest_service("/static", ServeDir::new(&state.config.http.static_path)) 114 117 .with_state(state) 118 + } 119 + 120 + async fn hande_sitemap(State(state): State<AppState>) -> Result<impl IntoResponse> { 121 + match get_all_posts(&state).await { 122 + Ok(posts) => { 123 + let mut headers = HeaderMap::new(); 124 + headers.insert( 125 + "Content-Type", 126 + HeaderValue::from_static("application/xml; charset=utf-8"), 127 + ); 128 + 129 + Ok(( 130 + headers, 131 + RenderHtml( 132 + "sitemap.xml", 133 + state.template_env.clone(), 134 + context! { 135 + posts => posts, 136 + }, 137 + ), 138 + )) 139 + } 140 + Err(_) => Err(BlahgError::HttpInternalServerError), 141 + } 115 142 } 116 143 117 144 /// GET / - Handle the index page (home page).
+11
templates/sitemap.xml
··· 1 + <?xml version='1.0' encoding='UTF-8'?> 2 + <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 + xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 4 + xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 5 + {% for post in posts %} 6 + <url> 7 + <loc>{{ external_base }}/posts/{{ post.slug }}</loc> 8 + <lastmod>{{ post.updated_at_date }}</lastmod> 9 + </url> 10 + {% endfor %} 11 + </urlset>