2021-07-27 22:01:06 +08:00
|
|
|
#![allow(rustdoc::private_intra_doc_links)]
|
2021-06-15 23:55:27 +08:00
|
|
|
#[macro_use]
|
2020-12-12 20:32:06 +08:00
|
|
|
pub mod error;
|
2021-12-02 23:03:26 +08:00
|
|
|
pub mod analytics;
|
|
|
|
mod task;
|
2021-06-24 20:22:12 +08:00
|
|
|
#[macro_use]
|
2021-06-23 20:56:02 +08:00
|
|
|
pub mod extractors;
|
2020-12-12 20:32:06 +08:00
|
|
|
pub mod helpers;
|
2021-03-16 01:11:10 +08:00
|
|
|
pub mod option;
|
|
|
|
pub mod routes;
|
2021-12-02 23:03:26 +08:00
|
|
|
|
2021-10-29 21:58:06 +08:00
|
|
|
use std::sync::Arc;
|
2021-09-29 00:10:09 +08:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-10-06 17:49:34 +08:00
|
|
|
use crate::error::MeilisearchHttpError;
|
2021-10-05 19:30:53 +08:00
|
|
|
use actix_web::error::JsonPayloadError;
|
2021-10-12 20:32:44 +08:00
|
|
|
use analytics::Analytics;
|
2021-10-06 17:49:34 +08:00
|
|
|
use error::PayloadError;
|
2021-10-05 19:30:53 +08:00
|
|
|
use http::header::CONTENT_TYPE;
|
2021-03-16 01:11:10 +08:00
|
|
|
pub use option::Opt;
|
2021-03-10 18:56:51 +08:00
|
|
|
|
2021-10-06 17:49:34 +08:00
|
|
|
use actix_web::{web, HttpRequest};
|
2021-06-23 20:56:02 +08:00
|
|
|
|
2021-06-24 22:25:52 +08:00
|
|
|
use extractors::payload::PayloadConfig;
|
2021-11-09 01:31:27 +08:00
|
|
|
use meilisearch_auth::AuthController;
|
2021-09-21 19:23:22 +08:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2021-09-20 21:31:03 +08:00
|
|
|
|
2021-09-29 00:10:09 +08:00
|
|
|
pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<MeiliSearch> {
|
|
|
|
let mut meilisearch = MeiliSearch::builder();
|
|
|
|
meilisearch
|
|
|
|
.set_max_index_size(opt.max_index_size.get_bytes() as usize)
|
2021-12-02 23:03:26 +08:00
|
|
|
.set_max_task_store_size(opt.max_task_db_size.get_bytes() as usize)
|
2021-09-29 00:10:09 +08:00
|
|
|
.set_ignore_missing_snapshot(opt.ignore_missing_snapshot)
|
|
|
|
.set_ignore_snapshot_if_db_exists(opt.ignore_snapshot_if_db_exists)
|
|
|
|
.set_dump_dst(opt.dumps_dir.clone())
|
|
|
|
.set_snapshot_interval(Duration::from_secs(opt.snapshot_interval_sec))
|
|
|
|
.set_snapshot_dir(opt.snapshot_dir.clone());
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
meilisearch.build(opt.db_path.clone(), opt.indexer_options.clone())
|
|
|
|
}
|
|
|
|
|
2021-10-12 20:32:44 +08:00
|
|
|
pub fn configure_data(
|
|
|
|
config: &mut web::ServiceConfig,
|
|
|
|
data: MeiliSearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
auth: AuthController,
|
2021-10-12 20:32:44 +08:00
|
|
|
opt: &Opt,
|
2021-10-29 21:58:06 +08:00
|
|
|
analytics: Arc<dyn Analytics>,
|
2021-10-12 20:32:44 +08:00
|
|
|
) {
|
2021-09-20 21:31:03 +08:00
|
|
|
let http_payload_size_limit = opt.http_payload_size_limit.get_bytes() as usize;
|
2021-06-23 19:21:48 +08:00
|
|
|
config
|
2021-06-24 20:22:12 +08:00
|
|
|
.app_data(data)
|
2021-11-09 01:31:27 +08:00
|
|
|
.app_data(auth)
|
2021-10-29 21:58:06 +08:00
|
|
|
.app_data(web::Data::from(analytics))
|
2021-06-23 19:21:48 +08:00
|
|
|
.app_data(
|
|
|
|
web::JsonConfig::default()
|
2021-10-05 19:30:53 +08:00
|
|
|
.content_type(|mime| mime == mime::APPLICATION_JSON)
|
2021-10-06 17:49:34 +08:00
|
|
|
.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(),
|
2021-10-05 19:30:53 +08:00
|
|
|
vec![mime::APPLICATION_JSON.to_string()],
|
2021-10-06 17:49:34 +08:00
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
None => MeilisearchHttpError::MissingContentType(vec![
|
|
|
|
mime::APPLICATION_JSON.to_string(),
|
|
|
|
])
|
|
|
|
.into(),
|
|
|
|
},
|
|
|
|
err => PayloadError::from(err).into(),
|
2021-10-05 19:30:53 +08:00
|
|
|
}),
|
2021-06-23 19:21:48 +08:00
|
|
|
)
|
2021-06-23 19:58:22 +08:00
|
|
|
.app_data(PayloadConfig::new(http_payload_size_limit))
|
2021-06-23 19:21:48 +08:00
|
|
|
.app_data(
|
2021-10-06 17:49:34 +08:00
|
|
|
web::QueryConfig::default().error_handler(|err, _req| PayloadError::from(err).into()),
|
2021-06-23 19:21:48 +08:00
|
|
|
);
|
|
|
|
}
|
2021-03-10 18:56:51 +08:00
|
|
|
|
2021-06-23 19:21:48 +08:00
|
|
|
#[cfg(feature = "mini-dashboard")]
|
|
|
|
pub fn dashboard(config: &mut web::ServiceConfig, enable_frontend: bool) {
|
2021-06-23 19:55:16 +08:00
|
|
|
use actix_web::HttpResponse;
|
2021-06-24 22:25:52 +08:00
|
|
|
use actix_web_static_files::Resource;
|
2021-04-21 19:49:21 +08:00
|
|
|
|
2021-06-23 20:48:33 +08:00
|
|
|
mod generated {
|
2021-06-23 19:21:48 +08:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
|
|
|
|
}
|
2021-04-21 19:49:21 +08:00
|
|
|
|
2021-06-23 19:21:48 +08:00
|
|
|
if enable_frontend {
|
2021-06-23 20:48:33 +08:00
|
|
|
let generated = generated::generate();
|
2021-06-24 22:25:52 +08: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" {
|
|
|
|
config.service(web::resource("/").route(
|
|
|
|
web::get().to(move || HttpResponse::Ok().content_type(mime_type).body(data)),
|
|
|
|
));
|
|
|
|
} else {
|
2021-10-02 17:54:04 +08:00
|
|
|
config.service(web::resource(path).route(
|
2021-06-24 22:25:52 +08:00
|
|
|
web::get().to(move || HttpResponse::Ok().content_type(mime_type).body(data)),
|
|
|
|
));
|
2021-06-23 19:21:48 +08:00
|
|
|
}
|
2021-06-24 22:25:52 +08:00
|
|
|
}
|
2021-06-23 19:21:48 +08:00
|
|
|
} else {
|
2021-06-25 01:02:28 +08:00
|
|
|
config.service(web::resource("/").route(web::get().to(routes::running)));
|
2021-06-23 19:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "mini-dashboard"))]
|
|
|
|
pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
|
2021-06-25 01:02:28 +08:00
|
|
|
config.service(web::resource("/").route(web::get().to(routes::running)));
|
2021-06-23 19:21:48 +08:00
|
|
|
}
|
2021-04-21 19:49:21 +08:00
|
|
|
|
2021-06-23 19:21:48 +08:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! create_app {
|
2021-11-09 01:31:27 +08:00
|
|
|
($data:expr, $auth:expr, $enable_frontend:expr, $opt:expr, $analytics:expr) => {{
|
2021-06-23 19:21:48 +08:00
|
|
|
use actix_cors::Cors;
|
|
|
|
use actix_web::middleware::TrailingSlash;
|
|
|
|
use actix_web::App;
|
|
|
|
use actix_web::{middleware, web};
|
2021-12-02 23:03:26 +08:00
|
|
|
use meilisearch_error::ResponseError;
|
|
|
|
use meilisearch_http::error::MeilisearchHttpError;
|
2021-07-05 20:29:20 +08:00
|
|
|
use meilisearch_http::routes;
|
2021-12-06 22:45:41 +08:00
|
|
|
use meilisearch_http::{configure_data, dashboard};
|
2021-04-21 19:49:21 +08:00
|
|
|
|
2021-06-23 19:21:48 +08:00
|
|
|
App::new()
|
2021-11-09 01:31:27 +08:00
|
|
|
.configure(|s| configure_data(s, $data.clone(), $auth.clone(), &$opt, $analytics))
|
2021-07-05 20:29:20 +08:00
|
|
|
.configure(routes::configure)
|
2021-06-23 19:21:48 +08:00
|
|
|
.configure(|s| dashboard(s, $enable_frontend))
|
|
|
|
.wrap(
|
2021-06-23 03:48:51 +08:00
|
|
|
Cors::default()
|
2021-06-23 20:48:33 +08:00
|
|
|
.send_wildcard()
|
2022-01-05 22:26:17 +08:00
|
|
|
.allow_any_header()
|
2021-06-23 20:48:33 +08:00
|
|
|
.allow_any_origin()
|
|
|
|
.allow_any_method()
|
|
|
|
.max_age(86_400), // 24h
|
2021-06-23 03:48:51 +08:00
|
|
|
)
|
2021-06-23 19:21:48 +08:00
|
|
|
.wrap(middleware::Logger::default())
|
|
|
|
.wrap(middleware::Compress::default())
|
2021-06-23 20:48:33 +08:00
|
|
|
.wrap(middleware::NormalizePath::new(
|
|
|
|
middleware::TrailingSlash::Trim,
|
|
|
|
))
|
2021-06-23 19:21:48 +08:00
|
|
|
}};
|
2021-03-10 18:56:51 +08:00
|
|
|
}
|