xesite/src/handlers/mod.rs

127 lines
3.9 KiB
Rust
Raw Normal View History

2020-07-12 23:26:53 +00:00
use crate::{
app::State,
2020-07-12 23:30:01 +00:00
templates::{self, Html, RenderRucte},
2020-07-12 23:26:53 +00:00
};
2020-07-14 13:10:12 +00:00
use lazy_static::lazy_static;
use prometheus::{IntCounterVec, register_int_counter_vec, opts};
2020-07-13 11:54:17 +00:00
use std::{convert::Infallible, fmt, sync::Arc};
use warp::{
http::{Response, StatusCode},
Rejection, Reply,
};
2020-07-12 22:39:50 +00:00
2020-07-14 13:10:12 +00:00
lazy_static! {
static ref HIT_COUNTER: IntCounterVec =
register_int_counter_vec!(opts!("hits", "Number of hits to various pages"), &["page"])
.unwrap();
}
2020-07-12 22:39:50 +00:00
pub async fn index() -> Result<impl Reply, Rejection> {
2020-07-14 13:10:12 +00:00
HIT_COUNTER.with_label_values(&["index"]).inc();
2020-07-12 22:39:50 +00:00
Response::builder().html(|o| templates::index_html(o))
}
2020-07-12 22:58:38 +00:00
pub async fn contact() -> Result<impl Reply, Rejection> {
2020-07-14 13:10:12 +00:00
HIT_COUNTER.with_label_values(&["contact"]).inc();
2020-07-12 22:58:38 +00:00
Response::builder().html(|o| templates::contact_html(o))
}
pub async fn feeds() -> Result<impl Reply, Rejection> {
2020-07-14 13:10:12 +00:00
HIT_COUNTER.with_label_values(&["feeds"]).inc();
2020-07-12 22:58:38 +00:00
Response::builder().html(|o| templates::feeds_html(o))
}
2020-07-12 23:26:53 +00:00
pub async fn resume(state: Arc<State>) -> Result<impl Reply, Rejection> {
2020-07-14 13:10:12 +00:00
HIT_COUNTER.with_label_values(&["resume"]).inc();
2020-07-12 23:26:53 +00:00
let state = state.clone();
Response::builder().html(|o| templates::resume_html(o, Html(state.resume.clone())))
}
2020-07-12 23:37:01 +00:00
pub async fn signalboost(state: Arc<State>) -> Result<impl Reply, Rejection> {
2020-07-14 13:10:12 +00:00
HIT_COUNTER.with_label_values(&["signalboost"]).inc();
2020-07-12 23:37:01 +00:00
let state = state.clone();
Response::builder().html(|o| templates::signalboost_html(o, state.signalboost.clone()))
}
2020-07-13 00:51:58 +00:00
2020-07-13 02:58:00 +00:00
pub async fn not_found() -> Result<impl Reply, Rejection> {
2020-07-14 13:10:12 +00:00
HIT_COUNTER.with_label_values(&["not_found"]).inc();
2020-07-13 02:58:00 +00:00
Response::builder().html(|o| templates::notfound_html(o, "some path".into()))
2020-07-13 00:51:58 +00:00
}
2020-07-13 02:16:59 +00:00
2020-07-13 12:19:33 +00:00
pub mod blog;
2020-07-13 12:26:55 +00:00
pub mod gallery;
2020-07-14 13:10:12 +00:00
pub mod talks;
2020-07-13 11:54:17 +00:00
#[derive(Debug, thiserror::Error)]
struct PostNotFound(String, String);
impl fmt::Display for PostNotFound {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "not found: {}/{}", self.0, self.1)
}
}
impl warp::reject::Reject for PostNotFound {}
impl From<PostNotFound> for warp::reject::Rejection {
fn from(error: PostNotFound) -> Self {
warp::reject::custom(error)
}
}
#[derive(Debug, thiserror::Error)]
struct SeriesNotFound(String);
impl fmt::Display for SeriesNotFound {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl warp::reject::Reject for SeriesNotFound {}
impl From<SeriesNotFound> for warp::reject::Rejection {
fn from(error: SeriesNotFound) -> Self {
warp::reject::custom(error)
}
}
2020-07-14 13:10:12 +00:00
lazy_static! {
static ref REJECTION_COUNTER: IntCounterVec =
register_int_counter_vec!(opts!("rejections", "Number of rejections by kind"), &["kind"])
.unwrap();
}
2020-07-13 11:54:17 +00:00
pub async fn rejection(err: Rejection) -> Result<impl Reply, Infallible> {
let path: String;
let code;
if err.is_not_found() {
2020-07-14 13:10:12 +00:00
REJECTION_COUNTER.with_label_values(&["404"]).inc();
2020-07-13 11:54:17 +00:00
path = "".into();
code = StatusCode::NOT_FOUND;
} else if let Some(SeriesNotFound(series)) = err.find() {
2020-07-14 13:10:12 +00:00
REJECTION_COUNTER.with_label_values(&["SeriesNotFound"]).inc();
2020-07-13 11:54:17 +00:00
log::error!("invalid series {}", series);
path = format!("/blog/series/{}", series);
code = StatusCode::NOT_FOUND;
} else if let Some(PostNotFound(kind, name)) = err.find() {
2020-07-14 13:10:12 +00:00
REJECTION_COUNTER.with_label_values(&["PostNotFound"]).inc();
2020-07-13 11:54:17 +00:00
log::error!("unknown post {}/{}", kind, name);
path = format!("/{}/{}", kind, name);
code = StatusCode::NOT_FOUND;
} else {
2020-07-14 13:10:12 +00:00
REJECTION_COUNTER.with_label_values(&["Other"]).inc();
2020-07-13 11:54:17 +00:00
log::error!("unhandled rejection: {:?}", err);
2020-07-14 13:10:12 +00:00
path = format!("weird rejection: {:?}", err);
2020-07-13 11:54:17 +00:00
code = StatusCode::INTERNAL_SERVER_ERROR;
}
Ok(warp::reply::with_status(
Response::builder()
.html(|o| templates::notfound_html(o, path))
.unwrap(),
code,
))
2020-07-13 02:58:00 +00:00
}