Delegate stub webserver to mod web.

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

266
Cargo.lock generated
View File

@ -89,6 +89,16 @@ dependencies = [
"byte-tools", "byte-tools",
] ]
[[package]]
name = "buf_redux"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b953a6887648bb07a535631f2bc00fbdb2a2216f135552cb3f534ed136b9c07f"
dependencies = [
"memchr",
"safemem",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.2.0" version = "3.2.0"
@ -442,7 +452,7 @@ dependencies = [
"chrono", "chrono",
"google_api_auth", "google_api_auth",
"google_field_selector", "google_field_selector",
"mime", "mime 0.3.16",
"percent-encoding 2.1.0", "percent-encoding 2.1.0",
"reqwest", "reqwest",
"serde", "serde",
@ -515,6 +525,32 @@ dependencies = [
"tokio-util", "tokio-util",
] ]
[[package]]
name = "headers"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "882ca7d8722f33ce2c2db44f95425d6267ed59ca96ce02acbe58320054ceb642"
dependencies = [
"base64 0.10.1",
"bitflags",
"bytes 0.4.12",
"headers-core",
"http 0.1.21",
"mime 0.3.16",
"sha-1",
"time",
]
[[package]]
name = "headers-core"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "967131279aaa9f7c20c7205b45a391638a83ab118e6509b2d0ccbe08de044237"
dependencies = [
"bytes 0.4.12",
"http 0.1.21",
]
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.3.1" version = "0.3.1"
@ -724,6 +760,15 @@ dependencies = [
"autocfg 1.0.0", "autocfg 1.0.0",
] ]
[[package]]
name = "input_buffer"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e1b822cc844905551931d6f81608ed5f50a79c1078a4e2b4d42dbc7c1eedfbf"
dependencies = [
"bytes 0.4.12",
]
[[package]] [[package]]
name = "iovec" name = "iovec"
version = "0.1.4" version = "0.1.4"
@ -839,20 +884,41 @@ dependencies = [
"rustc_version", "rustc_version",
] ]
[[package]]
name = "mime"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0"
dependencies = [
"log 0.3.9",
]
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.3.16" version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "mime_guess"
version = "1.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d977de9ee851a0b16e932979515c0f3da82403183879811bc97d50bd9cc50f7"
dependencies = [
"mime 0.2.6",
"phf",
"phf_codegen",
"unicase 1.4.2",
]
[[package]] [[package]]
name = "mime_guess" name = "mime_guess"
version = "2.0.1" version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a0ed03949aef72dbdf3116a383d7b38b4768e6f960528cd6a6044aa9ed68599" checksum = "1a0ed03949aef72dbdf3116a383d7b38b4768e6f960528cd6a6044aa9ed68599"
dependencies = [ dependencies = [
"mime", "mime 0.3.16",
"unicase", "unicase 2.6.0",
] ]
[[package]] [[package]]
@ -897,6 +963,24 @@ dependencies = [
"ws2_32-sys", "ws2_32-sys",
] ]
[[package]]
name = "multipart"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "136eed74cadb9edd2651ffba732b19a450316b680e4f48d6c79e905799e19d01"
dependencies = [
"buf_redux",
"httparse",
"log 0.4.8",
"mime 0.2.6",
"mime_guess 1.8.7",
"quick-error",
"rand 0.6.5",
"safemem",
"tempfile",
"twoway",
]
[[package]] [[package]]
name = "native-tls" name = "native-tls"
version = "0.2.3" version = "0.2.3"
@ -1048,6 +1132,45 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "phf"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3da44b85f8e8dfaec21adae67f95d93244b2ecf6ad2a692320598dcc8e6dd18"
dependencies = [
"phf_shared",
]
[[package]]
name = "phf_codegen"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b03e85129e324ad4166b06b2c7491ae27fe3ec353af72e72cd1654c7225d517e"
dependencies = [
"phf_generator",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662"
dependencies = [
"phf_shared",
"rand 0.6.5",
]
[[package]]
name = "phf_shared"
version = "0.7.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0"
dependencies = [
"siphasher",
"unicase 1.4.2",
]
[[package]] [[package]]
name = "photosync" name = "photosync"
version = "0.1.0" version = "0.1.0"
@ -1057,11 +1180,13 @@ dependencies = [
"hexihasher", "hexihasher",
"lazy_static 1.4.0", "lazy_static 1.4.0",
"log 0.4.8", "log 0.4.8",
"prometheus",
"regex", "regex",
"reqwest", "reqwest",
"serde_json", "serde_json",
"stderrlog", "stderrlog",
"structopt", "structopt",
"warp",
"yup-oauth2", "yup-oauth2",
] ]
@ -1161,6 +1286,32 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "prometheus"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5567486d5778e2c6455b1b90ff1c558f29e751fc018130fa182e15828e728af1"
dependencies = [
"cfg-if",
"fnv",
"lazy_static 1.4.0",
"protobuf",
"quick-error",
"spin",
]
[[package]]
name = "protobuf"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6686ddd96a8dbe2687b5f2a687b2cfb520854010ec480f2d74c32e7c9873d3c5"
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.2" version = "1.0.2"
@ -1387,8 +1538,8 @@ dependencies = [
"js-sys", "js-sys",
"lazy_static 1.4.0", "lazy_static 1.4.0",
"log 0.4.8", "log 0.4.8",
"mime", "mime 0.3.16",
"mime_guess", "mime_guess 2.0.1",
"native-tls", "native-tls",
"percent-encoding 2.1.0", "percent-encoding 2.1.0",
"pin-project-lite", "pin-project-lite",
@ -1474,6 +1625,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" checksum = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
[[package]]
name = "safemem"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072"
[[package]] [[package]]
name = "schannel" name = "schannel"
version = "0.1.17" version = "0.1.17"
@ -1484,6 +1641,12 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "scoped-tls"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.0.0" version = "1.0.0"
@ -1590,6 +1753,18 @@ dependencies = [
"url 2.1.1", "url 2.1.1",
] ]
[[package]]
name = "sha-1"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df"
dependencies = [
"block-buffer",
"digest",
"fake-simd",
"opaque-debug",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.8.1" version = "0.8.1"
@ -1602,6 +1777,12 @@ dependencies = [
"opaque-debug", "opaque-debug",
] ]
[[package]]
name = "siphasher"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac"
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.2" version = "0.4.2"
@ -2069,12 +2250,49 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382"
[[package]]
name = "tungstenite"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a0c2bd5aeb7dcd2bb32e472c8872759308495e5eccc942e929a513cd8d36110"
dependencies = [
"base64 0.11.0",
"byteorder",
"bytes 0.4.12",
"http 0.1.21",
"httparse",
"input_buffer",
"log 0.4.8",
"rand 0.7.3",
"sha-1",
"url 2.1.1",
"utf-8",
]
[[package]]
name = "twoway"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59b11b2b5241ba34be09c3cc85a36e56e48f9888862e19cedf23336d35316ed1"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.11.2" version = "1.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" checksum = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9"
[[package]]
name = "unicase"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33"
dependencies = [
"version_check 0.1.5",
]
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "2.6.0" version = "2.6.0"
@ -2157,6 +2375,18 @@ dependencies = [
"percent-encoding 2.1.0", "percent-encoding 2.1.0",
] ]
[[package]]
name = "urlencoding"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3df3561629a8bb4c57e5a2e4c43348d9e29c7c29d9b1c4c1f47166deca8f37ed"
[[package]]
name = "utf-8"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7"
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.8" version = "0.2.8"
@ -2208,6 +2438,32 @@ dependencies = [
"try-lock", "try-lock",
] ]
[[package]]
name = "warp"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3921463c44f680d24f1273ea55efd985f31206a22a02dee207a2ec72684285ca"
dependencies = [
"bytes 0.4.12",
"futures",
"headers",
"http 0.1.21",
"hyper 0.12.35",
"log 0.4.8",
"mime 0.3.16",
"mime_guess 2.0.1",
"multipart",
"scoped-tls",
"serde",
"serde_json",
"serde_urlencoded",
"tokio 0.1.22",
"tokio-io",
"tokio-threadpool",
"tungstenite",
"urlencoding",
]
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.9.0+wasi-snapshot-preview1" version = "0.9.0+wasi-snapshot-preview1"

View File

@ -19,3 +19,5 @@ serde_json = "1.0.46"
stderrlog = "0.4.3" stderrlog = "0.4.3"
structopt = "0.3.9" structopt = "0.3.9"
yup-oauth2 = "^3.1" yup-oauth2 = "^3.1"
warp = "0.1"
prometheus = "0.7.0"

View File

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