2021-03-15 18:11:10 +01:00
|
|
|
use actix_web::get;
|
2020-12-12 13:32:06 +01:00
|
|
|
use actix_web::web;
|
|
|
|
use actix_web::HttpResponse;
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2020-12-22 14:02:41 +01:00
|
|
|
use crate::error::ResponseError;
|
2020-12-12 13:32:06 +01:00
|
|
|
use crate::helpers::Authentication;
|
|
|
|
use crate::routes::IndexParam;
|
|
|
|
use crate::Data;
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
2021-04-01 16:44:42 +02:00
|
|
|
cfg.service(get_index_stats)
|
2020-12-12 13:32:06 +01:00
|
|
|
.service(get_stats)
|
|
|
|
.service(get_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/stats", wrap = "Authentication::Private")]
|
2021-04-01 16:44:42 +02:00
|
|
|
async fn get_index_stats(
|
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-04-14 18:55:04 +02:00
|
|
|
let response = data.get_index_stats(path.index_uid.clone()).await?;
|
2021-04-01 16:44:42 +02:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(response))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/stats", wrap = "Authentication::Private")]
|
2021-04-01 16:44:42 +02:00
|
|
|
async fn get_stats(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
2021-04-14 18:55:04 +02:00
|
|
|
let response = data.get_all_stats().await?;
|
2021-04-01 16:44:42 +02:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(response))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct VersionResponse {
|
|
|
|
commit_sha: String,
|
2021-04-09 08:03:25 +02:00
|
|
|
commit_date: String,
|
2020-12-12 13:32:06 +01:00
|
|
|
pkg_version: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/version", wrap = "Authentication::Private")]
|
|
|
|
async fn get_version() -> HttpResponse {
|
2021-03-30 19:03:13 +02:00
|
|
|
let commit_sha = match option_env!("COMMIT_SHA") {
|
|
|
|
Some("") | None => env!("VERGEN_SHA"),
|
2021-04-01 16:44:42 +02:00
|
|
|
Some(commit_sha) => commit_sha,
|
2021-03-30 19:03:13 +02:00
|
|
|
};
|
|
|
|
let commit_date = match option_env!("COMMIT_DATE") {
|
|
|
|
Some("") | None => env!("VERGEN_COMMIT_DATE"),
|
2021-04-01 16:44:42 +02:00
|
|
|
Some(commit_date) => commit_date,
|
2021-03-30 19:03:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
HttpResponse::Ok().json(VersionResponse {
|
|
|
|
commit_sha: commit_sha.to_string(),
|
2021-04-09 08:03:25 +02:00
|
|
|
commit_date: commit_date.to_string(),
|
2021-03-15 19:08:19 +01:00
|
|
|
pkg_version: env!("CARGO_PKG_VERSION").to_string(),
|
|
|
|
})
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|