forked from plabayo/rama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_k8s_health.rs
52 lines (50 loc) · 1.62 KB
/
http_k8s_health.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! An example to show how to create a k8s health check server,
//! using the [`HttpServer`] and [`Executor`] from Rama.
//!
//! [`HttpServer`]: crate::http::server::HttpServer
//! [`Executor`]: crate::rt::Executor
//!
//! This example will create a server that listens on `127.0.0.1:8080.
//!
//! # Run the example
//!
//! ```sh
//! cargo run --example http_k8s_health
//! ```
//!
//! # Expected output
//!
//! The server will start and listen on `:8080`. You can use `curl` to check if the server is ready:
//!
//! ```sh
//! curl -v http://127.0.0.1:8080/k8s/ready
//! ```
//!
//! You should see a response with `HTTP/1.1 503 Service Unavailable` and an empty body.
//! When running that same curl command, at least 10 seconds after your started the service,
//! you should see a response with `HTTP/1.1 200 OK` and an empty body.
use rama::{
http::{server::HttpServer, service::web::k8s_health_builder},
rt::Executor,
};
#[tokio::main]
async fn main() {
let exec = Executor::default();
let startup_time = std::time::Instant::now();
HttpServer::auto(exec)
.listen(
"127.0.0.1:8080",
// by default the k8s health service is always ready and alive,
// optionally you can define your own conditional closures to define
// more accurate health checks
k8s_health_builder()
.ready(move || {
// simulate a service only ready after 10s for w/e reason
let uptime = startup_time.elapsed().as_secs();
uptime > 10
})
.build(),
)
.await
.unwrap();
}