Line data Source code
1 : use anyhow::{anyhow, bail};
2 : use hyper::{Body, Request, Response, StatusCode};
3 : use std::{convert::Infallible, net::TcpListener};
4 : use tracing::info;
5 : use utils::http::{endpoint, error::ApiError, json::json_response, RouterBuilder, RouterService};
6 :
7 23 : async fn status_handler(_: Request<Body>) -> Result<Response<Body>, ApiError> {
8 23 : json_response(StatusCode::OK, "")
9 23 : }
10 :
11 23 : fn make_router() -> RouterBuilder<hyper::Body, ApiError> {
12 23 : endpoint::make_router().get("/v1/status", status_handler)
13 23 : }
14 :
15 23 : pub async fn task_main(http_listener: TcpListener) -> anyhow::Result<Infallible> {
16 23 : scopeguard::defer! {
17 23 : info!("http has shut down");
18 23 : }
19 23 :
20 23 : let service = || RouterService::new(make_router().build()?);
21 23 :
22 23 : hyper::Server::from_tcp(http_listener)?
23 23 : .serve(service().map_err(|e| anyhow!(e))?)
24 24 : .await?;
25 :
26 0 : bail!("hyper server without shutdown handling cannot shutdown successfully");
27 0 : }
|