Skip to content
Snippets Groups Projects
Commit 33fd1286 authored by Stephen D's avatar Stephen D
Browse files

metric auth

parent 3b0b395d
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,11 @@ use std::fs;
use anyhow::anyhow;
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Metrics {
pub auth: String,
}
#[derive(Deserialize)]
pub struct Logo {
pub path: String,
......@@ -18,6 +23,7 @@ pub struct Config {
pub path: String,
pub root: String,
pub logo: Option<Logo>,
pub metrics: Metrics,
}
impl Config {
......
......@@ -61,21 +61,23 @@ async fn handle_request(
#[tokio::main]
async fn main() {
let config = Config::load().unwrap();
let metric_auth = config.metrics.auth.clone();
let timer = BUILD_BLOG_FROM_ASSETS.start_timer();
let blog: RenderedBlog = Blog::new(config).unwrap().try_into().unwrap();
let blog = Arc::new(RwLock::new(blog));
timer.observe_duration();
let metrics_filter = warp::path!("metrics").and_then(metrics::metrics_handler);
let metrics_path = warp::path!("metrics")
.and(warp::any().map(move || metric_auth.clone()))
.and(warp::query())
.and_then(metrics::metrics_handler);
let blog_filter = warp::any().map(move || blog.clone());
let blog_path = warp::filters::path::full()
.and(warp::any().map(move || blog.clone()))
.and_then(handle_request);
warp::serve(
metrics_filter.or(warp::filters::path::full()
.and(blog_filter.clone())
.and_then(handle_request)),
)
.run(([0, 0, 0, 0], 3030))
.await;
warp::serve(metrics_path.or(blog_path))
.run(([0, 0, 0, 0], 3030))
.await;
}
......@@ -2,6 +2,7 @@ use lazy_static::lazy_static;
use prometheus::{
register_counter, register_counter_vec, register_histogram, Counter, CounterVec, Histogram,
};
use serde::Deserialize;
use warp::{Rejection, Reply};
lazy_static! {
......@@ -24,7 +25,16 @@ lazy_static! {
.unwrap();
}
pub async fn metrics_handler() -> Result<impl Reply, Rejection> {
#[derive(Deserialize)]
pub struct AuthQuery {
auth: String,
}
pub async fn metrics_handler(pass: String, query: AuthQuery) -> Result<impl Reply, Rejection> {
if query.auth != pass {
return Ok("Go away".to_string());
}
use prometheus::Encoder;
let encoder = prometheus::TextEncoder::new();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment