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::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<Index>,
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<Data, Box<dyn Error>> {
todo!()
pub fn new(options: Opt) -> anyhow::Result<Data> {
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`.
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));
}
@ -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)
}
};

View File

@ -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()),
)

View File

@ -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. \

View File

@ -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<M, P, N> {
Pending { update_id: u64, meta: M },
Progressing { update_id: u64, meta: P },
@ -331,8 +331,8 @@ impl Handler<UpdateMeta, String> for UpdateHandler {
}
impl UpdateQueue {
pub fn new<P: AsRef<Path>>(
opt: Opt,
pub fn new(
opt: &Opt,
indexes: Arc<Index>,
) -> Result<Self> {
let (sender, _) = broadcast::channel(100);