Skip to content

Commit e7e3572

Browse files
committed
Remove public /metrics endpoint from directory
Metrics collection is typically done on a private network address rather than one that is exposed to open internet. This removes the public /metrics endpoint and serves metrics requests separately on the --metrics-port if one was provided.
1 parent 0284a9d commit e7e3572

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

payjoin-directory/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ impl<D: Db> Service<D> {
187187
(Method::POST, ["", id]) => self.post_fallback_v1(id, query, body).await,
188188
(Method::GET, ["", "health"]) => health_check().await,
189189
(Method::GET, ["", ""]) => handle_directory_home_path().await,
190-
(Method::GET, ["", "metrics"]) => Ok(self.handle_metrics().await),
191190
_ => Ok(not_found()),
192191
}
193192
.unwrap_or_else(|e| e.to_response());
@@ -423,6 +422,10 @@ impl<D: Db> Service<D> {
423422
let io = TokioIo::new(stream);
424423
let service = self.clone();
425424
tokio::spawn(async move {
425+
let service = hyper::service::service_fn(move |req| {
426+
let this = service.clone();
427+
async move { this.serve_metrics_request(req).await }
428+
});
426429
if let Err(err) =
427430
http1::Builder::new().serve_connection(io, service).with_upgrades().await
428431
{
@@ -433,6 +436,22 @@ impl<D: Db> Service<D> {
433436

434437
Ok(())
435438
}
439+
440+
async fn serve_metrics_request(
441+
&self,
442+
req: Request<Incoming>,
443+
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, anyhow::Error> {
444+
let path = req.uri().path().to_string();
445+
let (parts, _body) = req.into_parts();
446+
447+
let path_segments: Vec<&str> = path.split('/').collect();
448+
let response = match (parts.method, path_segments.as_slice()) {
449+
(Method::GET, ["", ""]) => self.handle_metrics().await,
450+
_ => not_found(),
451+
};
452+
453+
Ok(response)
454+
}
436455
}
437456

438457
fn handle_peek<Error: db::SendableError>(

0 commit comments

Comments
 (0)