-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move health endpoint-related code into a separate file (#344)
* chore: Move code related to the health endpoint into a separate file This is a first step towards #342. At this point HealthHandler merely encapsulates static methods. The idea is to have init() method also handle moving Settings::General::health to RequestHandlerOpts::health in future. Ideally, handling of the health setting CLI and config file will be added here as well (not trivial given how parsing is tied to data structures). * Fixed formatting * Fixed tests for experimental feature * Correctly declare imports for experimental feature as Unix-only * Drop HealthHandler struct, it isn't necessary now and might not be needed in future either * Moved initialization of RequestHandlerOpts::health into the health module * Align defaults for RequestHandlerOpts with CLI defaults * Improved comment * Don't expose the health module publicly --------- Co-authored-by: Jose Quintana <1700322+joseluisq@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
243 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// This file is part of Static Web Server. | ||
// See https://static-web-server.net/ for more information | ||
// Copyright (C) 2019-present Jose Quintana <joseluisq.net> | ||
|
||
//! Module providing the health endpoint. | ||
//! | ||
|
||
use headers::{ContentType, HeaderMapExt}; | ||
use hyper::{Body, Request, Response}; | ||
|
||
use crate::{handler::RequestHandlerOpts, http_ext::MethodExt, server_info, Error}; | ||
|
||
/// Initializes the health endpoint. | ||
pub fn init(enabled: bool, handler_opts: &mut RequestHandlerOpts) { | ||
handler_opts.health = enabled; | ||
server_info!("health endpoint: enabled={enabled}"); | ||
} | ||
|
||
/// Handles health requests | ||
pub fn pre_process( | ||
opts: &RequestHandlerOpts, | ||
req: &Request<Body>, | ||
remote_addr_str: &str, | ||
) -> Option<Result<Response<Body>, Error>> { | ||
if !opts.health { | ||
return None; | ||
} | ||
|
||
let uri = req.uri(); | ||
if uri.path() != "/health" { | ||
return None; | ||
} | ||
|
||
let method = req.method(); | ||
if !method.is_get() && !method.is_head() { | ||
return None; | ||
} | ||
|
||
tracing::debug!( | ||
"incoming request: method={} uri={}{}", | ||
method, | ||
uri, | ||
remote_addr_str, | ||
); | ||
|
||
let body = if method.is_get() { | ||
Body::from("OK") | ||
} else { | ||
Body::empty() | ||
}; | ||
|
||
let mut resp = Response::new(body); | ||
resp.headers_mut().typed_insert(ContentType::html()); | ||
Some(Ok(resp)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::pre_process; | ||
use crate::handler::RequestHandlerOpts; | ||
use hyper::{Body, Request}; | ||
|
||
fn make_request(method: &str, uri: &str) -> Request<Body> { | ||
Request::builder() | ||
.method(method) | ||
.uri(uri) | ||
.body(Body::empty()) | ||
.unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_health_disabled() { | ||
assert!(pre_process( | ||
&RequestHandlerOpts { | ||
health: false, | ||
..Default::default() | ||
}, | ||
&make_request("GET", "/health"), | ||
"" | ||
) | ||
.is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_wrong_uri() { | ||
assert!(pre_process( | ||
&RequestHandlerOpts { | ||
health: true, | ||
..Default::default() | ||
}, | ||
&make_request("GET", "/health2"), | ||
"" | ||
) | ||
.is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_wrong_method() { | ||
assert!(pre_process( | ||
&RequestHandlerOpts { | ||
health: true, | ||
..Default::default() | ||
}, | ||
&make_request("POST", "/health"), | ||
"" | ||
) | ||
.is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_correct_request() { | ||
assert!(pre_process( | ||
&RequestHandlerOpts { | ||
health: true, | ||
..Default::default() | ||
}, | ||
&make_request("GET", "/health"), | ||
"" | ||
) | ||
.is_some()); | ||
} | ||
} |
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
Oops, something went wrong.