Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions payjoin-directory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<D: Db> Service<D> {
(Method::POST, ["", ""]) => self.handle_ohttp_gateway(body).await,
(Method::GET, ["", "ohttp-keys"]) => self.get_ohttp_keys().await,
(Method::POST, ["", id]) => self.handle_post_v1(id, query, body).await,
(Method::GET, ["", "health"]) => health_check().await,
(Method::GET, ["", "health"]) => self.health_check().await,
(Method::GET, ["", ""]) => handle_directory_home_path().await,
_ => Ok(not_found()),
}
Expand Down Expand Up @@ -577,8 +577,14 @@ fn handle_peek<Error: db::SendableError>(
}
}

async fn health_check() -> Result<Response<BoxBody<Bytes, hyper::Error>>, HandlerError> {
Ok(Response::new(empty()))
impl<D: Db> Service<D> {
async fn health_check(&self) -> Result<Response<BoxBody<Bytes, hyper::Error>>, HandlerError> {
let versions = if self.v1.is_some() { "[1,2]" } else { "[2]" };
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be more descriptive if we had the bip numbers? if 1 and 2 are mentioned elsewhere in the docs and devs know what they means lets role with that.

Suggested change
let versions = if self.v1.is_some() { "[1,2]" } else { "[2]" };
let versions = if self.v1.is_some() { "[bip78,bip77]" } else { "[bip77]" };

Copy link
Copy Markdown
Collaborator Author

@spacebear21 spacebear21 Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the convention from the BIP78 error message for unsupported versions https://github.com/bitcoin/bips/blob/master/bip-0078.mediawiki#optional-parameters
I think using bip numbers everywhere would be more descriptive but we may have too much lock in with version numbers already (pj uri parameter, error responses, payjoin-cli and mailroom config...)

let body = format!(r#"{{"versions":{versions}}}"#);
let mut res = Response::new(full(body));
res.headers_mut().insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
Ok(res)
}
}

async fn handle_directory_home_path() -> Result<Response<BoxBody<Bytes, hyper::Error>>, HandlerError>
Expand Down Expand Up @@ -962,4 +968,38 @@ mod tests {
let psbt_b64 = make_test_psbt_base64(addr);
assert!(matches!(screen_v1_addresses(&psbt_b64, &blocked), ScreenResult::Blocked));
}

// Health check

#[tokio::test]
async fn health_check_without_v1() {
let mut svc = test_service(None).await;
let req = Request::builder()
.method(Method::GET)
.uri("http://localhost/health")
.body(Full::new(Bytes::new()))
.unwrap();

let res = tower::Service::call(&mut svc, req).await.unwrap();
assert_eq!(res.headers().get(CONTENT_TYPE).unwrap(), "application/json");
let (status, body) = collect_body(res).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body, r#"{"versions":[2]}"#);
}

#[tokio::test]
async fn health_check_with_v1() {
let mut svc = test_service(Some(V1::new(None))).await;
let req = Request::builder()
.method(Method::GET)
.uri("http://localhost/health")
.body(Full::new(Bytes::new()))
.unwrap();

let res = tower::Service::call(&mut svc, req).await.unwrap();
assert_eq!(res.headers().get(CONTENT_TYPE).unwrap(), "application/json");
let (status, body) = collect_body(res).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body, r#"{"versions":[1,2]}"#);
}
}
Loading