1478: refactor routes r=irevoire a=MarinPostma

refactor the route directory, so the module tree follows the route structure


Co-authored-by: mpostma <postma.marin@protonmail.com>
This commit is contained in:
bors[bot] 2021-07-05 12:55:39 +00:00 committed by GitHub
commit 0f024cc225
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 203 additions and 207 deletions

View File

@ -106,20 +106,13 @@ macro_rules! create_app {
use actix_web::middleware::TrailingSlash; use actix_web::middleware::TrailingSlash;
use actix_web::App; use actix_web::App;
use actix_web::{middleware, web}; use actix_web::{middleware, web};
use meilisearch_http::routes::*; use meilisearch_http::routes;
use meilisearch_http::{configure_auth, configure_data, dashboard}; use meilisearch_http::{configure_auth, configure_data, dashboard};
App::new() App::new()
.configure(|s| configure_data(s, $data.clone())) .configure(|s| configure_data(s, $data.clone()))
.configure(|s| configure_auth(s, &$data)) .configure(|s| configure_auth(s, &$data))
.configure(document::services) .configure(routes::configure)
.configure(index::services)
.configure(search::services)
.configure(settings::services)
.configure(health::services)
.configure(stats::services)
.configure(key::services)
.configure(dump::services)
.configure(|s| dashboard(s, $enable_frontend)) .configure(|s| dashboard(s, $enable_frontend))
.wrap( .wrap(
Cors::default() Cors::default()

View File

@ -6,9 +6,9 @@ use crate::error::ResponseError;
use crate::extractors::authentication::{policies::*, GuardedData}; use crate::extractors::authentication::{policies::*, GuardedData};
use crate::Data; use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) { pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("/dumps").route(web::post().to(create_dump))) cfg.service(web::resource("").route(web::post().to(create_dump)))
.service(web::resource("/dumps/{dump_uid}/status").route(web::get().to(get_dump_status))); .service(web::resource("/{dump_uid}/status").route(web::get().to(get_dump_status)));
} }
async fn create_dump(data: GuardedData<Private, Data>) -> Result<HttpResponse, ResponseError> { async fn create_dump(data: GuardedData<Private, Data>) -> Result<HttpResponse, ResponseError> {

View File

@ -1,11 +0,0 @@
use actix_web::{web, HttpResponse};
use crate::error::ResponseError;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("/health").route(web::get().to(get_health)));
}
async fn get_health() -> Result<HttpResponse, ResponseError> {
Ok(HttpResponse::Ok().json(serde_json::json!({ "status": "available" })))
}

View File

@ -54,10 +54,8 @@ struct DocumentParam {
document_id: String, document_id: String,
} }
pub fn services(cfg: &mut web::ServiceConfig) { pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service( cfg.service(
web::scope("/indexes/{index_uid}/documents")
.service(
web::resource("") web::resource("")
.route(web::get().to(get_all_documents)) .route(web::get().to(get_all_documents))
.route(web::post().guard(guard_json).to(add_documents)) .route(web::post().guard(guard_json).to(add_documents))
@ -70,7 +68,6 @@ pub fn services(cfg: &mut web::ServiceConfig) {
web::resource("/{document_id}") web::resource("/{document_id}")
.route(web::get().to(get_document)) .route(web::get().to(get_document))
.route(web::delete().to(delete_document)), .route(web::delete().to(delete_document)),
),
); );
} }

View File

@ -3,32 +3,44 @@ use chrono::{DateTime, Utc};
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{IndexParam, UpdateStatusResponse};
use crate::error::ResponseError; use crate::error::ResponseError;
use crate::extractors::authentication::{policies::*, GuardedData}; use crate::extractors::authentication::{policies::*, GuardedData};
use crate::routes::IndexParam;
use crate::Data; use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) { mod documents;
mod search;
mod settings;
mod updates;
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service( cfg.service(
web::resource("indexes") web::resource("")
.route(web::get().to(list_indexes)) .route(web::get().to(list_indexes))
.route(web::post().to(create_index)), .route(web::post().to(create_index)),
) )
.service( .service(
web::resource("/indexes/{index_uid}") web::scope("/{index_uid}")
.service(
web::resource("")
.route(web::get().to(get_index)) .route(web::get().to(get_index))
.route(web::put().to(update_index)) .route(web::put().to(update_index))
.route(web::delete().to(delete_index)), .route(web::delete().to(delete_index)),
) )
.service( .service(web::resource("/stats").route(web::get().to(get_index_stats)))
web::resource("/indexes/{index_uid}/updates").route(web::get().to(get_all_updates_status)), .service(web::scope("/documents").configure(documents::configure))
) .service(web::scope("/search").configure(search::configure))
.service( .service(web::scope("/updates").configure(updates::configure))
web::resource("/indexes/{index_uid}/updates/{update_id}") .service(web::scope("/settings").configure(settings::configure)),
.route(web::get().to(get_update_status)),
); );
} }
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))
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct IndexCreateRequest { struct IndexCreateRequest {
@ -36,6 +48,15 @@ struct IndexCreateRequest {
primary_key: Option<String>, primary_key: Option<String>,
} }
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?;
Ok(HttpResponse::Ok().json(meta))
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateIndexRequest { struct UpdateIndexRequest {
@ -52,22 +73,6 @@ pub struct UpdateIndexResponse {
updated_at: DateTime<Utc>, updated_at: DateTime<Utc>,
primary_key: Option<String>, primary_key: Option<String>,
} }
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))
}
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?;
Ok(HttpResponse::Ok().json(meta))
}
async fn get_index( async fn get_index(
data: GuardedData<Private, Data>, data: GuardedData<Private, Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
@ -99,35 +104,12 @@ async fn delete_index(
Ok(HttpResponse::NoContent().finish()) Ok(HttpResponse::NoContent().finish())
} }
#[derive(Deserialize)] async fn get_index_stats(
struct UpdateParam {
index_uid: String,
update_id: u64,
}
async fn get_update_status(
data: GuardedData<Private, Data>,
path: web::Path<UpdateParam>,
) -> Result<HttpResponse, ResponseError> {
let params = path.into_inner();
let meta = data
.get_update_status(params.index_uid, params.update_id)
.await?;
let meta = UpdateStatusResponse::from(meta);
debug!("returns: {:?}", meta);
Ok(HttpResponse::Ok().json(meta))
}
async fn get_all_updates_status(
data: GuardedData<Private, Data>, data: GuardedData<Private, Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let metas = data.get_updates_status(path.into_inner().index_uid).await?; let response = data.get_index_stats(path.index_uid.clone()).await?;
let metas = metas
.into_iter()
.map(UpdateStatusResponse::from)
.collect::<Vec<_>>();
debug!("returns: {:?}", metas); debug!("returns: {:?}", response);
Ok(HttpResponse::Ok().json(metas)) Ok(HttpResponse::Ok().json(response))
} }

View File

@ -11,9 +11,9 @@ use crate::index::{default_crop_length, SearchQuery, DEFAULT_SEARCH_LIMIT};
use crate::routes::IndexParam; use crate::routes::IndexParam;
use crate::Data; use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) { pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service( cfg.service(
web::resource("/indexes/{index_uid}/search") web::resource("")
.route(web::get().to(search_with_url_query)) .route(web::get().to(search_with_url_query))
.route(web::post().to(search_with_post)), .route(web::post().to(search_with_post)),
); );

View File

@ -69,59 +69,54 @@ macro_rules! make_setting_route {
} }
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/filterable-attributes", "/filterable-attributes",
std::collections::HashSet<String>, std::collections::HashSet<String>,
filterable_attributes, filterable_attributes,
"filterableAttributes" "filterableAttributes"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/displayed-attributes", "/displayed-attributes",
Vec<String>, Vec<String>,
displayed_attributes, displayed_attributes,
"displayedAttributes" "displayedAttributes"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/searchable-attributes", "/searchable-attributes",
Vec<String>, Vec<String>,
searchable_attributes, searchable_attributes,
"searchableAttributes" "searchableAttributes"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/stop-words", "/stop-words",
std::collections::BTreeSet<String>, std::collections::BTreeSet<String>,
stop_words, stop_words,
"stopWords" "stopWords"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/synonyms", "/synonyms",
std::collections::BTreeMap<String, Vec<String>>, std::collections::BTreeMap<String, Vec<String>>,
synonyms, synonyms,
"synonyms" "synonyms"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/distinct-attribute", "/distinct-attribute",
String, String,
distinct_attribute, distinct_attribute,
"distinctAttribute" "distinctAttribute"
); );
make_setting_route!( make_setting_route!("/ranking-rules", Vec<String>, ranking_rules, "rankingRules");
"/indexes/{index_uid}/settings/ranking-rules",
Vec<String>,
ranking_rules,
"rankingRules"
);
macro_rules! create_services { macro_rules! generate_configure {
($($mod:ident),*) => { ($($mod:ident),*) => {
pub fn services(cfg: &mut web::ServiceConfig) { pub fn configure(cfg: &mut web::ServiceConfig) {
cfg cfg.service(
.service(web::resource("/indexes/{index_uid}/settings") web::resource("")
.route(web::post().to(update_all)) .route(web::post().to(update_all))
.route(web::get().to(get_all)) .route(web::get().to(get_all))
.route(web::delete().to(delete_all))) .route(web::delete().to(delete_all)))
@ -130,7 +125,7 @@ macro_rules! create_services {
}; };
} }
create_services!( generate_configure!(
filterable_attributes, filterable_attributes,
displayed_attributes, displayed_attributes,
searchable_attributes, searchable_attributes,

View File

@ -0,0 +1,64 @@
use actix_web::{web, HttpResponse};
use chrono::{DateTime, Utc};
use log::debug;
use serde::{Deserialize, Serialize};
use crate::error::ResponseError;
use crate::extractors::authentication::{policies::*, GuardedData};
use crate::routes::{IndexParam, UpdateStatusResponse};
use crate::Data;
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("").route(web::get().to(get_all_updates_status)))
.service(web::resource("{update_id}").route(web::get().to(get_update_status)));
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct UpdateIndexRequest {
uid: Option<String>,
primary_key: Option<String>,
}
#[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>,
}
#[derive(Deserialize)]
struct UpdateParam {
index_uid: String,
update_id: u64,
}
async fn get_update_status(
data: GuardedData<Private, Data>,
path: web::Path<UpdateParam>,
) -> Result<HttpResponse, ResponseError> {
let params = path.into_inner();
let meta = data
.get_update_status(params.index_uid, params.update_id)
.await?;
let meta = UpdateStatusResponse::from(meta);
debug!("returns: {:?}", meta);
Ok(HttpResponse::Ok().json(meta))
}
async fn get_all_updates_status(
data: GuardedData<Private, Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let metas = data.get_updates_status(path.into_inner().index_uid).await?;
let metas = metas
.into_iter()
.map(UpdateStatusResponse::from)
.collect::<Vec<_>>();
debug!("returns: {:?}", metas);
Ok(HttpResponse::Ok().json(metas))
}

View File

@ -1,23 +0,0 @@
use actix_web::{web, HttpResponse};
use serde::Serialize;
use crate::extractors::authentication::{policies::*, GuardedData};
use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("/keys").route(web::get().to(list)));
}
#[derive(Serialize)]
struct KeysResponse {
private: Option<String>,
public: Option<String>,
}
async fn list(data: GuardedData<Admin, Data>) -> HttpResponse {
let api_keys = data.api_keys.clone();
HttpResponse::Ok().json(&KeysResponse {
private: api_keys.private,
public: api_keys.public,
})
}

View File

@ -1,21 +1,27 @@
use std::time::Duration; use std::time::Duration;
use actix_web::HttpResponse; use actix_web::{web, HttpResponse};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::error::ResponseError; use crate::error::ResponseError;
use crate::extractors::authentication::{policies::*, GuardedData};
use crate::index::{Settings, Unchecked}; use crate::index::{Settings, Unchecked};
use crate::index_controller::{UpdateMeta, UpdateResult, UpdateStatus}; use crate::index_controller::{UpdateMeta, UpdateResult, UpdateStatus};
use crate::Data;
pub mod document; mod dump;
pub mod dump; mod indexes;
pub mod health;
pub mod index; pub fn configure(cfg: &mut web::ServiceConfig) {
pub mod key; cfg.service(web::resource("/health").route(web::get().to(get_health)))
pub mod search; .service(web::scope("/dumps").configure(dump::configure))
pub mod settings; .service(web::resource("/keys").route(web::get().to(list_keys)))
pub mod stats; .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)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
@ -43,7 +49,6 @@ pub enum UpdateType {
impl From<&UpdateStatus> for UpdateType { impl From<&UpdateStatus> for UpdateType {
fn from(other: &UpdateStatus) -> Self { fn from(other: &UpdateStatus) -> Self {
use milli::update::IndexDocumentsMethod::*; use milli::update::IndexDocumentsMethod::*;
match other.meta() { match other.meta() {
UpdateMeta::DocumentsAddition { method, .. } => { UpdateMeta::DocumentsAddition { method, .. } => {
let number = match other { let number = match other {
@ -223,3 +228,53 @@ impl IndexUpdateResponse {
pub async fn running() -> HttpResponse { pub async fn running() -> HttpResponse {
HttpResponse::Ok().json(serde_json::json!({ "status": "MeiliSearch is running" })) HttpResponse::Ok().json(serde_json::json!({ "status": "MeiliSearch is running" }))
} }
async fn get_stats(data: GuardedData<Private, Data>) -> Result<HttpResponse, ResponseError> {
let response = data.get_all_stats().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(_data: GuardedData<Private, Data>) -> HttpResponse {
let commit_sha = match option_env!("COMMIT_SHA") {
Some("") | None => env!("VERGEN_SHA"),
Some(commit_sha) => commit_sha,
};
let commit_date = match option_env!("COMMIT_DATE") {
Some("") | None => env!("VERGEN_COMMIT_DATE"),
Some(commit_date) => commit_date,
};
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 list_keys(data: GuardedData<Admin, Data>) -> HttpResponse {
let api_keys = data.api_keys.clone();
HttpResponse::Ok().json(&KeysResponse {
private: api_keys.private,
public: api_keys.public,
})
}
pub async fn get_health() -> Result<HttpResponse, ResponseError> {
Ok(HttpResponse::Ok().json(serde_json::json!({ "status": "available" })))
}

View File

@ -1,56 +0,0 @@
use actix_web::{web, HttpResponse};
use log::debug;
use serde::Serialize;
use crate::error::ResponseError;
use crate::extractors::authentication::{policies::*, GuardedData};
use crate::routes::IndexParam;
use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("/indexes/{index_uid}/stats").route(web::get().to(get_index_stats)))
.service(web::resource("/stats").route(web::get().to(get_stats)))
.service(web::resource("/version").route(web::get().to(get_version)));
}
async fn get_index_stats(
data: GuardedData<Private, Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
let response = data.get_index_stats(path.index_uid.clone()).await?;
debug!("returns: {:?}", response);
Ok(HttpResponse::Ok().json(response))
}
async fn get_stats(data: GuardedData<Private, Data>) -> Result<HttpResponse, ResponseError> {
let response = data.get_all_stats().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(_data: GuardedData<Private, Data>) -> HttpResponse {
let commit_sha = match option_env!("COMMIT_SHA") {
Some("") | None => env!("VERGEN_SHA"),
Some(commit_sha) => commit_sha,
};
let commit_date = match option_env!("COMMIT_DATE") {
Some("") | None => env!("VERGEN_COMMIT_DATE"),
Some(commit_date) => commit_date,
};
HttpResponse::Ok().json(VersionResponse {
commit_sha: commit_sha.to_string(),
commit_date: commit_date.to_string(),
pkg_version: env!("CARGO_PKG_VERSION").to_string(),
})
}