From 0d7c4beecdd7e25e255c0d8ea651d4bdb2cfdbcf Mon Sep 17 00:00:00 2001 From: mpostma Date: Tue, 22 Dec 2020 17:53:13 +0100 Subject: [PATCH] reimplement Data --- src/data.rs | 43 ++++++++++++++++++++++++++--------- src/helpers/authentication.rs | 14 ++++++------ src/lib.rs | 2 +- src/main.rs | 2 +- src/updates/mod.rs | 6 ++--- 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/data.rs b/src/data.rs index 2b0ece53a..621d8cf2b 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,6 +1,4 @@ -use std::error::Error; use std::ops::Deref; -use std::path::PathBuf; use std::sync::Arc; use sha2::Digest; @@ -25,13 +23,9 @@ impl Deref for Data { #[derive(Clone)] pub struct DataInner { pub indexes: Arc, - pub update_store: UpdateQueue, - pub db_path: String, - pub dumps_dir: PathBuf, - pub dump_batch_size: usize, - pub api_keys: ApiKeys, - pub server_pid: u32, - pub http_payload_size_limit: usize, + pub update_queue: UpdateQueue, + api_keys: ApiKeys, + options: Opt, } #[derive(Clone)] @@ -59,7 +53,34 @@ impl ApiKeys { } impl Data { - pub fn new(_opt: Opt) -> Result> { - todo!() + pub fn new(options: Opt) -> anyhow::Result { + let db_size = options.max_mdb_size.get_bytes() as usize; + let path = options.db_path.join("main"); + let indexes = Index::new(&path, Some(db_size))?; + let indexes = Arc::new(indexes); + + let update_queue = UpdateQueue::new(&options, indexes.clone())?; + + let mut api_keys = ApiKeys { + master: options.clone().master_key, + private: None, + public: None, + }; + + api_keys.generate_missing_api_keys(); + + let inner = DataInner { indexes, options, update_queue, api_keys }; + let inner = Arc::new(inner); + + Ok(Data { inner }) + } + + #[inline] + pub fn http_payload_size_limit(&self) -> usize { + self.options.http_payload_size_limit.get_bytes() as usize + } + + pub fn api_keys(&self) -> &ApiKeys { + &self.api_keys } } diff --git a/src/helpers/authentication.rs b/src/helpers/authentication.rs index 974c622f0..0de709ba3 100644 --- a/src/helpers/authentication.rs +++ b/src/helpers/authentication.rs @@ -65,7 +65,7 @@ where // it means that actix-web has an issue or someone changes the type `Data`. let data = req.app_data::>().unwrap(); - if data.api_keys.master.is_none() { + if data.api_keys().master.is_none() { return Box::pin(svc.call(req)); } @@ -80,15 +80,15 @@ where }; let authenticated = match self.acl { - Authentication::Admin => data.api_keys.master.as_deref() == Some(auth_header), + Authentication::Admin => data.api_keys().master.as_deref() == Some(auth_header), Authentication::Private => { - data.api_keys.master.as_deref() == Some(auth_header) - || data.api_keys.private.as_deref() == Some(auth_header) + data.api_keys().master.as_deref() == Some(auth_header) + || data.api_keys().private.as_deref() == Some(auth_header) } Authentication::Public => { - data.api_keys.master.as_deref() == Some(auth_header) - || data.api_keys.private.as_deref() == Some(auth_header) - || data.api_keys.public.as_deref() == Some(auth_header) + data.api_keys().master.as_deref() == Some(auth_header) + || data.api_keys().private.as_deref() == Some(auth_header) + || data.api_keys().public.as_deref() == Some(auth_header) } }; diff --git a/src/lib.rs b/src/lib.rs index 0f2aabccd..2c625588d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,7 @@ pub fn create_app( .data(data.clone()) .app_data( web::JsonConfig::default() - .limit(data.http_payload_size_limit) + .limit(data.http_payload_size_limit()) .content_type(|_mime| true) // Accept all mime types .error_handler(|err, _req| payload_error_handler(err).into()), ) diff --git a/src/main.rs b/src/main.rs index e5b167cd0..7e50225be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,7 +147,7 @@ pub fn print_launch_resume(opt: &Opt, data: &Data) { eprintln!(); - if data.api_keys.master.is_some() { + if data.api_keys().master.is_some() { eprintln!("A Master Key has been set. Requests to MeiliSearch won't be authorized unless you provide an authentication key."); } else { eprintln!("No master key found; The server will accept unidentified requests. \ diff --git a/src/updates/mod.rs b/src/updates/mod.rs index dd76ed1aa..30490ec43 100644 --- a/src/updates/mod.rs +++ b/src/updates/mod.rs @@ -3,7 +3,6 @@ mod settings; pub use settings::{Settings, Facets}; use std::io; -use std::path::Path; use std::sync::Arc; use anyhow::Result; @@ -41,6 +40,7 @@ enum UpdateMetaProgress { #[derive(Debug, Clone, Serialize)] #[serde(tag = "type")] +#[allow(dead_code)] enum UpdateStatus { Pending { update_id: u64, meta: M }, Progressing { update_id: u64, meta: P }, @@ -331,8 +331,8 @@ impl Handler for UpdateHandler { } impl UpdateQueue { - pub fn new>( - opt: Opt, + pub fn new( + opt: &Opt, indexes: Arc, ) -> Result { let (sender, _) = broadcast::channel(100);