Delegate stub webserver to mod web.
Add prometheus monitoring.
This commit is contained in:
@@ -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
47
src/web.rs
Normal 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user