reimplement Data

This commit is contained in:
mpostma 2020-12-22 17:53:13 +01:00
parent 55e1552957
commit 0d7c4beecd
5 changed files with 44 additions and 23 deletions

View File

@ -1,6 +1,4 @@
use std::error::Error;
use std::ops::Deref; use std::ops::Deref;
use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use sha2::Digest; use sha2::Digest;
@ -25,13 +23,9 @@ impl Deref for Data {
#[derive(Clone)] #[derive(Clone)]
pub struct DataInner { pub struct DataInner {
pub indexes: Arc<Index>, pub indexes: Arc<Index>,
pub update_store: UpdateQueue, pub update_queue: UpdateQueue,
pub db_path: String, api_keys: ApiKeys,
pub dumps_dir: PathBuf, options: Opt,
pub dump_batch_size: usize,
pub api_keys: ApiKeys,
pub server_pid: u32,
pub http_payload_size_limit: usize,
} }
#[derive(Clone)] #[derive(Clone)]
@ -59,7 +53,34 @@ impl ApiKeys {
} }
impl Data { impl Data {
pub fn new(_opt: Opt) -> Result<Data, Box<dyn Error>> { pub fn new(options: Opt) -> anyhow::Result<Data> {
todo!() 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
} }
} }

View File

@ -65,7 +65,7 @@ where
// it means that actix-web has an issue or someone changes the type `Data`. // it means that actix-web has an issue or someone changes the type `Data`.
let data = req.app_data::<web::Data<Data>>().unwrap(); let data = req.app_data::<web::Data<Data>>().unwrap();
if data.api_keys.master.is_none() { if data.api_keys().master.is_none() {
return Box::pin(svc.call(req)); return Box::pin(svc.call(req));
} }
@ -80,15 +80,15 @@ where
}; };
let authenticated = match self.acl { 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 => { Authentication::Private => {
data.api_keys.master.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().private.as_deref() == Some(auth_header)
} }
Authentication::Public => { Authentication::Public => {
data.api_keys.master.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().private.as_deref() == Some(auth_header)
|| data.api_keys.public.as_deref() == Some(auth_header) || data.api_keys().public.as_deref() == Some(auth_header)
} }
}; };

View File

@ -35,7 +35,7 @@ pub fn create_app(
.data(data.clone()) .data(data.clone())
.app_data( .app_data(
web::JsonConfig::default() web::JsonConfig::default()
.limit(data.http_payload_size_limit) .limit(data.http_payload_size_limit())
.content_type(|_mime| true) // Accept all mime types .content_type(|_mime| true) // Accept all mime types
.error_handler(|err, _req| payload_error_handler(err).into()), .error_handler(|err, _req| payload_error_handler(err).into()),
) )

View File

@ -147,7 +147,7 @@ pub fn print_launch_resume(opt: &Opt, data: &Data) {
eprintln!(); 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."); eprintln!("A Master Key has been set. Requests to MeiliSearch won't be authorized unless you provide an authentication key.");
} else { } else {
eprintln!("No master key found; The server will accept unidentified requests. \ eprintln!("No master key found; The server will accept unidentified requests. \

View File

@ -3,7 +3,6 @@ mod settings;
pub use settings::{Settings, Facets}; pub use settings::{Settings, Facets};
use std::io; use std::io;
use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
@ -41,6 +40,7 @@ enum UpdateMetaProgress {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
#[allow(dead_code)]
enum UpdateStatus<M, P, N> { enum UpdateStatus<M, P, N> {
Pending { update_id: u64, meta: M }, Pending { update_id: u64, meta: M },
Progressing { update_id: u64, meta: P }, Progressing { update_id: u64, meta: P },
@ -331,8 +331,8 @@ impl Handler<UpdateMeta, String> for UpdateHandler {
} }
impl UpdateQueue { impl UpdateQueue {
pub fn new<P: AsRef<Path>>( pub fn new(
opt: Opt, opt: &Opt,
indexes: Arc<Index>, indexes: Arc<Index>,
) -> Result<Self> { ) -> Result<Self> {
let (sender, _) = broadcast::channel(100); let (sender, _) = broadcast::channel(100);