-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
145 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,51 @@ | ||
mod router; | ||
use anyhow::{Context, Result}; | ||
use axum::Router; | ||
use router::create_router; | ||
use std::net::SocketAddr; | ||
use std::time::Duration; | ||
use tokio::time::timeout; | ||
use tokio_util::sync::CancellationToken; | ||
use tracing::info; | ||
|
||
pub struct HttpServer; | ||
impl HttpServer { | ||
pub async fn run(cancellation_token: CancellationToken) -> Result<()> { | ||
let router = create_router()?; | ||
|
||
start_http_server(router, cancellation_token).await | ||
} | ||
} | ||
|
||
async fn start_http_server(router: Router, cancellation_token: CancellationToken) -> Result<()> { | ||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); | ||
let listener = tokio::net::TcpListener::bind(addr).await?; | ||
let token_clone = cancellation_token.clone(); | ||
let server_future = tokio::spawn(async { | ||
axum::serve(listener, router) | ||
.with_graceful_shutdown(shutdown_hook(token_clone)) | ||
.await | ||
.context("Failed to start HTTP server") | ||
}); | ||
|
||
await_shutdown(cancellation_token, server_future).await; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn await_shutdown( | ||
cancellation_token: CancellationToken, | ||
server_future: tokio::task::JoinHandle<Result<()>>, | ||
) { | ||
cancellation_token.cancelled().await; | ||
info!("Shutdown signal received."); | ||
match timeout(Duration::from_secs(5), server_future).await { | ||
Ok(_) => info!("HTTP service exited successfully."), | ||
Err(e) => info!("HTTP service exited after timeout: {}", e), | ||
} | ||
} | ||
|
||
async fn shutdown_hook(cancellation_token: CancellationToken) { | ||
cancellation_token.cancelled().await; | ||
info!("Exiting the process"); | ||
} |
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,38 @@ | ||
use anyhow::Result; | ||
use axum::{http::HeaderMap, response::Html}; | ||
use axum::{response::IntoResponse, routing::get, Router}; | ||
use std::time::Duration; | ||
use tower_http::trace::{DefaultMakeSpan, DefaultOnResponse, TraceLayer}; | ||
use tower_http::LatencyUnit; | ||
use tower_http::{timeout::TimeoutLayer, trace::DefaultOnFailure}; | ||
use tracing::Level; | ||
|
||
pub fn create_router() -> Result<Router> { | ||
let tracing_layer = TraceLayer::new_for_http() | ||
.make_span_with(DefaultMakeSpan::new().level(Level::INFO)) | ||
.on_response( | ||
DefaultOnResponse::new() | ||
.level(Level::INFO) | ||
.latency_unit(LatencyUnit::Millis), | ||
) | ||
.on_failure(DefaultOnFailure::new().level(Level::ERROR)); | ||
|
||
Ok(Router::new() | ||
.route("/", get(serve_root_page)) | ||
.layer(tracing_layer) | ||
.layer(TimeoutLayer::new(Duration::from_secs(1)))) | ||
} | ||
|
||
async fn serve_root_page(_headers: HeaderMap) -> impl IntoResponse { | ||
let body = " | ||
<html> | ||
<head> | ||
<title>Nos</title> | ||
</head> | ||
<body> | ||
<h1>Healthy</h1> | ||
</body> | ||
"; | ||
|
||
Html(body) | ||
} |
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