Skip to content

Commit 17ff7b8

Browse files
feat: add health check endpoint (#2856)
1 parent 5ce342c commit 17ff7b8

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/core/http/request_handler.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,13 @@ async fn handle_request_inner<T: DeserializeOwned + GraphQLRequestLike>(
321321

322322
graphql_request::<T>(req, &Arc::new(app_ctx), req_counter).await
323323
}
324-
324+
hyper::Method::GET if req.uri().path() == "/status" => {
325+
let status_response = Response::builder()
326+
.status(StatusCode::OK)
327+
.header(CONTENT_TYPE, "application/json")
328+
.body(Body::from(r#"{"message": "ready"}"#))?;
329+
Ok(status_response)
330+
}
325331
hyper::Method::GET => {
326332
if let Some(TelemetryExporter::Prometheus(prometheus)) =
327333
app_ctx.blueprint.telemetry.export.as_ref()
@@ -330,7 +336,6 @@ async fn handle_request_inner<T: DeserializeOwned + GraphQLRequestLike>(
330336
return prometheus_metrics(prometheus);
331337
}
332338
};
333-
334339
not_found()
335340
}
336341
_ => not_found(),
@@ -377,6 +382,39 @@ pub async fn handle_request<T: DeserializeOwned + GraphQLRequestLike>(
377382

378383
#[cfg(test)]
379384
mod test {
385+
use super::*;
386+
use crate::core::async_graphql_hyper::GraphQLRequest;
387+
use crate::core::blueprint::Blueprint;
388+
use crate::core::config::{Config, ConfigModule};
389+
use crate::core::rest::EndpointSet;
390+
use crate::core::runtime::test::init;
391+
use crate::core::valid::Validator;
392+
393+
#[tokio::test]
394+
async fn test_health_endpoint() -> anyhow::Result<()> {
395+
let sdl = tokio::fs::read_to_string(tailcall_fixtures::configs::JSONPLACEHOLDER).await?;
396+
let config = Config::from_sdl(&sdl).to_result()?;
397+
let blueprint = Blueprint::try_from(&ConfigModule::from(config))?;
398+
let app_ctx = Arc::new(AppContext::new(
399+
blueprint,
400+
init(None),
401+
EndpointSet::default(),
402+
));
403+
404+
let req = Request::builder()
405+
.method(Method::GET)
406+
.uri("http://localhost:8000/status".to_string())
407+
.body(Body::empty())?;
408+
409+
let resp = handle_request::<GraphQLRequest>(req, app_ctx).await?;
410+
411+
assert_eq!(resp.status(), StatusCode::OK);
412+
let body = hyper::body::to_bytes(resp.into_body()).await?;
413+
assert_eq!(body, r#"{"message": "ready"}"#);
414+
415+
Ok(())
416+
}
417+
380418
#[test]
381419
fn test_create_allowed_headers() {
382420
use std::collections::BTreeSet;

0 commit comments

Comments
 (0)