MeiliSearch/meilisearch-http/src/lib.rs

158 lines
5.1 KiB
Rust
Raw Normal View History

#![allow(rustdoc::private_intra_doc_links)]
2021-06-15 17:55:27 +02:00
#[macro_use]
2020-12-12 13:32:06 +01:00
pub mod error;
pub mod analytics;
2021-06-24 14:22:12 +02:00
#[macro_use]
2021-06-23 14:56:02 +02:00
pub mod extractors;
2021-03-15 18:11:10 +01:00
pub mod option;
pub mod routes;
pub mod search;
2022-08-29 12:36:54 +02:00
#[cfg(feature = "metrics")]
pub mod metrics;
#[cfg(feature = "metrics")]
pub mod route_metrics;
2022-01-19 11:21:19 +01:00
use std::sync::{atomic::AtomicBool, Arc};
2021-09-28 18:10:09 +02:00
use crate::error::MeilisearchHttpError;
use actix_web::error::JsonPayloadError;
2022-09-27 16:33:37 +02:00
use actix_web::web::Data;
use analytics::Analytics;
use error::PayloadError;
use http::header::CONTENT_TYPE;
2021-03-15 18:11:10 +01:00
pub use option::Opt;
2021-03-10 11:56:51 +01:00
use actix_web::{web, HttpRequest};
2021-06-23 14:56:02 +02:00
2021-06-24 16:25:52 +02:00
use extractors::payload::PayloadConfig;
2022-09-27 16:33:37 +02:00
use index_scheduler::IndexScheduler;
use meilisearch_auth::AuthController;
2021-09-20 15:31:03 +02:00
2022-01-19 11:21:19 +01:00
pub static AUTOBATCHING_ENABLED: AtomicBool = AtomicBool::new(false);
2022-09-27 16:33:37 +02:00
// TODO: TAMO: Finish setting up things
pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<IndexScheduler> {
let meilisearch = IndexScheduler::new(
opt.db_path.join("tasks"),
opt.db_path.join("update_files"),
opt.db_path.join("indexes"),
2022-10-13 15:02:59 +02:00
opt.dumps_dir.clone(),
2022-09-27 16:33:37 +02:00
opt.max_index_size.get_bytes() as usize,
(&opt.indexer_options).try_into()?,
true,
2022-09-27 16:33:37 +02:00
#[cfg(test)]
todo!("We'll see later"),
)?;
/*
TODO: We should start a thread to handle the snapshots.
2021-09-28 18:10:09 +02:00
meilisearch
// snapshot
2021-09-28 18:10:09 +02:00
.set_ignore_missing_snapshot(opt.ignore_missing_snapshot)
.set_ignore_snapshot_if_db_exists(opt.ignore_snapshot_if_db_exists)
.set_snapshot_interval(Duration::from_secs(opt.snapshot_interval_sec))
.set_snapshot_dir(opt.snapshot_dir.clone())
// dump
.set_ignore_missing_dump(opt.ignore_missing_dump)
.set_ignore_dump_if_db_exists(opt.ignore_dump_if_db_exists)
.set_dump_dst(opt.dumps_dir.clone());
2021-09-28 18:10:09 +02:00
if let Some(ref path) = opt.import_snapshot {
meilisearch.set_import_snapshot(path.clone());
}
if let Some(ref path) = opt.import_dump {
meilisearch.set_dump_src(path.clone());
}
if opt.schedule_snapshot {
meilisearch.set_schedule_snapshot();
}
2022-09-27 16:33:37 +02:00
*/
2021-09-28 18:10:09 +02:00
2022-09-27 16:33:37 +02:00
Ok(meilisearch)
2021-09-28 18:10:09 +02:00
}
pub fn configure_data(
config: &mut web::ServiceConfig,
2022-09-27 16:33:37 +02:00
index_scheduler: Data<IndexScheduler>,
auth: AuthController,
opt: &Opt,
analytics: Arc<dyn Analytics>,
) {
2021-09-20 15:31:03 +02:00
let http_payload_size_limit = opt.http_payload_size_limit.get_bytes() as usize;
2021-06-23 13:21:48 +02:00
config
2022-09-27 16:33:37 +02:00
.app_data(index_scheduler)
.app_data(auth)
.app_data(web::Data::from(analytics))
2021-06-23 13:21:48 +02:00
.app_data(
web::JsonConfig::default()
.content_type(|mime| mime == mime::APPLICATION_JSON)
.error_handler(|err, req: &HttpRequest| match err {
JsonPayloadError::ContentType => match req.headers().get(CONTENT_TYPE) {
Some(content_type) => MeilisearchHttpError::InvalidContentType(
content_type.to_str().unwrap_or("unknown").to_string(),
vec![mime::APPLICATION_JSON.to_string()],
)
.into(),
None => MeilisearchHttpError::MissingContentType(vec![
mime::APPLICATION_JSON.to_string(),
])
.into(),
},
err => PayloadError::from(err).into(),
}),
2021-06-23 13:21:48 +02:00
)
2021-06-23 13:58:22 +02:00
.app_data(PayloadConfig::new(http_payload_size_limit))
2021-06-23 13:21:48 +02:00
.app_data(
web::QueryConfig::default().error_handler(|err, _req| PayloadError::from(err).into()),
2021-06-23 13:21:48 +02:00
);
}
2021-03-10 11:56:51 +01:00
2021-06-23 13:21:48 +02:00
#[cfg(feature = "mini-dashboard")]
pub fn dashboard(config: &mut web::ServiceConfig, enable_frontend: bool) {
2021-06-23 13:55:16 +02:00
use actix_web::HttpResponse;
use static_files::Resource;
2021-04-21 13:49:21 +02:00
2021-06-23 14:48:33 +02:00
mod generated {
2021-06-23 13:21:48 +02:00
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
}
2021-04-21 13:49:21 +02:00
2021-06-23 13:21:48 +02:00
if enable_frontend {
2021-06-23 14:48:33 +02:00
let generated = generated::generate();
2021-06-24 16:25:52 +02:00
// Generate routes for mini-dashboard assets
for (path, resource) in generated.into_iter() {
let Resource {
mime_type, data, ..
} = resource;
// Redirect index.html to /
if path == "index.html" {
2022-02-26 00:28:55 +01:00
config.service(web::resource("/").route(web::get().to(move || async move {
HttpResponse::Ok().content_type(mime_type).body(data)
})));
2021-06-24 16:25:52 +02:00
} else {
2022-02-26 00:28:55 +01:00
config.service(web::resource(path).route(web::get().to(move || async move {
HttpResponse::Ok().content_type(mime_type).body(data)
})));
2021-06-23 13:21:48 +02:00
}
2021-06-24 16:25:52 +02:00
}
2021-06-23 13:21:48 +02:00
} else {
2021-06-24 19:02:28 +02:00
config.service(web::resource("/").route(web::get().to(routes::running)));
2021-06-23 13:21:48 +02:00
}
}
#[cfg(not(feature = "mini-dashboard"))]
pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
2021-06-24 19:02:28 +02:00
config.service(web::resource("/").route(web::get().to(routes::running)));
2021-06-23 13:21:48 +02:00
}
2021-04-21 13:49:21 +02:00
2022-08-29 12:36:54 +02:00
#[cfg(feature = "metrics")]
pub fn configure_metrics_route(config: &mut web::ServiceConfig, enable_metrics_route: bool) {
if enable_metrics_route {
2022-08-29 12:36:54 +02:00
config.service(
web::resource("/metrics").route(web::get().to(crate::route_metrics::get_metrics)),
);
}
}