meta_srv/service/admin/
recovery.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 axum::extract::State;
16use axum::response::{IntoResponse, Response};
17use axum::Json;
18use common_meta::key::runtime_switch::RuntimeSwitchManagerRef;
19use serde::{Deserialize, Serialize};
20
21use crate::service::admin::util::ErrorHandler;
22
23#[derive(Clone)]
24pub(crate) struct RecoveryHandler {
25    pub(crate) manager: RuntimeSwitchManagerRef,
26}
27
28#[derive(Debug, Serialize, Deserialize)]
29pub(crate) struct RecoveryResponse {
30    pub enabled: bool,
31}
32
33/// Get the recovery mode.
34#[axum_macros::debug_handler]
35pub(crate) async fn status(State(handler): State<RecoveryHandler>) -> Response {
36    handler
37        .manager
38        .recovery_mode()
39        .await
40        .map(|enabled| Json(RecoveryResponse { enabled }))
41        .map_err(ErrorHandler::new)
42        .into_response()
43}
44
45/// Set the recovery mode.
46#[axum_macros::debug_handler]
47pub(crate) async fn set(State(handler): State<RecoveryHandler>) -> Response {
48    handler
49        .manager
50        .set_recovery_mode()
51        .await
52        .map(|_| Json(RecoveryResponse { enabled: true }))
53        .map_err(ErrorHandler::new)
54        .into_response()
55}
56
57/// Unset the recovery mode.
58#[axum_macros::debug_handler]
59pub(crate) async fn unset(State(handler): State<RecoveryHandler>) -> Response {
60    handler
61        .manager
62        .unset_recovery_mode()
63        .await
64        .map(|_| Json(RecoveryResponse { enabled: false }))
65        .map_err(ErrorHandler::new)
66        .into_response()
67}