meilisearch/meilisearch-http/src/routes/indexes/mod.rs

117 lines
3.5 KiB
Rust
Raw Normal View History

2020-12-12 20:32:06 +08:00
use actix_web::{web, HttpResponse};
2021-05-11 02:24:14 +08:00
use chrono::{DateTime, Utc};
2021-06-23 18:18:34 +08:00
use log::debug;
2021-05-31 22:03:39 +08:00
use serde::{Deserialize, Serialize};
2020-12-12 20:32:06 +08:00
2020-12-22 21:02:41 +08:00
use crate::error::ResponseError;
2021-06-24 21:33:21 +08:00
use crate::extractors::authentication::{policies::*, GuardedData};
2021-07-05 20:29:20 +08:00
use crate::routes::IndexParam;
2021-03-16 01:11:10 +08:00
use crate::Data;
2020-12-12 20:32:06 +08:00
2021-07-05 20:29:20 +08:00
mod documents;
mod search;
mod settings;
mod updates;
pub fn configure(cfg: &mut web::ServiceConfig) {
2021-06-24 21:33:21 +08:00
cfg.service(
2021-07-05 20:29:20 +08:00
web::resource("")
2021-06-24 21:33:21 +08:00
.route(web::get().to(list_indexes))
.route(web::post().to(create_index)),
)
.service(
2021-07-05 20:29:20 +08:00
web::scope("/{index_uid}")
.service(
web::resource("")
.route(web::get().to(get_index))
.route(web::put().to(update_index))
.route(web::delete().to(delete_index)),
)
.service(web::resource("/stats").route(web::get().to(get_index_stats)))
.service(web::scope("/documents").configure(documents::configure))
.service(web::scope("/search").configure(search::configure))
.service(web::scope("/updates").configure(updates::configure))
.service(web::scope("/settings").configure(settings::configure)),
2021-06-24 21:33:21 +08:00
);
2020-12-12 20:32:06 +08:00
}
2021-07-05 20:29:20 +08:00
async fn list_indexes(data: GuardedData<Private, Data>) -> Result<HttpResponse, ResponseError> {
let indexes = data.list_indexes().await?;
debug!("returns: {:?}", indexes);
Ok(HttpResponse::Ok().json(indexes))
}
2020-12-12 20:32:06 +08:00
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct IndexCreateRequest {
2021-02-19 00:48:37 +08:00
uid: String,
2020-12-12 20:32:06 +08:00
primary_key: Option<String>,
}
2021-07-05 20:29:20 +08:00
async fn create_index(
data: GuardedData<Private, Data>,
body: web::Json<IndexCreateRequest>,
) -> Result<HttpResponse, ResponseError> {
let body = body.into_inner();
let meta = data.create_index(body.uid, body.primary_key).await?;
2021-07-05 16:10:17 +08:00
Ok(HttpResponse::Created().json(meta))
2021-07-05 20:29:20 +08:00
}
2020-12-12 20:32:06 +08:00
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateIndexRequest {
2021-03-15 21:43:47 +08:00
uid: Option<String>,
2020-12-12 20:32:06 +08:00
primary_key: Option<String>,
}
2021-04-28 22:43:49 +08:00
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateIndexResponse {
name: String,
uid: String,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
primary_key: Option<String>,
}
2021-07-05 16:10:17 +08:00
2021-06-24 21:33:21 +08:00
async fn get_index(
data: GuardedData<Private, Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let meta = data.index(path.index_uid.clone()).await?;
debug!("returns: {:?}", meta);
Ok(HttpResponse::Ok().json(meta))
}
2020-12-12 20:32:06 +08:00
async fn update_index(
2021-06-24 21:33:21 +08:00
data: GuardedData<Private, Data>,
path: web::Path<IndexParam>,
body: web::Json<UpdateIndexRequest>,
2020-12-12 20:32:06 +08:00
) -> Result<HttpResponse, ResponseError> {
2021-06-23 18:18:34 +08:00
debug!("called with params: {:?}", body);
2021-03-15 23:52:05 +08:00
let body = body.into_inner();
2021-06-15 22:02:49 +08:00
let meta = data
2021-03-16 01:11:10 +08:00
.update_index(path.into_inner().index_uid, body.primary_key, body.uid)
2021-06-15 22:02:49 +08:00
.await?;
2021-06-23 18:18:34 +08:00
debug!("returns: {:?}", meta);
2021-06-15 22:02:49 +08:00
Ok(HttpResponse::Ok().json(meta))
2020-12-12 20:32:06 +08:00
}
async fn delete_index(
2021-06-24 21:33:21 +08:00
data: GuardedData<Private, Data>,
2021-02-15 17:53:21 +08:00
path: web::Path<IndexParam>,
2020-12-12 20:32:06 +08:00
) -> Result<HttpResponse, ResponseError> {
2021-06-15 22:02:49 +08:00
data.delete_index(path.index_uid.clone()).await?;
Ok(HttpResponse::NoContent().finish())
2020-12-12 20:32:06 +08:00
}
2021-07-05 20:29:20 +08:00
async fn get_index_stats(
2021-06-24 21:33:21 +08:00
data: GuardedData<Private, Data>,
2021-01-29 01:32:24 +08:00
path: web::Path<IndexParam>,
2020-12-12 20:32:06 +08:00
) -> Result<HttpResponse, ResponseError> {
2021-07-05 20:29:20 +08:00
let response = data.get_index_stats(path.index_uid.clone()).await?;
2021-07-05 20:29:20 +08:00
debug!("returns: {:?}", response);
Ok(HttpResponse::Ok().json(response))
2020-12-12 20:32:06 +08:00
}