-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice.rs
More file actions
37 lines (33 loc) · 1.06 KB
/
Copy pathservice.rs
File metadata and controls
37 lines (33 loc) · 1.06 KB
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
use axum::{
Router,
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
};
use init4_bin_base::deps::tracing::error;
use std::net::SocketAddr;
/// Return a 404 Not Found response
pub async fn return_404() -> Response {
(StatusCode::NOT_FOUND, "not found").into_response()
}
/// Return a 200 OK response
pub async fn return_200() -> Response {
(StatusCode::OK, "ok").into_response()
}
/// Serve a builder service on the given socket address.
pub fn serve_builder(socket: impl Into<SocketAddr>) -> tokio::task::JoinHandle<()> {
let router = Router::new().route("/healthcheck", get(return_200)).fallback(return_404);
let addr = socket.into();
tokio::spawn(async move {
match tokio::net::TcpListener::bind(&addr).await {
Ok(listener) => {
if let Err(err) = axum::serve(listener, router).await {
error!(%err, "serve failed");
}
}
Err(err) => {
error!(%err, "failed to bind to the address");
}
};
})
}