use std::env;
use actix_web::{get, web, HttpResponse};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct SystemInfo {
  version: String,
  healthy: bool,
}

#[get("/info")]
async fn info() -> HttpResponse {
  let mut healthy = true;
  let version = match env::var("CARGO_PKG_VERSION") {
    Ok(v) => v,
    Err(_) => {
      healthy = false;
      String::from("unknown")
    }
  };

  let info = SystemInfo { version, healthy };

  HttpResponse::Ok().json(info)
}

pub fn init_routes(config: &mut web::ServiceConfig) {
  config.service(web::scope("/system").service(info));
}
