mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-02-02 16:43:34 +01:00
ffefd0caf2
implements: https://github.com/meilisearch/specifications/blob/develop/text/0085-api-keys.md - Add tests on API keys management route (meilisearch-http/tests/auth/api_keys.rs) - Add tests checking authorizations on each meilisearch routes (meilisearch-http/tests/auth/authorization.rs) - Implement API keys management routes (meilisearch-http/src/routes/api_key.rs) - Create module to manage API keys and authorizations (meilisearch-auth) - Reimplement GuardedData to extend authorizations (meilisearch-http/src/extractors/authentication/mod.rs) - Change X-MEILI-API-KEY by Authorization Bearer (meilisearch-http/src/extractors/authentication/mod.rs) - Change meilisearch routes to fit to the new authorization feature (meilisearch-http/src/routes/) - close #1867
168 lines
4.6 KiB
Rust
168 lines
4.6 KiB
Rust
use actix_web::{web, HttpResponse};
|
|
use chrono::{DateTime, Utc};
|
|
use log::debug;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use meilisearch_error::ResponseError;
|
|
use meilisearch_lib::index::{Settings, Unchecked};
|
|
use meilisearch_lib::MeiliSearch;
|
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
|
|
|
mod api_key;
|
|
mod dump;
|
|
pub mod indexes;
|
|
mod tasks;
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
cfg.service(web::scope("/tasks").configure(tasks::configure))
|
|
.service(web::resource("/health").route(web::get().to(get_health)))
|
|
.service(web::scope("/keys").configure(api_key::configure))
|
|
.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));
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[allow(clippy::large_enum_variant)]
|
|
#[serde(tag = "name")]
|
|
pub enum UpdateType {
|
|
ClearAll,
|
|
Customs,
|
|
DocumentsAddition {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
number: Option<usize>,
|
|
},
|
|
DocumentsPartial {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
number: Option<usize>,
|
|
},
|
|
DocumentsDeletion {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
number: Option<usize>,
|
|
},
|
|
Settings {
|
|
settings: Settings<Unchecked>,
|
|
},
|
|
}
|
|
|
|
#[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,
|
|
pub error: ResponseError,
|
|
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,
|
|
},
|
|
}
|
|
|
|
#[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 }
|
|
}
|
|
}
|
|
|
|
/// 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" }))
|
|
}
|
|
|
|
async fn get_stats(
|
|
meilisearch: GuardedData<ActionPolicy<{ actions::STATS_GET }>, MeiliSearch>,
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
let filters = meilisearch.filters();
|
|
|
|
let response = meilisearch.get_all_stats(&filters.indexes).await?;
|
|
|
|
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,
|
|
}
|
|
|
|
async fn get_version(
|
|
_meilisearch: GuardedData<ActionPolicy<{ actions::VERSION }>, MeiliSearch>,
|
|
) -> HttpResponse {
|
|
let commit_sha = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
|
|
let commit_date = option_env!("VERGEN_GIT_COMMIT_TIMESTAMP").unwrap_or("unknown");
|
|
|
|
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" })))
|
|
}
|