Delegate stub webserver to mod web.

Add prometheus monitoring.
This commit is contained in:
2020-02-09 21:56:41 -08:00
parent c71af2dc90
commit 88234c156c
4 changed files with 314 additions and 6 deletions

View File

@@ -17,6 +17,8 @@ use reqwest;
use structopt::StructOpt;
use yup_oauth2::{Authenticator, InstalledFlow};
mod web;
#[derive(Debug, StructOpt)]
enum Command {
/// List albums for the user of the given credentials. Optionally title filter.
@@ -285,8 +287,9 @@ fn list_albums(
})
.collect())
}
pub fn serve(addr: SocketAddr, root: PathBuf) -> Result<(), Box<dyn Error>> {
Ok(())
web::run(addr, root)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {

47
src/web.rs Normal file
View File

@@ -0,0 +1,47 @@
use std::error::Error;
use std::net::SocketAddr;
use std::path::PathBuf;
use prometheus::Encoder;
use warp;
use warp::http::header::{HeaderMap, HeaderValue};
use warp::reject::Rejection;
use warp::Filter;
fn metrics() -> impl Filter<Extract = (impl warp::reply::Reply,), Error = Rejection> + Clone {
let mut text_headers = HeaderMap::new();
text_headers.insert("content-type", HeaderValue::from_static("text/plain"));
warp::path("metrics")
.map(|| {
let mut buffer = Vec::new();
let encoder = prometheus::TextEncoder::new();
// Gather the metrics.
let metric_families = prometheus::gather();
// Encode them to send.
encoder.encode(&metric_families, &mut buffer).unwrap();
// TODO(wathiede): see if there's a wrapper like html()
buffer
})
.with(warp::reply::with::headers(text_headers))
}
fn index() -> Result<impl warp::Reply, warp::Rejection> {
Ok("Hello world")
}
pub fn run(addr: SocketAddr, root: PathBuf) -> Result<(), Box<dyn Error>> {
let index = warp::path::end().and_then(index);
// Fallback, always keep this last.
//let api = api.or(index);
let api = index;
let api = api.with(warp::log("moviewatcher"));
// We don't want metrics & heath checking filling up the logs, so we add this handler after
// wrapping with the log filter.
let routes = metrics().or(api);
warp::serve(routes).run(addr);
Ok(())
}