2020-05-13 16:38:16 -04:00
|
|
|
use std::convert::Infallible;
|
2020-05-15 09:16:16 -04:00
|
|
|
use pfacts::Facts;
|
2020-05-13 16:38:16 -04:00
|
|
|
use rand::prelude::*;
|
|
|
|
use warp::Filter;
|
|
|
|
|
2020-05-15 09:16:16 -04:00
|
|
|
async fn give_fact(facts: Facts) -> Result<String, Infallible> {
|
2020-05-13 16:38:16 -04:00
|
|
|
Ok(facts.choose(&mut thread_rng()).unwrap().clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2020-07-09 17:07:01 -04:00
|
|
|
pretty_env_logger::init();
|
2020-05-15 09:16:16 -04:00
|
|
|
let facts = pfacts::make();
|
2020-05-13 16:38:16 -04:00
|
|
|
|
|
|
|
let mw = warp::any().map(move || facts.clone());
|
|
|
|
|
|
|
|
let fact_handler = warp::any()
|
|
|
|
.and(mw)
|
|
|
|
.and_then(give_fact);
|
|
|
|
|
2020-07-09 17:07:01 -04:00
|
|
|
log::info!("listening on port 5000");
|
2020-05-13 16:38:16 -04:00
|
|
|
warp::serve(fact_handler)
|
|
|
|
.run(([0, 0, 0, 0], 5000))
|
|
|
|
.await;
|
|
|
|
}
|