2019-10-31 22:00:36 +08:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-05-22 18:35:23 +08:00
|
|
|
use meilisearch_core::{Database, DatabaseOptions};
|
2020-02-06 22:41:11 +08:00
|
|
|
use sha2::Digest;
|
2020-02-27 01:49:17 +08:00
|
|
|
use sysinfo::Pid;
|
2020-06-23 23:03:56 +08:00
|
|
|
use log::error;
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-04-15 16:51:15 +08:00
|
|
|
use crate::index_update_callback;
|
2019-10-31 22:00:36 +08:00
|
|
|
use crate::option::Opt;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Data {
|
|
|
|
inner: Arc<DataInner>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Data {
|
|
|
|
type Target = DataInner;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DataInner {
|
|
|
|
pub db: Arc<Database>,
|
|
|
|
pub db_path: String,
|
2020-02-06 22:41:11 +08:00
|
|
|
pub api_keys: ApiKeys,
|
2019-10-31 22:00:36 +08:00
|
|
|
pub server_pid: Pid,
|
2020-05-14 23:52:10 +08:00
|
|
|
pub http_payload_size_limit: usize,
|
2019-10-31 22:00:36 +08:00
|
|
|
}
|
|
|
|
|
2020-04-24 21:00:52 +08:00
|
|
|
#[derive(Clone)]
|
2020-02-06 22:41:11 +08:00
|
|
|
pub struct ApiKeys {
|
|
|
|
pub public: Option<String>,
|
|
|
|
pub private: Option<String>,
|
|
|
|
pub master: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiKeys {
|
|
|
|
pub fn generate_missing_api_keys(&mut self) {
|
|
|
|
if let Some(master_key) = &self.master {
|
|
|
|
if self.private.is_none() {
|
|
|
|
let key = format!("{}-private", master_key);
|
|
|
|
let sha = sha2::Sha256::digest(key.as_bytes());
|
|
|
|
self.private = Some(format!("{:x}", sha));
|
|
|
|
}
|
|
|
|
if self.public.is_none() {
|
|
|
|
let key = format!("{}-public", master_key);
|
|
|
|
let sha = sha2::Sha256::digest(key.as_bytes());
|
|
|
|
self.public = Some(format!("{:x}", sha));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:00:36 +08:00
|
|
|
impl Data {
|
|
|
|
pub fn new(opt: Opt) -> Data {
|
2019-11-20 00:09:06 +08:00
|
|
|
let db_path = opt.db_path.clone();
|
2019-10-31 22:00:36 +08:00
|
|
|
let server_pid = sysinfo::get_current_pid().unwrap();
|
|
|
|
|
2020-04-29 06:40:06 +08:00
|
|
|
let db_opt = DatabaseOptions {
|
|
|
|
main_map_size: opt.main_map_size,
|
2020-05-07 18:28:41 +08:00
|
|
|
update_map_size: opt.update_map_size,
|
2020-04-29 06:40:06 +08:00
|
|
|
};
|
|
|
|
|
2020-05-14 23:52:10 +08:00
|
|
|
let http_payload_size_limit = opt.http_payload_size_limit;
|
|
|
|
|
2020-06-23 23:03:56 +08:00
|
|
|
let db = match Database::open_or_create(opt.db_path, db_opt) {
|
|
|
|
Ok(db) => Arc::new(db),
|
|
|
|
Err(e) => {
|
|
|
|
error!("{}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
2019-10-31 22:00:36 +08:00
|
|
|
|
2020-02-06 22:41:11 +08:00
|
|
|
let mut api_keys = ApiKeys {
|
2020-04-11 01:05:05 +08:00
|
|
|
master: opt.master_key,
|
2020-02-06 22:41:11 +08:00
|
|
|
private: None,
|
|
|
|
public: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
api_keys.generate_missing_api_keys();
|
|
|
|
|
2019-10-31 22:00:36 +08:00
|
|
|
let inner_data = DataInner {
|
|
|
|
db: db.clone(),
|
|
|
|
db_path,
|
2020-02-06 22:41:11 +08:00
|
|
|
api_keys,
|
2019-10-31 22:00:36 +08:00
|
|
|
server_pid,
|
2020-05-14 23:52:10 +08:00
|
|
|
http_payload_size_limit,
|
2019-10-31 22:00:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let data = Data {
|
|
|
|
inner: Arc::new(inner_data),
|
|
|
|
};
|
|
|
|
|
2019-11-16 00:33:06 +08:00
|
|
|
let callback_context = data.clone();
|
2019-11-19 23:15:49 +08:00
|
|
|
db.set_update_callback(Box::new(move |index_uid, status| {
|
2020-04-08 20:13:45 +08:00
|
|
|
index_update_callback(&index_uid, &callback_context, status);
|
2019-11-16 00:33:06 +08:00
|
|
|
}));
|
2019-10-31 22:00:36 +08:00
|
|
|
|
|
|
|
data
|
|
|
|
}
|
|
|
|
}
|