From 0d9115749b094cb338e093870cda11b7193753c2 Mon Sep 17 00:00:00 2001 From: Adrian Hedqvist Date: Sun, 26 Mar 2023 12:01:59 +0200 Subject: [PATCH] hide posts with future dates --- Cargo.lock | 116 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + Dockerfile | 21 ++++--- posts/foldertest/index.md | 7 ++- src/handlers/mod.rs | 7 ++- src/handlers/posts.rs | 15 ++++- src/main.rs | 4 +- src/post.rs | 8 +++ 8 files changed, 164 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index abcc312..0905621 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -381,6 +381,25 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf2b3e8478797446514c91ef04bafcb59faba183e621ad488df88983cc14128c" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" +dependencies = [ + "cfg-if", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -470,6 +489,19 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "dashmap" +version = "5.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "907076dfda823b0b36d2a1bb5f90c96660a5bbcd7729e10727f07858f22c4edc" +dependencies = [ + "cfg-if", + "hashbrown 0.12.3", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "deunicode" version = "0.4.3" @@ -572,12 +604,34 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd" +[[package]] +name = "futures-executor" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1997dd9df74cdac935c76252744c1ed5794fac083242ea4fe77ef3ed60ba0f83" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89d422fa3cbe3b40dca574ab087abb5bc98258ea57eea3fd6f1fa7162c778b91" +[[package]] +name = "futures-macro" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "futures-sink" version = "0.3.27" @@ -597,6 +651,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab" dependencies = [ "futures-core", + "futures-macro", "futures-sink", "futures-task", "pin-project-lite", @@ -1077,6 +1132,52 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "opentelemetry" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69d6c3d7288a106c0a363e4b0e8d308058d56902adefb16f4936f417ffef086e" +dependencies = [ + "opentelemetry_api", + "opentelemetry_sdk", +] + +[[package]] +name = "opentelemetry_api" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c24f96e21e7acc813c7a8394ee94978929db2bcc46cf6b5014fc612bf7760c22" +dependencies = [ + "fnv", + "futures-channel", + "futures-util", + "indexmap", + "js-sys", + "once_cell", + "pin-project-lite", + "thiserror", +] + +[[package]] +name = "opentelemetry_sdk" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ca41c4933371b61c2a2f214bf16931499af4ec90543604ec828f7a625c09113" +dependencies = [ + "async-trait", + "crossbeam-channel", + "dashmap", + "fnv", + "futures-channel", + "futures-executor", + "futures-util", + "once_cell", + "opentelemetry_api", + "percent-encoding", + "rand", + "thiserror", +] + [[package]] name = "overload" version = "0.1.1" @@ -1862,6 +1963,20 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-opentelemetry" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21ebb87a95ea13271332df069020513ab70bdb5637ca42d6e492dc3bbbad48de" +dependencies = [ + "once_cell", + "opentelemetry", + "tracing", + "tracing-core", + "tracing-log", + "tracing-subscriber", +] + [[package]] name = "tracing-subscriber" version = "0.3.16" @@ -2105,6 +2220,7 @@ dependencies = [ "tower", "tower-http", "tracing", + "tracing-opentelemetry", "tracing-subscriber", ] diff --git a/Cargo.toml b/Cargo.toml index caf4133..fd4a87d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,5 @@ toml = "0.7.3" tower = { version = "0.4.12", features = ["full"] } tower-http = { version = "0.4.0", features = ["full"] } tracing = "0.1.35" +tracing-opentelemetry = "0.18.0" tracing-subscriber = { version = "0.3.11", features = ["fmt"] } diff --git a/Dockerfile b/Dockerfile index 5d66c30..58463a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,15 @@ FROM rust:slim AS chef RUN cargo install cargo-chef -WORKDIR app +WORKDIR /app #################################################################################################### ## Planner #################################################################################################### FROM chef AS planner -COPY . . +WORKDIR /app +COPY ./Cargo.lock ./ +COPY ./Cargo.toml ./ +COPY ./src ./src RUN cargo chef prepare --recipe-path recipe.json #################################################################################################### @@ -33,10 +36,14 @@ RUN adduser \ WORKDIR /app -COPY --from=planner /app/recipe.json . +COPY --from=planner /app/recipe.json ./ + RUN cargo chef cook --target x86_64-unknown-linux-musl --release --recipe-path recipe.json -COPY . . +COPY ./Cargo.lock ./ +COPY ./Cargo.toml ./ + +COPY ./src ./src RUN cargo build --target x86_64-unknown-linux-musl --release @@ -53,9 +60,9 @@ WORKDIR /app # Copy our build COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/website ./ -COPY --from=builder /app/posts ./posts -COPY --from=builder /app/static ./static -COPY --from=builder /app/templates ./templates +COPY ./static ./static +COPY ./templates ./templates +COPY ./posts ./posts EXPOSE 8180 diff --git a/posts/foldertest/index.md b/posts/foldertest/index.md index 45721db..b7e741c 100644 --- a/posts/foldertest/index.md +++ b/posts/foldertest/index.md @@ -1,10 +1,13 @@ +++ title="TOML metadata test" -date=2023-03-25T14:50:25+01:00 +date=2023-03-26T11:57:00+02:00 +++ hope it works yay -here have a squid miku to test relative paths: +here have a squid kid miku to test relative paths: ![Squid kid miku](FbHSmoeUUAA2x-m.png) + +modified post test, see if docker skips build using +its cache if only a post has changed. diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index eeadb95..b5a3f80 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -13,8 +13,11 @@ use crate::{State, WebsiteError}; pub mod posts; lazy_static! { - pub static ref HIT_COUNTER: IntCounterVec = - prometheus::register_int_counter_vec!(opts!("page_hits", "Number of hits to various pages"), &["page"]).unwrap(); + pub static ref HIT_COUNTER: IntCounterVec = prometheus::register_int_counter_vec!( + opts!("page_hits", "Number of hits to various pages"), + &["page"] + ) + .unwrap(); } #[instrument(skip(state))] diff --git a/src/handlers/posts.rs b/src/handlers/posts.rs index b47648c..061efbc 100644 --- a/src/handlers/posts.rs +++ b/src/handlers/posts.rs @@ -26,9 +26,16 @@ pub async fn view( debug!("viewing post: {slug}"); let post = state.posts.get(&slug).ok_or(WebsiteError::NotFound)?; + if !post.is_published() { + warn!("attempted to view post before it has been published!"); + return Err(WebsiteError::NotFound); + } + let res = render_post(&state.tera, post).await?; - HIT_COUNTER.with_label_values(&[&format!("/posts/{}/", slug)]).inc(); + HIT_COUNTER + .with_label_values(&[&format!("/posts/{}/", slug)]) + .inc(); Ok(Html(res)) } @@ -40,7 +47,11 @@ struct IndexContext<'a> { #[instrument(skip(state))] pub async fn index(Extension(state): Extension>) -> Result, WebsiteError> { - let mut posts = state.posts.values().collect::>(); + let mut posts = state + .posts + .values() + .filter(|p| p.is_published()) + .collect::>(); posts.sort_by_key(|p| &p.date); posts.reverse(); diff --git a/src/main.rs b/src/main.rs index 1a87c3c..0d834ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ use std::{collections::HashMap, sync::Arc}; -use axum::{routing::get, Extension, Router, response::Response, body}; +use axum::{body, response::Response, routing::get, Extension, Router}; use color_eyre::eyre::Result; use hyper::header::CONTENT_TYPE; use post::Post; -use prometheus::{TextEncoder, Encoder}; +use prometheus::{Encoder, TextEncoder}; use tera::Tera; use tower_http::{compression::CompressionLayer, trace::TraceLayer}; use tracing::{instrument, log::*}; diff --git a/src/post.rs b/src/post.rs index 83ce45b..6a5aea5 100644 --- a/src/post.rs +++ b/src/post.rs @@ -47,6 +47,14 @@ impl Post { tags: fm.tags.unwrap_or_default(), } } + + pub fn is_published(&self) -> bool { + let now = chrono::offset::Local::now(); + if let Some(date) = self.date { + return date.timestamp() - now.timestamp() <= 0; + } + true + } } #[instrument]