2021-12-15 17:30:00 +08:00
|
|
|
#![allow(dead_code)]
|
2022-03-21 17:50:55 +08:00
|
|
|
|
|
|
|
use clap::Parser;
|
2021-03-24 18:03:01 +08:00
|
|
|
use std::path::Path;
|
|
|
|
|
2021-03-10 21:43:10 +08:00
|
|
|
use actix_web::http::StatusCode;
|
2021-02-19 02:50:52 +08:00
|
|
|
use byte_unit::{Byte, ByteUnit};
|
2021-11-09 01:31:27 +08:00
|
|
|
use meilisearch_auth::AuthController;
|
2021-09-29 00:10:09 +08:00
|
|
|
use meilisearch_http::setup_meilisearch;
|
|
|
|
use meilisearch_lib::options::{IndexerOpts, MaxMemory};
|
|
|
|
use once_cell::sync::Lazy;
|
2021-02-20 02:14:25 +08:00
|
|
|
use serde_json::Value;
|
2021-09-29 00:10:09 +08:00
|
|
|
use tempfile::TempDir;
|
2021-02-19 02:50:52 +08:00
|
|
|
|
2021-09-29 00:10:09 +08:00
|
|
|
use meilisearch_http::option::Opt;
|
2021-02-19 02:50:52 +08:00
|
|
|
|
|
|
|
use super::index::Index;
|
|
|
|
use super::service::Service;
|
|
|
|
|
|
|
|
pub struct Server {
|
2021-02-24 16:30:51 +08:00
|
|
|
pub service: Service,
|
2021-03-24 18:03:01 +08:00
|
|
|
// hold ownership to the tempdir while we use the server instance.
|
2021-09-29 00:10:09 +08:00
|
|
|
_dir: Option<TempDir>,
|
2021-02-19 02:50:52 +08:00
|
|
|
}
|
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
pub static TEST_TEMP_DIR: Lazy<TempDir> = Lazy::new(|| TempDir::new().unwrap());
|
2021-09-29 00:10:09 +08:00
|
|
|
|
2021-02-19 02:50:52 +08:00
|
|
|
impl Server {
|
|
|
|
pub async fn new() -> Self {
|
2021-09-29 00:10:09 +08:00
|
|
|
let dir = TempDir::new().unwrap();
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
std::env::set_var("TMP", TEST_TEMP_DIR.path());
|
|
|
|
} else {
|
|
|
|
std::env::set_var("TMPDIR", TEST_TEMP_DIR.path());
|
|
|
|
}
|
2021-02-19 02:50:52 +08:00
|
|
|
|
2021-09-29 00:10:09 +08:00
|
|
|
let options = default_settings(dir.path());
|
2021-02-19 02:50:52 +08:00
|
|
|
|
2021-09-29 00:10:09 +08:00
|
|
|
let meilisearch = setup_meilisearch(&options).unwrap();
|
2021-11-09 01:31:27 +08:00
|
|
|
let auth = AuthController::new(&options.db_path, &options.master_key).unwrap();
|
2021-09-29 04:22:59 +08:00
|
|
|
let service = Service {
|
|
|
|
meilisearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
auth,
|
2021-09-29 04:22:59 +08:00
|
|
|
options,
|
2021-11-09 01:31:27 +08:00
|
|
|
api_key: None,
|
2021-09-29 04:22:59 +08:00
|
|
|
};
|
2021-02-19 02:50:52 +08:00
|
|
|
|
2021-03-24 18:29:11 +08:00
|
|
|
Server {
|
|
|
|
service,
|
|
|
|
_dir: Some(dir),
|
|
|
|
}
|
2021-03-24 18:03:01 +08:00
|
|
|
}
|
|
|
|
|
2022-06-10 00:08:26 +08:00
|
|
|
pub async fn new_auth_with_options(mut options: Opt, dir: TempDir) -> Self {
|
2021-12-15 21:52:33 +08:00
|
|
|
if cfg!(windows) {
|
|
|
|
std::env::set_var("TMP", TEST_TEMP_DIR.path());
|
|
|
|
} else {
|
|
|
|
std::env::set_var("TMPDIR", TEST_TEMP_DIR.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
options.master_key = Some("MASTER_KEY".to_string());
|
|
|
|
|
|
|
|
let meilisearch = setup_meilisearch(&options).unwrap();
|
|
|
|
let auth = AuthController::new(&options.db_path, &options.master_key).unwrap();
|
|
|
|
let service = Service {
|
|
|
|
meilisearch,
|
|
|
|
auth,
|
|
|
|
options,
|
|
|
|
api_key: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Server {
|
|
|
|
service,
|
|
|
|
_dir: Some(dir),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 00:08:26 +08:00
|
|
|
pub async fn new_auth() -> Self {
|
|
|
|
let dir = TempDir::new().unwrap();
|
|
|
|
let options = default_settings(dir.path());
|
|
|
|
Self::new_auth_with_options(options, dir).await
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:48:57 +08:00
|
|
|
pub async fn new_with_options(options: Opt) -> Result<Self, anyhow::Error> {
|
|
|
|
let meilisearch = setup_meilisearch(&options)?;
|
|
|
|
let auth = AuthController::new(&options.db_path, &options.master_key)?;
|
2021-09-29 04:22:59 +08:00
|
|
|
let service = Service {
|
|
|
|
meilisearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
auth,
|
2021-09-29 04:22:59 +08:00
|
|
|
options,
|
2021-11-09 01:31:27 +08:00
|
|
|
api_key: None,
|
2021-09-29 04:22:59 +08:00
|
|
|
};
|
2021-03-24 18:03:01 +08:00
|
|
|
|
2022-04-28 16:48:57 +08:00
|
|
|
Ok(Server {
|
2021-03-24 18:29:11 +08:00
|
|
|
service,
|
|
|
|
_dir: None,
|
2022-04-28 16:48:57 +08:00
|
|
|
})
|
2021-02-19 02:50:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a view to an index. There is no guarantee that the index exists.
|
2021-05-31 22:03:39 +08:00
|
|
|
pub fn index(&self, uid: impl AsRef<str>) -> Index<'_> {
|
2021-02-19 02:50:52 +08:00
|
|
|
Index {
|
2021-11-03 21:25:49 +08:00
|
|
|
uid: uid.as_ref().to_string(),
|
2021-02-19 02:50:52 +08:00
|
|
|
service: &self.service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 17:29:03 +08:00
|
|
|
pub async fn list_indexes(
|
|
|
|
&self,
|
|
|
|
offset: Option<usize>,
|
|
|
|
limit: Option<usize>,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let (offset, limit) = (
|
|
|
|
offset.map(|offset| format!("offset={offset}")),
|
|
|
|
limit.map(|limit| format!("limit={limit}")),
|
|
|
|
);
|
|
|
|
let query_parameter = offset
|
|
|
|
.as_ref()
|
|
|
|
.zip(limit.as_ref())
|
|
|
|
.map(|(offset, limit)| format!("{offset}&{limit}"))
|
|
|
|
.or_else(|| offset.xor(limit));
|
|
|
|
if let Some(query_parameter) = query_parameter {
|
|
|
|
self.service
|
|
|
|
.get(format!("/indexes?{query_parameter}"))
|
|
|
|
.await
|
|
|
|
} else {
|
|
|
|
self.service.get("/indexes").await
|
|
|
|
}
|
2021-02-20 02:14:25 +08:00
|
|
|
}
|
2021-03-16 02:08:19 +08:00
|
|
|
|
|
|
|
pub async fn version(&self) -> (Value, StatusCode) {
|
|
|
|
self.service.get("/version").await
|
|
|
|
}
|
2021-04-02 02:54:37 +08:00
|
|
|
|
|
|
|
pub async fn stats(&self) -> (Value, StatusCode) {
|
|
|
|
self.service.get("/stats").await
|
|
|
|
}
|
2021-12-02 23:03:26 +08:00
|
|
|
|
|
|
|
pub async fn tasks(&self) -> (Value, StatusCode) {
|
|
|
|
self.service.get("/tasks").await
|
|
|
|
}
|
2021-12-15 17:30:00 +08:00
|
|
|
|
|
|
|
pub async fn get_dump_status(&self, uid: &str) -> (Value, StatusCode) {
|
|
|
|
self.service.get(format!("/dumps/{}/status", uid)).await
|
|
|
|
}
|
2021-02-19 02:50:52 +08:00
|
|
|
}
|
2021-03-24 18:03:01 +08:00
|
|
|
|
|
|
|
pub fn default_settings(dir: impl AsRef<Path>) -> Opt {
|
|
|
|
Opt {
|
|
|
|
db_path: dir.as_ref().join("db"),
|
|
|
|
dumps_dir: dir.as_ref().join("dump"),
|
|
|
|
env: "development".to_owned(),
|
2021-06-16 23:12:49 +08:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
2022-01-12 22:57:31 +08:00
|
|
|
no_analytics: true,
|
2022-06-07 19:56:55 +08:00
|
|
|
max_index_size: Byte::from_unit(100.0, ByteUnit::MiB).unwrap(),
|
|
|
|
max_task_db_size: Byte::from_unit(1.0, ByteUnit::GiB).unwrap(),
|
2021-03-24 18:03:01 +08:00
|
|
|
http_payload_size_limit: Byte::from_unit(10.0, ByteUnit::MiB).unwrap(),
|
|
|
|
snapshot_dir: ".".into(),
|
2021-09-06 19:46:19 +08:00
|
|
|
indexer_options: IndexerOpts {
|
|
|
|
// memory has to be unlimited because several meilisearch are running in test context.
|
2022-03-25 02:52:36 +08:00
|
|
|
max_indexing_memory: MaxMemory::unlimited(),
|
2022-03-21 19:10:12 +08:00
|
|
|
..Parser::parse_from(None as Option<&str>)
|
2021-09-06 19:46:19 +08:00
|
|
|
},
|
2022-08-24 23:03:43 +08:00
|
|
|
enable_metrics_route: true,
|
2022-03-21 17:50:55 +08:00
|
|
|
..Parser::parse_from(None as Option<&str>)
|
2021-03-24 18:03:01 +08:00
|
|
|
}
|
|
|
|
}
|