1
0
Fork 0
This commit is contained in:
Adrian Hedqvist 2023-03-26 12:40:25 +02:00
parent 0d9115749b
commit ff8f38c563
5 changed files with 28 additions and 151 deletions

123
Cargo.lock generated
View file

@ -50,12 +50,6 @@ dependencies = [
"libc",
]
[[package]]
name = "anyhow"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
[[package]]
name = "async-compression"
version = "0.3.15"
@ -381,25 +375,6 @@ 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"
@ -489,19 +464,6 @@ 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"
@ -604,34 +566,12 @@ 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"
@ -651,7 +591,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab"
dependencies = [
"futures-core",
"futures-macro",
"futures-sink",
"futures-task",
"pin-project-lite",
@ -1132,52 +1071,6 @@ 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"
@ -1963,20 +1856,6 @@ 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"
@ -2200,7 +2079,6 @@ checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
name = "website"
version = "0.1.0"
dependencies = [
"anyhow",
"axum",
"cached",
"chrono",
@ -2220,7 +2098,6 @@ dependencies = [
"tower",
"tower-http",
"tracing",
"tracing-opentelemetry",
"tracing-subscriber",
]

View file

@ -6,13 +6,11 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.70"
axum = { version = "0.6.12", features = ["http2"] }
cached = "0.42.0"
chrono = { version = "0.4.24", features = ["serde"] }
color-eyre = "0.6.1"
glob = "0.3.0"
#grass = { version = "0.12.3", features = ["random"] } # not really needed yet
hyper = { version = "0.14.19", features = ["full"] }
lazy_static = "1.4.0"
prometheus = { version = "0.13.3", features = ["process"] }
@ -27,5 +25,4 @@ 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"] }

View file

@ -33,6 +33,10 @@ pub async fn index(
Ok(Html(res.into()))
}
pub async fn not_found() -> Response {
(StatusCode::NOT_FOUND, ()).into_response()
}
impl IntoResponse for WebsiteError {
fn into_response(self) -> Response {
match self {

View file

@ -15,30 +15,10 @@ pub fn router() -> Router {
Router::new()
.route("/", get(index))
.route("/:slug/", get(view))
.route("/:slug/index.md", get(super::not_found))
.fallback_service(tower_http::services::ServeDir::new("./posts"))
}
#[instrument(skip(state))]
pub async fn view(
Path(slug): Path<String>,
Extension(state): Extension<Arc<State>>,
) -> Result<Html<String>, WebsiteError> {
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();
Ok(Html(res))
}
#[derive(Serialize)]
struct IndexContext<'a> {
title: &'a str,
@ -75,3 +55,22 @@ pub async fn index(Extension(state): Extension<Arc<State>>) -> Result<Html<Strin
HIT_COUNTER.with_label_values(&["/posts/"]).inc();
Ok(Html(res))
}
#[instrument(skip(state))]
pub async fn view(
Path(slug): Path<String>,
Extension(state): Extension<Arc<State>>,
) -> Result<Html<String>, WebsiteError> {
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();
Ok(Html(res))
}

View file

@ -1,7 +1,7 @@
use std::{collections::HashMap, sync::Arc};
use axum::{body, response::Response, routing::get, Extension, Router};
use color_eyre::eyre::Result;
use color_eyre::eyre::{Result, Error};
use hyper::header::CONTENT_TYPE;
use post::Post;
use prometheus::{Encoder, TextEncoder};
@ -50,7 +50,7 @@ pub async fn init_app() -> Result<Router> {
.route("/", get(handlers::index))
.nest("/posts", handlers::posts::router())
.nest_service("/static", tower_http::services::ServeDir::new("./static"))
.route("/.healthcheck", get(healthcheck))
.route("/healthcheck", get(healthcheck))
.route("/metrics", get(metrics))
.layer(middleware);
@ -76,12 +76,12 @@ async fn metrics() -> Response {
#[derive(Debug)]
pub enum WebsiteError {
NotFound,
InternalError(anyhow::Error),
InternalError(Error),
}
impl<E> From<E> for WebsiteError
where
E: Into<anyhow::Error>,
E: Into<Error>,
{
fn from(value: E) -> Self {
WebsiteError::InternalError(value.into())