printerfacts/src/main.rs

77 lines
2.2 KiB
Rust
Raw Normal View History

2020-07-16 20:10:45 +00:00
use crate::templates::RenderRucte;
use lazy_static::lazy_static;
2020-05-15 13:16:16 +00:00
use pfacts::Facts;
2020-07-16 20:10:45 +00:00
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
2020-05-13 20:38:16 +00:00
use rand::prelude::*;
2020-07-16 20:10:45 +00:00
use std::convert::Infallible;
use warp::{http::Response, Filter, Rejection, Reply};
2020-05-13 20:38:16 +00:00
2020-07-16 20:10:45 +00:00
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
const APPLICATION_NAME: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
lazy_static! {
static ref HIT_COUNTER: IntCounterVec =
register_int_counter_vec!(opts!("hits", "Number of hits to various pages"), &["page"])
.unwrap();
2020-05-13 20:38:16 +00:00
}
2020-07-16 20:10:45 +00:00
async fn give_fact(facts: Facts) -> Result<String, Infallible> {
HIT_COUNTER.with_label_values(&["fact"]).inc();
Ok(facts.choose(&mut thread_rng()).unwrap().clone())
2020-07-09 22:19:03 +00:00
}
2020-07-16 20:10:45 +00:00
async fn index(facts: Facts) -> Result<impl Reply, Rejection> {
HIT_COUNTER.with_label_values(&["index"]).inc();
Response::builder()
.html(|o| templates::index_html(o, facts.choose(&mut thread_rng()).unwrap().clone()))
2020-07-09 22:19:03 +00:00
}
2020-07-16 20:10:45 +00:00
async fn not_found() -> Result<impl Reply, Rejection> {
HIT_COUNTER.with_label_values(&["not_found"]).inc();
Response::builder()
.status(404)
.html(|o| templates::not_found_html(o))
2020-07-09 22:19:03 +00:00
}
2020-05-13 20:38:16 +00:00
#[tokio::main]
2020-07-09 22:19:03 +00:00
async fn main() -> anyhow::Result<()> {
2020-07-09 21:07:01 +00:00
pretty_env_logger::init();
2020-05-15 13:16:16 +00:00
let facts = pfacts::make();
2020-07-09 22:19:03 +00:00
let fact = {
let facts = facts.clone();
warp::any().map(move || facts.clone())
};
2020-05-13 20:38:16 +00:00
2020-07-09 22:19:03 +00:00
let files = warp::path("static").and(warp::fs::dir("./static"));
2020-05-13 20:38:16 +00:00
2020-07-09 22:19:03 +00:00
let fact_handler = warp::get()
.and(warp::path("fact"))
2020-07-16 20:10:45 +00:00
.and(fact.clone())
2020-05-13 20:38:16 +00:00
.and_then(give_fact);
2020-07-09 22:19:03 +00:00
let index_handler = warp::get()
.and(warp::path::end())
2020-07-16 20:10:45 +00:00
.and(fact.clone())
.and_then(index);
2020-07-09 22:19:03 +00:00
2020-07-16 20:10:45 +00:00
let not_found_handler = warp::any().and_then(not_found);
let port = std::env::var("PORT")
.unwrap_or("5000".into())
.parse::<u16>()
.expect("PORT to be a string-encoded u16");
2020-07-09 22:19:03 +00:00
log::info!("listening on port {}", port);
2020-07-09 22:19:03 +00:00
warp::serve(
fact_handler
.or(index_handler)
.or(files)
2020-07-16 20:10:45 +00:00
.or(not_found_handler)
.with(warp::log(APPLICATION_NAME)),
2020-07-09 22:19:03 +00:00
)
.run(([0, 0, 0, 0], port))
2020-07-09 22:19:03 +00:00
.await;
Ok(())
2020-05-13 20:38:16 +00:00
}