-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [#508] app health check endpoint checks API
The app health check endpoint checks is the API is running healthy when is enabled.
- Loading branch information
1 parent
e1a45a2
commit ef296f7
Showing
10 changed files
with
115 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::extract::State; | ||
use axum::Json; | ||
use torrust_tracker_configuration::Configuration; | ||
|
||
use super::resources::Report; | ||
use super::responses; | ||
|
||
/// Endpoint for container health check. | ||
pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>>) -> Json<Report> { | ||
if config.http_api.enabled { | ||
let health_check_url = format!("http://{}/health_check", config.http_api.bind_address); | ||
if !get_req_is_ok(&health_check_url).await { | ||
return responses::error(format!("API is not healthy. Health check endpoint: {health_check_url}")); | ||
} | ||
} | ||
|
||
// todo: for all HTTP Trackers, if enabled, check if is healthy | ||
|
||
// todo: for all UDP Trackers, if enabled, check if is healthy | ||
|
||
responses::ok() | ||
} | ||
|
||
async fn get_req_is_ok(url: &str) -> bool { | ||
match reqwest::get(url).await { | ||
Ok(response) => response.status().is_success(), | ||
Err(_err) => false, | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
pub mod handlers; | ||
pub mod resources; | ||
pub mod responses; | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub enum Status { | ||
Ok, | ||
Error, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub struct Report { | ||
pub status: Status, | ||
pub message: String, | ||
} | ||
|
||
impl Report { | ||
#[must_use] | ||
pub fn ok() -> Report { | ||
Self { | ||
status: Status::Ok, | ||
message: String::new(), | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn error(message: String) -> Report { | ||
Self { | ||
status: Status::Error, | ||
message, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use axum::Json; | ||
|
||
use super::resources::Report; | ||
|
||
pub fn ok() -> Json<Report> { | ||
Json(Report::ok()) | ||
} | ||
|
||
pub fn error(message: String) -> Json<Report> { | ||
Json(Report::error(message)) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
use torrust_tracker::servers::apis::v1::context::health_check::resources::{Report, Status}; | ||
use torrust_tracker::servers::health_check_api::resources::Report; | ||
use torrust_tracker_test_helpers::configuration; | ||
|
||
use crate::servers::health_check_api::client::get; | ||
use crate::servers::health_check_api::test_environment; | ||
|
||
#[tokio::test] | ||
async fn health_check_endpoint_should_return_status_ok() { | ||
let configuration = configuration::ephemeral(); | ||
async fn health_check_endpoint_should_return_status_ok_when_no_service_is_running() { | ||
let configuration = configuration::ephemeral_with_no_services(); | ||
|
||
let (bound_addr, test_env) = test_environment::start(&configuration.health_check_api).await; | ||
let (bound_addr, test_env) = test_environment::start(configuration.into()).await; | ||
|
||
let url = format!("http://{bound_addr}/health_check"); | ||
|
||
let response = get(&url).await; | ||
|
||
assert_eq!(response.status(), 200); | ||
assert_eq!(response.headers().get("content-type").unwrap(), "application/json"); | ||
assert_eq!(response.json::<Report>().await.unwrap(), Report { status: Status::Ok }); | ||
assert_eq!(response.json::<Report>().await.unwrap(), Report::ok()); | ||
|
||
test_env.abort(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters