darkwing/server/api/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
mod start_controller;
mod stop_controller;

use axum::{routing::*, Json};

use self::start_controller::StartController;
use self::stop_controller::StopController;

use super::{
  dtos::health_dto::{
    HealthResponse, ServiceStatus, ServiceStatuses, HEALTHCHECK_S3_KEY,
  },
  error::AppResult,
  extractors::ServiceProvider,
  services::cloud_file_services::S3ClientType,
};

pub async fn metrics(
  ServiceProvider(services): ServiceProvider,
) -> AppResult<String> {
  Ok(services.recorder_handle.render())
}

pub async fn health() -> AppResult<Json<HealthResponse>> {
  Ok(Json(HealthResponse::new(None, None)))
}

pub async fn full_health(
  ServiceProvider(services): ServiceProvider,
) -> AppResult<Json<HealthResponse>> {
  let primary_s3 = services
    .cloud_files
    .does_file_exist(HEALTHCHECK_S3_KEY, S3ClientType::Primary)
    .await;

  let primary_s3 = ServiceStatus {
    status: primary_s3.is_ok(),
    message: primary_s3.err().map(|e| e.to_string()),
  };

  let backup_s3 = services
    .cloud_files
    .does_file_exist(HEALTHCHECK_S3_KEY, S3ClientType::Backup)
    .await;

  let backup_s3 = ServiceStatus {
    status: backup_s3.is_ok(),
    message: backup_s3.err().map(|e| e.to_string()),
  };

  let s3_status = ServiceStatuses {
    primary_s3,
    backup_s3,
  };

  let health_response = services.status.get_health_status(s3_status).await?;

  Ok(Json(health_response))
}

pub fn app() -> Router {
  Router::new()
    .nest("/start", StartController::app())
    .nest("/stop", StopController::app())
    .route("/health", get(health))
    .route("/full-health", get(full_health))
}