2020-01-31 10:50:28 +01:00
|
|
|
#![allow(clippy::or_fun_call)]
|
|
|
|
|
2019-10-31 15:00:36 +01:00
|
|
|
pub mod data;
|
|
|
|
pub mod error;
|
|
|
|
pub mod helpers;
|
|
|
|
pub mod models;
|
|
|
|
pub mod option;
|
|
|
|
pub mod routes;
|
2020-05-26 19:03:13 +02:00
|
|
|
pub mod analytics;
|
2020-07-02 16:20:45 +02:00
|
|
|
pub mod snapshot;
|
2020-09-29 12:18:09 +02:00
|
|
|
pub mod dump;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-15 10:51:15 +02:00
|
|
|
use actix_http::Error;
|
|
|
|
use actix_service::ServiceFactory;
|
|
|
|
use actix_web::{dev, web, App};
|
2020-05-22 12:03:57 +02:00
|
|
|
use chrono::Utc;
|
2020-04-15 10:51:15 +02:00
|
|
|
use log::error;
|
2020-05-22 12:03:57 +02:00
|
|
|
|
2020-09-29 12:18:09 +02:00
|
|
|
use meilisearch_core::{Index, MainWriter, ProcessedUpdateResult};
|
2020-04-15 10:51:15 +02:00
|
|
|
|
2020-05-26 19:03:13 +02:00
|
|
|
pub use option::Opt;
|
2020-05-22 12:03:57 +02:00
|
|
|
pub use self::data::Data;
|
2020-06-03 18:21:03 +02:00
|
|
|
use self::error::{payload_error_handler, ResponseError};
|
2020-05-22 12:03:57 +02:00
|
|
|
|
2020-04-15 10:51:15 +02:00
|
|
|
pub fn create_app(
|
|
|
|
data: &Data,
|
2020-11-23 13:13:10 +01:00
|
|
|
enable_frontend: bool,
|
2020-04-15 10:51:15 +02:00
|
|
|
) -> App<
|
|
|
|
impl ServiceFactory<
|
|
|
|
Config = (),
|
|
|
|
Request = dev::ServiceRequest,
|
|
|
|
Response = dev::ServiceResponse<actix_http::body::Body>,
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
actix_http::body::Body,
|
|
|
|
> {
|
2020-11-23 13:13:10 +01:00
|
|
|
let app = App::new()
|
2020-09-12 04:29:17 +02:00
|
|
|
.data(data.clone())
|
2020-05-07 12:29:18 +02:00
|
|
|
.app_data(
|
|
|
|
web::JsonConfig::default()
|
2020-05-14 17:52:10 +02:00
|
|
|
.limit(data.http_payload_size_limit)
|
2020-05-07 12:29:18 +02:00
|
|
|
.content_type(|_mime| true) // Accept all mime types
|
2020-06-03 18:21:03 +02:00
|
|
|
.error_handler(|err, _req| payload_error_handler(err).into()),
|
|
|
|
)
|
|
|
|
.app_data(
|
|
|
|
web::QueryConfig::default()
|
|
|
|
.error_handler(|err, _req| payload_error_handler(err).into())
|
2020-05-07 12:29:18 +02:00
|
|
|
)
|
2020-04-22 17:43:51 +02:00
|
|
|
.configure(routes::document::services)
|
|
|
|
.configure(routes::index::services)
|
|
|
|
.configure(routes::search::services)
|
|
|
|
.configure(routes::setting::services)
|
|
|
|
.configure(routes::stop_words::services)
|
|
|
|
.configure(routes::synonym::services)
|
|
|
|
.configure(routes::health::services)
|
|
|
|
.configure(routes::stats::services)
|
|
|
|
.configure(routes::key::services)
|
2020-11-23 13:13:10 +01:00
|
|
|
.configure(routes::dump::services);
|
|
|
|
if enable_frontend {
|
|
|
|
app
|
|
|
|
.service(routes::load_html)
|
|
|
|
.service(routes::load_css)
|
|
|
|
} else {
|
|
|
|
app
|
|
|
|
}
|
2020-09-29 12:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn index_update_callback_txn(index: Index, index_uid: &str, data: &Data, mut writer: &mut MainWriter) -> Result<(), String> {
|
|
|
|
if let Err(e) = data.db.compute_stats(&mut writer, index_uid) {
|
|
|
|
return Err(format!("Impossible to compute stats; {}", e));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = data.db.set_last_update(&mut writer, &Utc::now()) {
|
|
|
|
return Err(format!("Impossible to update last_update; {}", e));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = index.main.put_updated_at(&mut writer) {
|
|
|
|
return Err(format!("Impossible to update updated_at; {}", e));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-04-15 10:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn index_update_callback(index_uid: &str, data: &Data, status: ProcessedUpdateResult) {
|
|
|
|
if status.error.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-29 12:18:09 +02:00
|
|
|
if let Some(index) = data.db.open_index(index_uid) {
|
2020-04-15 10:51:15 +02:00
|
|
|
let db = &data.db;
|
2020-05-22 12:03:57 +02:00
|
|
|
let res = db.main_write::<_, _, ResponseError>(|mut writer| {
|
2020-09-29 12:18:09 +02:00
|
|
|
if let Err(e) = index_update_callback_txn(index, index_uid, data, &mut writer) {
|
|
|
|
error!("{}", e);
|
2020-04-15 10:51:15 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 12:03:57 +02:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
match res {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => error!("{}", e),
|
2020-04-15 10:51:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|