feat: Added a /health endpoint (#230)

This commit is contained in:
Maciej Hirsz
2020-02-18 13:59:12 +01:00
committed by GitHub
parent 483cf81c2d
commit ff29540d56
2 changed files with 33 additions and 3 deletions
+15
View File
@@ -131,10 +131,17 @@ pub struct NodeCount(pub ChainId, pub usize);
/// Message sent to the Aggregator to get the network state of a particular node
pub struct GetNetworkState(pub Box<str>, pub NodeId);
/// Message sent to the Aggregator to get a health check
pub struct GetHealth;
impl Message for GetNetworkState {
type Result = Option<Request<Chain, GetNodeNetworkState>>;
}
impl Message for GetHealth {
type Result = usize;
}
impl Handler<AddNode> for Aggregator {
type Result = ();
@@ -264,3 +271,11 @@ impl Handler<GetNetworkState> for Aggregator {
Some(self.get_chain(&*chain)?.addr.send(GetNodeNetworkState(nid)))
}
}
impl Handler<GetHealth> for Aggregator {
type Result = usize;
fn handle(&mut self, _: GetHealth, _: &mut Self::Context) -> Self::Result {
self.chains.len()
}
}
+18 -3
View File
@@ -17,7 +17,7 @@ mod util;
use node::connector::NodeConnector;
use feed::connector::FeedConnector;
use aggregator::{Aggregator, GetNetworkState};
use aggregator::{Aggregator, GetNetworkState, GetHealth};
use util::{Locator, LocatorFactory};
use types::NodeId;
@@ -77,6 +77,19 @@ fn state_route(
})
}
fn health(
aggregator: web::Data<Addr<Aggregator>>
) -> impl Future<Item = HttpResponse, Error = Error> {
aggregator
.send(GetHealth)
.from_err()
.and_then(|count| {
let body = format!("Connected chains: {}", count);
HttpResponse::Ok().body(body)
})
}
fn main() -> std::io::Result<()> {
use web::{resource, get};
@@ -86,9 +99,9 @@ fn main() -> std::io::Result<()> {
let aggregator = Aggregator::new().start();
let factory = LocatorFactory::new();
let locator = SyncArbiter::start(4, move || factory.create());
let port = std::env::var("PORT").ok().and_then(|v| v.parse().ok()).unwrap_or(8000u16);
HttpServer::new(move || {
App::new()
.data(aggregator.clone())
@@ -99,6 +112,8 @@ fn main() -> std::io::Result<()> {
.service(resource("/feed/").route(get().to(feed_route)))
.service(resource("/network_state/{chain}/{nid}").route(get().to_async(state_route)))
.service(resource("/network_state/{chain}/{nid}/").route(get().to_async(state_route)))
.service(resource("/health").route(get().to_async(health)))
.service(resource("/health/").route(get().to_async(health)))
})
.bind(format!("0.0.0.0:{}", port))?
.start();