From 4b798c71ae9123b29de28039b838d74760c61c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 19 Nov 2019 17:09:06 +0100 Subject: [PATCH] Introduce new arguments and understand env vars --- Cargo.lock | 28 ++------------- meilidb-http/Cargo.toml | 2 -- meilidb-http/src/data.rs | 10 +++--- meilidb-http/src/helpers/tide.rs | 6 ++-- meilidb-http/src/lib.rs | 3 -- meilidb-http/src/main.rs | 3 +- meilidb-http/src/option.rs | 60 ++++++-------------------------- meilidb-http/src/routes/stats.rs | 1 + 8 files changed, 24 insertions(+), 89 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5a881485f..d637974f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -370,24 +370,6 @@ dependencies = [ "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "envconfig" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "envconfig_derive" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "error-chain" version = "0.12.1" @@ -857,7 +839,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "meilidb-core" -version = "0.6.5" +version = "0.7.0" dependencies = [ "arc-swap 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -894,22 +876,20 @@ dependencies = [ [[package]] name = "meilidb-http" -version = "0.1.1" +version = "0.3.0" dependencies = [ "async-compression 0.1.0-alpha.7 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", - "envconfig 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "envconfig_derive 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "heed 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", "indexmap 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "jemallocator 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "main_error 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "meilidb-core 0.6.5", + "meilidb-core 0.7.0", "meilidb-schema 0.6.0", "pretty-bytes 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2314,8 +2294,6 @@ dependencies = [ "checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e" "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" -"checksum envconfig 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e84293f9f371139d8bb0913da796c565108c36109539e74f73c0d0b2fa117845" -"checksum envconfig_derive 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "38f6cf35a56246cdf91ef102367259e77b154a023d885aa518718266c4886228" "checksum error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ab49e9dcb602294bc42f9a7dfc9bc6e936fca4418ea300dbfb84fe16de0b7d9" "checksum failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f8273f13c977665c5db7eb2b99ae520952fe5ac831ae4cd09d80c4c7042b5ed9" "checksum failure_derive 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0bc225b78e0391e4b8683440bf2e63c2deeeb2ce5189eab46e2b68c6d3725d08" diff --git a/meilidb-http/Cargo.toml b/meilidb-http/Cargo.toml index 14301712a..190fe553b 100644 --- a/meilidb-http/Cargo.toml +++ b/meilidb-http/Cargo.toml @@ -12,8 +12,6 @@ bincode = "1.2.0" chrono = { version = "0.4.9", features = ["serde"] } crossbeam-channel = "0.4.0" env_logger = "0.7.1" -envconfig = "0.5.1" -envconfig_derive = "0.5.1" heed = "0.5.0" http = "0.1.19" indexmap = { version = "1.3.0", features = ["serde-1"] } diff --git a/meilidb-http/src/data.rs b/meilidb-http/src/data.rs index 35f16574e..c4fba4af9 100644 --- a/meilidb-http/src/data.rs +++ b/meilidb-http/src/data.rs @@ -32,7 +32,7 @@ impl Deref for Data { pub struct DataInner { pub db: Arc, pub db_path: String, - pub admin_token: Option, + pub api_key: Option, pub server_pid: Pid, } @@ -126,16 +126,16 @@ impl DataInner { impl Data { pub fn new(opt: Opt) -> Data { - let db_path = opt.database_path.clone(); - let admin_token = opt.admin_token.clone(); + let db_path = opt.db_path.clone(); + let api_key = opt.api_key.clone(); let server_pid = sysinfo::get_current_pid().unwrap(); - let db = Arc::new(Database::open_or_create(opt.database_path.clone()).unwrap()); + let db = Arc::new(Database::open_or_create(opt.db_path.clone()).unwrap()); let inner_data = DataInner { db: db.clone(), db_path, - admin_token, + api_key, server_pid, }; diff --git a/meilidb-http/src/helpers/tide.rs b/meilidb-http/src/helpers/tide.rs index e68da7852..3ede9d9f9 100644 --- a/meilidb-http/src/helpers/tide.rs +++ b/meilidb-http/src/helpers/tide.rs @@ -17,13 +17,13 @@ pub trait ContextExt { impl ContextExt for Context { fn is_allowed(&self, acl: ACL) -> SResult<()> { - let admin_token = match &self.state().admin_token { - Some(admin_token) => admin_token, + let api_key = match &self.state().api_key { + Some(api_key) => api_key, None => return Ok(()), }; let user_api_key = self.header("X-Meili-API-Key")?; - if user_api_key == *admin_token { + if user_api_key == *api_key { return Ok(()); } let request_index: Option = None; //self.param::("index").ok(); diff --git a/meilidb-http/src/lib.rs b/meilidb-http/src/lib.rs index 118af8dc1..199b780c4 100644 --- a/meilidb-http/src/lib.rs +++ b/meilidb-http/src/lib.rs @@ -1,6 +1,3 @@ -#[macro_use] -extern crate envconfig_derive; - pub mod data; pub mod error; pub mod helpers; diff --git a/meilidb-http/src/main.rs b/meilidb-http/src/main.rs index 1ad429649..ebf6adb5c 100644 --- a/meilidb-http/src/main.rs +++ b/meilidb-http/src/main.rs @@ -1,6 +1,7 @@ use http::header::HeaderValue; use log::info; use main_error::MainError; +use structopt::StructOpt; use tide::middleware::{CorsMiddleware, CorsOrigin}; use tide_log::RequestLogger; @@ -16,7 +17,7 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; pub fn main() -> Result<(), MainError> { env_logger::init(); - let opt = Opt::new(); + let opt = Opt::from_args(); let data = Data::new(opt.clone()); let data_cloned = data.clone(); diff --git a/meilidb-http/src/option.rs b/meilidb-http/src/option.rs index fee010733..2c6851b4a 100644 --- a/meilidb-http/src/option.rs +++ b/meilidb-http/src/option.rs @@ -1,56 +1,16 @@ -use envconfig::Envconfig; use structopt::StructOpt; -#[derive(Debug, Clone, StructOpt, Envconfig)] -struct Vars { - /// The destination where the database must be created. - #[structopt(long)] - #[envconfig(from = "MEILI_DATABASE_PATH")] - pub database_path: Option, - - /// The addr on which the http server will listen. - #[structopt(long)] - #[envconfig(from = "MEILI_HTTP_ADDR")] - pub http_addr: Option, - - #[structopt(long)] - #[envconfig(from = "MEILI_ADMIN_TOKEN")] - pub admin_token: Option, -} - -#[derive(Clone, Debug)] +#[derive(Debug, Clone, StructOpt)] pub struct Opt { - pub database_path: String, + /// The destination where the database must be created. + #[structopt(long, env = "MEILI_DB_PATH", default_value = "/tmp/meilidb")] + pub db_path: String, + + /// The address on which the http server will listen. + #[structopt(long, env = "MEILI_HTTP_ADDR", default_value = "127.0.0.1:8080")] pub http_addr: String, - pub admin_token: Option, -} -impl Default for Opt { - fn default() -> Self { - Opt { - database_path: String::from("/tmp/meilidb"), - http_addr: String::from("127.0.0.1:8080"), - admin_token: None, - } - } -} - -impl Opt { - pub fn new() -> Self { - let default = Self::default(); - let args = Vars::from_args(); - let env = Vars::init().unwrap(); - - Self { - database_path: env - .database_path - .or(args.database_path) - .unwrap_or(default.database_path), - http_addr: env - .http_addr - .or(args.http_addr) - .unwrap_or(default.http_addr), - admin_token: env.admin_token.or(args.admin_token).or(default.admin_token), - } - } + /// The master key allowing you to do everything on the server. + #[structopt(long, env = "MEILI_API_KEY")] + pub api_key: Option, } diff --git a/meilidb-http/src/routes/stats.rs b/meilidb-http/src/routes/stats.rs index bdc82b138..dd4d24653 100644 --- a/meilidb-http/src/routes/stats.rs +++ b/meilidb-http/src/routes/stats.rs @@ -69,6 +69,7 @@ struct StatsResult { pub async fn get_stats(ctx: Context) -> SResult { ctx.is_allowed(Admin)?; + let mut index_list = HashMap::new(); if let Ok(indexes_set) = ctx.state().db.indexes_names() {