diff options
author | metamuffin <metamuffin@disroot.org> | 2023-10-06 10:22:31 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-10-06 10:22:31 +0200 |
commit | 6e4095bd811a53cb75092516ef303b746e9aafba (patch) | |
tree | 44875ca252da14cc8018413ed2a25f99ed1fa06d /src/main.rs | |
parent | 452541087c6562fca94f24b9275b17d771622fbd (diff) | |
download | gnix-6e4095bd811a53cb75092516ef303b746e9aafba.tar gnix-6e4095bd811a53cb75092516ef303b746e9aafba.tar.bz2 gnix-6e4095bd811a53cb75092516ef303b746e9aafba.tar.zst |
some basic reporting for mond
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index 4d14bec..a1b04d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ pub mod error; pub mod files; pub mod helper; pub mod proxy; +#[cfg(feature = "mond")] +pub mod reporting; use crate::{ config::{Config, HostConfig}, @@ -26,6 +28,8 @@ use hyper::{ Request, Response, StatusCode, }; use log::{debug, error, info, warn}; +#[cfg(feature = "mond")] +use reporting::Reporting; use std::{fs::File, io::BufReader, net::SocketAddr, path::Path, process::exit, sync::Arc}; use tokio::{net::TcpListener, signal::ctrl_c, sync::Semaphore}; use tokio_rustls::TlsAcceptor; @@ -34,6 +38,8 @@ pub struct State { pub config: Config, pub l_incoming: Semaphore, pub l_outgoing: Semaphore, + #[cfg(feature = "mond")] + pub reporting: Reporting, } #[tokio::main] @@ -54,6 +60,8 @@ async fn main() -> anyhow::Result<()> { let state = Arc::new(State { l_incoming: Semaphore::new(config.limits.max_incoming_connections), l_outgoing: Semaphore::new(config.limits.max_outgoing_connections), + #[cfg(feature = "mond")] + reporting: Reporting::new(&config), config, }); @@ -204,18 +212,19 @@ async fn service( addr: SocketAddr, ) -> Result<hyper::Response<BoxBody<bytes::Bytes, ServiceError>>, ServiceError> { debug!("{addr} ~> {:?} {}", req.headers().get(HOST), req.uri()); + #[cfg(feature = "mond")] + state.reporting.request_in.inc(); - let route = state - .config - .hosts - .get(remove_port( - &req.headers() - .get(HOST) - .and_then(|e| e.to_str().ok()) - .map(String::from) - .unwrap_or(String::from("")), - )) - .ok_or(ServiceError::NoHost)?; + let host = req + .headers() + .get(HOST) + .and_then(|e| e.to_str().ok()) + .map(String::from) + .unwrap_or(String::from("")); + let host = remove_port(&host); + let route = state.config.hosts.get(host).ok_or(ServiceError::NoHost)?; + #[cfg(feature = "mond")] + state.reporting.hosts.get(host).unwrap().requests_in.inc(); let mut resp = match route { HostConfig::Backend { backend } => proxy_request(&state, req, addr, backend).await, |