From ad83c3ab5a91e16d082297bdf88c1a3cd1dd2171 Mon Sep 17 00:00:00 2001 From: qdequele Date: Thu, 6 Feb 2020 15:42:35 +0100 Subject: [PATCH] add launch resume & environment --- meilisearch-http/src/main.rs | 66 +++++++++++++++++++++++++++++--- meilisearch-http/src/option.rs | 9 +++++ meilisearch-http/tests/common.rs | 1 + 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/meilisearch-http/src/main.rs b/meilisearch-http/src/main.rs index 84bdddf4b..7d24e53a0 100644 --- a/meilisearch-http/src/main.rs +++ b/meilisearch-http/src/main.rs @@ -1,4 +1,3 @@ -use std::env::VarError::NotPresent; use std::{env, thread}; use async_std::task; @@ -19,20 +18,35 @@ mod analytics; static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; pub fn main() -> Result<(), MainError> { - env_logger::init(); let opt = Opt::from_args(); - let data = Data::new(opt.clone()); - if env::var("MEILI_NO_ANALYTICS") == Err(NotPresent) { + match opt.env.as_ref() { + "production" => { + if opt.master_key.is_none() { + return Err("In production mode, the environment variable MEILI_MASTER_KEY is mandatory".into()); + } + env_logger::init(); + }, + "development" => { + env_logger::from_env(env_logger::Env::default().default_filter_or("info")).init(); + }, + _ => unreachable!(), + } + + if !opt.no_analytics { thread::spawn(analytics::analytics_sender); } + let data = Data::new(opt.clone()); + let data_cloned = data.clone(); data.db.set_update_callback(Box::new(move |name, status| { index_update_callback(name, &data_cloned, status); })); + print_launch_resume(&opt, &data); + let mut app = tide::with_state(data); app.middleware(Cors::new()); @@ -40,8 +54,48 @@ pub fn main() -> Result<(), MainError> { routes::load_routes(&mut app); - info!("Server HTTP enabled"); - task::block_on(app.listen(opt.http_addr))?; Ok(()) } + + +pub fn print_launch_resume(opt: &Opt, data: &Data) { + let ascii_name = r#" +888b d888 d8b 888 d8b .d8888b. 888 +8888b d8888 Y8P 888 Y8P d88P Y88b 888 +88888b.d88888 888 Y88b. 888 +888Y88888P888 .d88b. 888 888 888 "Y888b. .d88b. 8888b. 888d888 .d8888b 88888b. +888 Y888P 888 d8P Y8b 888 888 888 "Y88b. d8P Y8b "88b 888P" d88P" 888 "88b +888 Y8P 888 88888888 888 888 888 "888 88888888 .d888888 888 888 888 888 +888 " 888 Y8b. 888 888 888 Y88b d88P Y8b. 888 888 888 Y88b. 888 888 +888 888 "Y8888 888 888 888 "Y8888P" "Y8888 "Y888888 888 "Y8888P 888 888 +"#; + + println!("{}", ascii_name); + + info!("Database path: {:?}", opt.db_path); + info!("Start server on: {:?}", opt.http_addr); + info!("Environment: {:?}", opt.env); + info!("Commit SHA: {:?}", env!("VERGEN_SHA").to_string()); + info!("Build date: {:?}", env!("VERGEN_BUILD_TIMESTAMP").to_string()); + info!("Package version: {:?}", env!("CARGO_PKG_VERSION").to_string()); + + if let Some(master_key) = &data.api_keys.master { + info!("Master Key: {:?}", master_key); + + if let Some(private_key) = &data.api_keys.private { + info!("Private Key: {:?}", private_key); + } + + if let Some(public_key) = &data.api_keys.public { + info!("Public Key: {:?}", public_key); + } + } else { + info!("No master key found; The server will have no securities.\ + If you need some protection in development mode, please export a key. export MEILI_MASTER_KEY=xxx"); + } + + info!("If you need extra information; Please refer to the documentation: http://docs.meilisearch.com"); + info!("If you want to support us or help us; Please consult our Github repo: http://github.com/meilisearch/meilisearch"); + info!("If you want to contact us; Please chat with us on http://meilisearch.com or by email to bonjour@meilisearch.com"); +} diff --git a/meilisearch-http/src/option.rs b/meilisearch-http/src/option.rs index 31cfab361..34a036732 100644 --- a/meilisearch-http/src/option.rs +++ b/meilisearch-http/src/option.rs @@ -1,5 +1,7 @@ use structopt::StructOpt; +const POSSIBLE_ENV: [&str; 2] = ["development", "production"]; + #[derive(Debug, Clone, StructOpt)] pub struct Opt { /// The destination where the database must be created. @@ -14,6 +16,13 @@ pub struct Opt { #[structopt(long, env = "MEILI_MASTER_KEY")] pub master_key: Option, + /// This environment variable must be set to `production` if your are running in production. + /// Could be `production` or `development` + /// - `production`: Force api keys + /// - `development`: Show logs in "info" mode + not mendatory to specify the api keys + #[structopt(long, env = "MEILI_ENV", default_value = "development", possible_values = &POSSIBLE_ENV)] + pub env: String, + /// Do not send analytics to Meili. #[structopt(long, env = "MEILI_NO_ANALYTICS")] pub no_analytics: bool, diff --git a/meilisearch-http/tests/common.rs b/meilisearch-http/tests/common.rs index 2f12d511e..90251b602 100644 --- a/meilisearch-http/tests/common.rs +++ b/meilisearch-http/tests/common.rs @@ -23,6 +23,7 @@ pub fn setup_server() -> Result>, Box> { db_path: tmp_dir.path().to_str().unwrap().to_string(), http_addr: "127.0.0.1:7700".to_owned(), master_key: None, + env: "development".to_owned(), no_analytics: true, };