2021-07-05 14:29:20 +02:00
|
|
|
use actix_web::{web, HttpResponse};
|
2021-04-29 19:31:58 +02:00
|
|
|
use chrono::{DateTime, Utc};
|
2021-07-05 14:29:20 +02:00
|
|
|
use log::debug;
|
2020-12-12 13:32:06 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-12-02 16:03:26 +01:00
|
|
|
use meilisearch_error::ResponseError;
|
2021-09-21 13:23:22 +02:00
|
|
|
use meilisearch_lib::index::{Settings, Unchecked};
|
2021-12-02 16:03:26 +01:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2021-09-21 13:23:22 +02:00
|
|
|
|
2021-07-05 14:29:20 +02:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2021-04-29 19:31:58 +02:00
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
mod api_key;
|
2021-07-05 14:29:20 +02:00
|
|
|
mod dump;
|
2021-10-25 16:41:23 +02:00
|
|
|
pub mod indexes;
|
2021-12-02 16:03:26 +01:00
|
|
|
mod tasks;
|
2021-07-05 14:29:20 +02:00
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
2021-12-02 16:03:26 +01:00
|
|
|
cfg.service(web::scope("/tasks").configure(tasks::configure))
|
|
|
|
.service(web::resource("/health").route(web::get().to(get_health)))
|
2021-11-08 18:31:27 +01:00
|
|
|
.service(web::scope("/keys").configure(api_key::configure))
|
2021-07-05 14:29:20 +02:00
|
|
|
.service(web::scope("/dumps").configure(dump::configure))
|
|
|
|
.service(web::resource("/stats").route(web::get().to(get_stats)))
|
|
|
|
.service(web::resource("/version").route(web::get().to(get_version)))
|
|
|
|
.service(web::scope("/indexes").configure(indexes::configure));
|
|
|
|
}
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2021-04-29 19:31:58 +02:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-06-17 18:51:07 +02:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2021-04-29 19:31:58 +02:00
|
|
|
#[serde(tag = "name")]
|
|
|
|
pub enum UpdateType {
|
|
|
|
ClearAll,
|
|
|
|
Customs,
|
|
|
|
DocumentsAddition {
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-06-15 17:39:07 +02:00
|
|
|
number: Option<usize>,
|
2021-04-29 19:31:58 +02:00
|
|
|
},
|
|
|
|
DocumentsPartial {
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-06-15 17:39:07 +02:00
|
|
|
number: Option<usize>,
|
2021-04-29 19:31:58 +02:00
|
|
|
},
|
|
|
|
DocumentsDeletion {
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-06-15 17:39:07 +02:00
|
|
|
number: Option<usize>,
|
|
|
|
},
|
|
|
|
Settings {
|
|
|
|
settings: Settings<Unchecked>,
|
2021-04-29 19:31:58 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ProcessedUpdateResult {
|
|
|
|
pub update_id: u64,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub update_type: UpdateType,
|
|
|
|
pub duration: f64, // in seconds
|
|
|
|
pub enqueued_at: DateTime<Utc>,
|
|
|
|
pub processed_at: DateTime<Utc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct FailedUpdateResult {
|
|
|
|
pub update_id: u64,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub update_type: UpdateType,
|
2021-11-04 17:04:06 +01:00
|
|
|
pub error: ResponseError,
|
2021-04-29 19:31:58 +02:00
|
|
|
pub duration: f64, // in seconds
|
|
|
|
pub enqueued_at: DateTime<Utc>,
|
|
|
|
pub processed_at: DateTime<Utc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct EnqueuedUpdateResult {
|
|
|
|
pub update_id: u64,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub update_type: UpdateType,
|
|
|
|
pub enqueued_at: DateTime<Utc>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub started_processing_at: Option<DateTime<Utc>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", tag = "status")]
|
|
|
|
pub enum UpdateStatusResponse {
|
|
|
|
Enqueued {
|
|
|
|
#[serde(flatten)]
|
|
|
|
content: EnqueuedUpdateResult,
|
|
|
|
},
|
|
|
|
Processing {
|
|
|
|
#[serde(flatten)]
|
|
|
|
content: EnqueuedUpdateResult,
|
|
|
|
},
|
|
|
|
Failed {
|
|
|
|
#[serde(flatten)]
|
|
|
|
content: FailedUpdateResult,
|
|
|
|
},
|
|
|
|
Processed {
|
|
|
|
#[serde(flatten)]
|
|
|
|
content: ProcessedUpdateResult,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:32:06 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct IndexUpdateResponse {
|
|
|
|
pub update_id: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexUpdateResponse {
|
|
|
|
pub fn with_id(update_id: u64) -> Self {
|
|
|
|
Self { update_id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 11:34:54 +01:00
|
|
|
/// Always return a 200 with:
|
|
|
|
/// ```json
|
|
|
|
/// {
|
|
|
|
/// "status": "Meilisearch is running"
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub async fn running() -> HttpResponse {
|
|
|
|
HttpResponse::Ok().json(serde_json::json!({ "status": "MeiliSearch is running" }))
|
|
|
|
}
|
2021-07-05 14:29:20 +02:00
|
|
|
|
2021-09-28 22:22:59 +02:00
|
|
|
async fn get_stats(
|
2021-11-08 18:31:27 +01:00
|
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::STATS_GET }>, MeiliSearch>,
|
2021-09-28 22:22:59 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-11-08 18:31:27 +01:00
|
|
|
let filters = meilisearch.filters();
|
|
|
|
|
|
|
|
let response = meilisearch.get_all_stats(&filters.indexes).await?;
|
2021-07-05 14:29:20 +02:00
|
|
|
|
|
|
|
debug!("returns: {:?}", response);
|
|
|
|
Ok(HttpResponse::Ok().json(response))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct VersionResponse {
|
|
|
|
commit_sha: String,
|
|
|
|
commit_date: String,
|
|
|
|
pkg_version: String,
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
async fn get_version(
|
|
|
|
_meilisearch: GuardedData<ActionPolicy<{ actions::VERSION }>, MeiliSearch>,
|
|
|
|
) -> HttpResponse {
|
2021-08-30 17:41:24 +02:00
|
|
|
let commit_sha = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
|
|
|
|
let commit_date = option_env!("VERGEN_GIT_COMMIT_TIMESTAMP").unwrap_or("unknown");
|
2021-07-05 14:29:20 +02:00
|
|
|
|
|
|
|
HttpResponse::Ok().json(VersionResponse {
|
|
|
|
commit_sha: commit_sha.to_string(),
|
|
|
|
commit_date: commit_date.to_string(),
|
|
|
|
pkg_version: env!("CARGO_PKG_VERSION").to_string(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct KeysResponse {
|
|
|
|
private: Option<String>,
|
|
|
|
public: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_health() -> Result<HttpResponse, ResponseError> {
|
|
|
|
Ok(HttpResponse::Ok().json(serde_json::json!({ "status": "available" })))
|
|
|
|
}
|