From c421c4e349aa69ce0573050703e3b30d674186fc Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 16 Jul 2020 07:30:57 -0400 Subject: [PATCH] sitemap support --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 1 + src/app.rs | 21 +++++++++++++++++++++ src/handlers/feeds.rs | 12 +++++++++++- src/main.rs | 7 +++++-- 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 914e17d..5061ed8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -244,6 +244,15 @@ dependencies = [ "time", ] +[[package]] +name = "chrono_utils" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f69ed74e2117892a1a4e05d31f6612178e8e827bfbd83bbf8ca8c1bcfbda710" +dependencies = [ + "chrono", +] + [[package]] name = "clap" version = "2.33.1" @@ -1903,6 +1912,18 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" +[[package]] +name = "sitemap" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c95c7e58b4461fec85bdd58f271bcd416ecc4d630c3ac280b60efa3421016b7" +dependencies = [ + "chrono", + "chrono_utils", + "url", + "xml-rs", +] + [[package]] name = "slab" version = "0.4.2" @@ -2524,6 +2545,7 @@ dependencies = [ "serde", "serde_dhall", "serde_yaml", + "sitemap", "thiserror", "tokio", "warp", diff --git a/Cargo.toml b/Cargo.toml index 4d3089e..d1cb0de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ ructe = "0.11" serde_dhall = "0.5.3" serde = { version = "1", features = ["derive"] } serde_yaml = "0.8" +sitemap = "0.4" thiserror = "1" tokio = { version = "0.2", features = ["macros"] } warp = "0.2" diff --git a/src/app.rs b/src/app.rs index 50af7f2..272e673 100644 --- a/src/app.rs +++ b/src/app.rs @@ -43,6 +43,7 @@ pub struct State { pub jf: jsonfeed::Feed, pub rf: rss::Channel, pub af: atom::Feed, + pub sitemap: Vec, } pub fn init(cfg: PathBuf) -> Result { @@ -119,6 +120,25 @@ pub fn init(cfg: PathBuf) -> Result { rf.build().unwrap() }; + let mut sm: Vec = vec![]; + let smw = sitemap::writer::SiteMapWriter::new(&mut sm); + let mut urlwriter = smw.start_urlset()?; + for url in &[ + "https://christine.website/resume", + "https://christine.website/contact", + "https://christine.website/", + "https://christine.website/blog", + "https://christine.website/signalboost", + ] { + urlwriter.url(*url)?; + } + + for post in &everything { + urlwriter.url(format!("https://christine.website/{}", post.link))?; + } + + urlwriter.end()?; + Ok(State { cfg: cfg, signalboost: sb, @@ -130,6 +150,7 @@ pub fn init(cfg: PathBuf) -> Result { jf: jfb.build(), af: af, rf: rf, + sitemap: sm, }) } diff --git a/src/handlers/feeds.rs b/src/handlers/feeds.rs index 4a0922b..7d7db32 100644 --- a/src/handlers/feeds.rs +++ b/src/handlers/feeds.rs @@ -44,7 +44,6 @@ pub async fn atom(state: Arc) -> Result { .map_err(warp::reject::custom) } - pub async fn rss(state: Arc) -> Result { HIT_COUNTER.with_label_values(&["rss"]).inc(); let state = state.clone(); @@ -61,3 +60,14 @@ pub async fn rss(state: Arc) -> Result { .map_err(RenderError::Build) .map_err(warp::reject::custom) } + +pub async fn sitemap(state: Arc) -> Result { + HIT_COUNTER.with_label_values(&["sitemap"]).inc(); + let state = state.clone(); + Response::builder() + .status(200) + .header("Content-Type", "application/xml") + .body(state.sitemap.clone()) + .map_err(RenderError::Build) + .map_err(warp::reject::custom) +} diff --git a/src/main.rs b/src/main.rs index d14c691..1743db7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ fn with_state( #[tokio::main] async fn main() -> Result<()> { pretty_env_logger::init(); - log::info!("starting up"); + log::info!("starting up commit {}", env!("GITHUB_SHA")); let state = Arc::new(app::init( std::env::var("CONFIG_FNAME") @@ -103,6 +103,9 @@ async fn main() -> Result<()> { let rss = warp::path("blog.rss") .and(with_state(state.clone())) .and_then(handlers::feeds::rss); + let sitemap = warp::path("sitemap.xml") + .and(with_state(state.clone())) + .and_then(handlers::feeds::sitemap); let go_vanity_jsonfeed = warp::path("jsonfeed") .and(warp::any().map(move || "christine.website/jsonfeed")) @@ -126,7 +129,7 @@ async fn main() -> Result<()> { .or(blog_index.or(series.or(series_view).or(post_view))) .or(gallery_index.or(gallery_post_view)) .or(talk_index.or(talk_post_view)) - .or(jsonfeed.or(atom).or(rss)) + .or(jsonfeed.or(atom).or(rss.or(sitemap))) .or(files.or(css).or(favicon).or(sw.or(robots))) .or(healthcheck.or(metrics_endpoint).or(go_vanity_jsonfeed)) .map(|reply| {