sitemap support

This commit is contained in:
Cadey Ratio 2020-07-16 07:30:57 -04:00
parent 975d7df714
commit c421c4e349
5 changed files with 60 additions and 3 deletions

22
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -43,6 +43,7 @@ pub struct State {
pub jf: jsonfeed::Feed,
pub rf: rss::Channel,
pub af: atom::Feed,
pub sitemap: Vec<u8>,
}
pub fn init(cfg: PathBuf) -> Result<State> {
@ -119,6 +120,25 @@ pub fn init(cfg: PathBuf) -> Result<State> {
rf.build().unwrap()
};
let mut sm: Vec<u8> = 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<State> {
jf: jfb.build(),
af: af,
rf: rf,
sitemap: sm,
})
}

View File

@ -44,7 +44,6 @@ pub async fn atom(state: Arc<State>) -> Result<impl Reply, Rejection> {
.map_err(warp::reject::custom)
}
pub async fn rss(state: Arc<State>) -> Result<impl Reply, Rejection> {
HIT_COUNTER.with_label_values(&["rss"]).inc();
let state = state.clone();
@ -61,3 +60,14 @@ pub async fn rss(state: Arc<State>) -> Result<impl Reply, Rejection> {
.map_err(RenderError::Build)
.map_err(warp::reject::custom)
}
pub async fn sitemap(state: Arc<State>) -> Result<impl Reply, Rejection> {
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)
}

View File

@ -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| {