meta_srv/service/admin/
health.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::HashMap;
16
17use tonic::codegen::http;
18
19use crate::error::Result;
20use crate::service::admin::HttpHandler;
21
22const HTTP_OK: &str = "OK\n";
23
24pub struct HealthHandler;
25
26#[async_trait::async_trait]
27impl HttpHandler for HealthHandler {
28    async fn handle(
29        &self,
30        _: &str,
31        _: http::Method,
32        _: &HashMap<String, String>,
33    ) -> Result<http::Response<String>> {
34        Ok(http::Response::builder()
35            .status(http::StatusCode::OK)
36            .body(HTTP_OK.to_owned())
37            .unwrap())
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[tokio::test]
46    async fn test_health_handle() {
47        let health_handler = HealthHandler {};
48        let path = "any";
49        let params = HashMap::default();
50        let res = health_handler
51            .handle(path, http::Method::GET, &params)
52            .await
53            .unwrap();
54
55        assert!(res.status().is_success());
56        assert_eq!(HTTP_OK.to_owned(), res.body().clone());
57    }
58}