Skip to content

Commit 2065ff8

Browse files
committed
feat: [torrust#508] add container healthcheck for API
1 parent 3141296 commit 2065ff8

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

Containerfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ COPY --from=build \
8585
RUN cargo nextest run --workspace-remap /test/src/ --extract-to /test/src/ --no-run --archive-file /test/torrust-tracker.tar.zst
8686
RUN cargo nextest run --workspace-remap /test/src/ --target-dir-remap /test/src/target/ --cargo-metadata /test/src/target/nextest/cargo-metadata.json --binaries-metadata /test/src/target/nextest/binaries-metadata.json
8787

88-
RUN mkdir -p /app/bin/; cp -l /test/src/target/release/torrust-tracker /app/bin/torrust-tracker
88+
RUN mkdir -p /app/bin/; cp -l /test/src/target/release/torrust-tracker /app/bin/torrust-tracker; cp -l /test/src/target/release/http_health_check /app/bin/http_health_check
8989
RUN mkdir -p /app/lib/; cp -l $(realpath $(ldd /app/bin/torrust-tracker | grep "libz\.so\.1" | awk '{print $3}')) /app/lib/libz.so.1
9090
RUN chown -R root:root /app; chmod -R u=rw,go=r,a+X /app; chmod -R a+x /app/bin
9191

@@ -136,5 +136,7 @@ CMD ["sh"]
136136
FROM runtime as release
137137
ENV RUNTIME="release"
138138
COPY --from=test /app/ /usr/
139-
# HEALTHCHECK CMD ["/usr/bin/wget", "--no-verbose", "--tries=1", "--spider", "localhost:${API_PORT}/version"]
139+
HEALTHCHECK --interval=5s --timeout=5s --start-period=3s --retries=3 \
140+
CMD /usr/bin/http_health_check http://localhost:${API_PORT}/health_check \
141+
|| exit 1
140142
CMD ["/usr/bin/torrust-tracker"]

src/bin/http_health_check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::{env, process};
1010
async fn main() {
1111
let args: Vec<String> = env::args().collect();
1212
if args.len() != 2 {
13-
eprintln!("Usage: cargo run --bin health_check <HEALTH_URL>");
14-
eprintln!("Example: cargo run --bin http_health_check http://localhost:1212/api/v1/stats?token=MyAccessToken");
13+
eprintln!("Usage: cargo run --bin http_health_check <HEALTH_URL>");
14+
eprintln!("Example: cargo run --bin http_health_check http://127.0.0.1:1212/health_check");
1515
std::process::exit(1);
1616
}
1717

@@ -34,4 +34,4 @@ async fn main() {
3434
process::exit(1);
3535
}
3636
}
37-
}
37+
}

src/servers/apis/routes.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//! first path segment. For example: `/api/v1/torrents`.
88
use std::sync::Arc;
99

10-
use axum::{middleware, Router};
10+
use axum::routing::get;
11+
use axum::{middleware, Json, Router};
12+
use serde_json::{json, Value};
1113
use tower_http::compression::CompressionLayer;
1214

1315
use super::v1;
@@ -18,14 +20,20 @@ use crate::tracker::Tracker;
1820
pub fn router(tracker: Arc<Tracker>) -> Router {
1921
let router = Router::new();
2022

21-
let prefix = "/api";
23+
let api_url_prefix = "/api";
2224

23-
let router = v1::routes::add(prefix, router, tracker.clone());
25+
let router = v1::routes::add(api_url_prefix, router, tracker.clone());
2426

2527
router
2628
.layer(middleware::from_fn_with_state(
2729
tracker.config.clone(),
2830
v1::middlewares::auth::auth,
2931
))
32+
.route("/health_check", get(health_check_handler))
3033
.layer(CompressionLayer::new())
3134
}
35+
36+
/// Endpoint for container health check.
37+
async fn health_check_handler() -> Json<Value> {
38+
Json(json!({ "status": "Ok" }))
39+
}

0 commit comments

Comments
 (0)