2020-12-22 21:02:41 +08:00
|
|
|
use std::env;
|
2021-10-29 21:58:06 +08:00
|
|
|
use std::sync::Arc;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-03-10 18:56:51 +08:00
|
|
|
use actix_web::HttpServer;
|
2022-01-12 20:54:39 +08:00
|
|
|
use clap::Parser;
|
2021-11-09 01:31:27 +08:00
|
|
|
use meilisearch_auth::AuthController;
|
2021-10-12 19:31:56 +08:00
|
|
|
use meilisearch_http::analytics;
|
|
|
|
use meilisearch_http::analytics::Analytics;
|
2021-09-29 04:22:59 +08:00
|
|
|
use meilisearch_http::{create_app, setup_meilisearch, Opt};
|
2021-09-21 19:23:22 +08:00
|
|
|
use meilisearch_lib::MeiliSearch;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[global_allocator]
|
2021-09-10 06:20:46 +08:00
|
|
|
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-09-27 22:48:03 +08:00
|
|
|
/// does all the setup before meilisearch is launched
|
2021-09-15 00:39:02 +08:00
|
|
|
fn setup(opt: &Opt) -> anyhow::Result<()> {
|
2021-06-23 17:07:19 +08:00
|
|
|
let mut log_builder = env_logger::Builder::new();
|
|
|
|
log_builder.parse_filters(&opt.log_level);
|
2021-06-28 19:23:13 +08:00
|
|
|
if opt.log_level == "info" {
|
2021-06-23 17:07:19 +08:00
|
|
|
// if we are in info we only allow the warn log_level for milli
|
|
|
|
log_builder.filter_module("milli", log::LevelFilter::Warn);
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-08-24 18:27:30 +08:00
|
|
|
log_builder.init();
|
|
|
|
|
2021-09-27 22:48:03 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:39:02 +08:00
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
2022-01-12 20:54:39 +08:00
|
|
|
let opt = Opt::parse();
|
2021-09-15 00:39:02 +08:00
|
|
|
|
|
|
|
setup(&opt)?;
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
match opt.env.as_ref() {
|
|
|
|
"production" => {
|
|
|
|
if opt.master_key.is_none() {
|
2021-09-29 04:22:59 +08:00
|
|
|
anyhow::bail!(
|
2020-12-12 20:32:06 +08:00
|
|
|
"In production mode, the environment variable MEILI_MASTER_KEY is mandatory"
|
2021-09-29 04:22:59 +08:00
|
|
|
)
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-24 18:27:30 +08:00
|
|
|
"development" => (),
|
2020-12-12 20:32:06 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:23:22 +08:00
|
|
|
let meilisearch = setup_meilisearch(&opt)?;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
let auth_controller = AuthController::new(&opt.db_path, &opt.master_key)?;
|
|
|
|
|
2021-06-16 23:12:49 +08:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
2022-01-12 22:57:31 +08:00
|
|
|
let (analytics, user) = if !opt.no_analytics {
|
2021-10-29 21:58:06 +08:00
|
|
|
analytics::SegmentAnalytics::new(&opt, &meilisearch).await
|
2021-10-12 19:31:56 +08:00
|
|
|
} else {
|
2021-10-29 22:46:00 +08:00
|
|
|
analytics::MockAnalytics::new(&opt)
|
2021-10-12 19:31:56 +08:00
|
|
|
};
|
|
|
|
#[cfg(any(debug_assertions, not(feature = "analytics")))]
|
2021-10-29 21:58:06 +08:00
|
|
|
let (analytics, user) = analytics::MockAnalytics::new(&opt);
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-10-29 21:58:06 +08:00
|
|
|
print_launch_resume(&opt, &user);
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
run_http(meilisearch, auth_controller, opt, analytics).await?;
|
2021-02-26 16:10:04 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-12 20:32:44 +08:00
|
|
|
async fn run_http(
|
|
|
|
data: MeiliSearch,
|
2021-11-09 01:31:27 +08:00
|
|
|
auth_controller: AuthController,
|
2021-10-12 20:32:44 +08:00
|
|
|
opt: Opt,
|
2021-10-29 21:58:06 +08:00
|
|
|
analytics: Arc<dyn Analytics>,
|
2021-10-12 20:32:44 +08:00
|
|
|
) -> anyhow::Result<()> {
|
2021-04-22 18:39:23 +08:00
|
|
|
let _enable_dashboard = &opt.env == "development";
|
2021-09-20 21:31:03 +08:00
|
|
|
let opt_clone = opt.clone();
|
2021-11-09 01:31:27 +08:00
|
|
|
let http_server = HttpServer::new(move || {
|
|
|
|
create_app!(
|
|
|
|
data,
|
|
|
|
auth_controller,
|
|
|
|
_enable_dashboard,
|
|
|
|
opt_clone,
|
|
|
|
analytics.clone()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
// Disable signals allows the server to terminate immediately when a user enter CTRL-C
|
|
|
|
.disable_signals();
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
if let Some(config) = opt.get_ssl_config()? {
|
|
|
|
http_server
|
|
|
|
.bind_rustls(opt.http_addr, config)?
|
|
|
|
.run()
|
|
|
|
.await?;
|
2021-06-23 20:48:33 +08:00
|
|
|
} else {
|
2021-09-20 21:31:03 +08:00
|
|
|
http_server.bind(&opt.http_addr)?.run().await?;
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:58:06 +08:00
|
|
|
pub fn print_launch_resume(opt: &Opt, user: &str) {
|
2021-08-30 23:41:24 +08:00
|
|
|
let commit_sha = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
|
|
|
|
let commit_date = option_env!("VERGEN_GIT_COMMIT_TIMESTAMP").unwrap_or("unknown");
|
2021-04-09 14:03:25 +08:00
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
let ascii_name = r#"
|
2022-01-27 01:53:07 +08:00
|
|
|
888b d888 d8b 888 d8b 888
|
|
|
|
8888b d8888 Y8P 888 Y8P 888
|
|
|
|
88888b.d88888 888 888
|
|
|
|
888Y88888P888 .d88b. 888 888 888 .d8888b .d88b. 8888b. 888d888 .d8888b 88888b.
|
|
|
|
888 Y888P 888 d8P Y8b 888 888 888 88K d8P Y8b "88b 888P" d88P" 888 "88b
|
|
|
|
888 Y8P 888 88888888 888 888 888 "Y8888b. 88888888 .d888888 888 888 888 888
|
|
|
|
888 " 888 Y8b. 888 888 888 X88 Y8b. 888 888 888 Y88b. 888 888
|
|
|
|
888 888 "Y8888 888 888 888 88888P' "Y8888 "Y888888 888 "Y8888P 888 888
|
2020-12-12 20:32:06 +08:00
|
|
|
"#;
|
|
|
|
|
|
|
|
eprintln!("{}", ascii_name);
|
|
|
|
|
|
|
|
eprintln!("Database path:\t\t{:?}", opt.db_path);
|
2021-03-26 19:09:51 +08:00
|
|
|
eprintln!("Server listening on:\t\"http://{}\"", opt.http_addr);
|
2020-12-12 20:32:06 +08:00
|
|
|
eprintln!("Environment:\t\t{:?}", opt.env);
|
2021-04-09 14:03:25 +08:00
|
|
|
eprintln!("Commit SHA:\t\t{:?}", commit_sha.to_string());
|
|
|
|
eprintln!("Commit date:\t\t{:?}", commit_date.to_string());
|
2020-12-12 20:32:06 +08:00
|
|
|
eprintln!(
|
|
|
|
"Package version:\t{:?}",
|
|
|
|
env!("CARGO_PKG_VERSION").to_string()
|
|
|
|
);
|
|
|
|
|
2021-06-16 23:12:49 +08:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
2021-06-15 21:36:30 +08:00
|
|
|
{
|
2022-01-12 22:57:31 +08:00
|
|
|
if !opt.no_analytics {
|
2021-06-15 21:36:30 +08:00
|
|
|
eprintln!(
|
|
|
|
"
|
2022-01-27 00:43:16 +08:00
|
|
|
Thank you for using Meilisearch!
|
2021-06-16 17:12:46 +08:00
|
|
|
|
2021-08-13 00:58:07 +08:00
|
|
|
We collect anonymized analytics to improve our product and your experience. To learn more, including how to turn off analytics, visit our dedicated documentation page: https://docs.meilisearch.com/learn/what_is_meilisearch/telemetry.html
|
2021-06-15 21:36:30 +08:00
|
|
|
|
2021-10-29 00:39:50 +08:00
|
|
|
Anonymous telemetry:\t\"Enabled\""
|
2021-06-15 21:36:30 +08:00
|
|
|
);
|
2021-12-08 18:23:16 +08:00
|
|
|
} else {
|
|
|
|
eprintln!("Anonymous telemetry:\t\"Disabled\"");
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
2021-06-15 21:36:30 +08:00
|
|
|
}
|
2021-10-14 01:38:14 +08:00
|
|
|
|
2021-10-29 21:58:06 +08:00
|
|
|
if !user.is_empty() {
|
|
|
|
eprintln!("Instance UID:\t\t\"{}\"", user);
|
2021-10-14 01:38:14 +08:00
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
eprintln!();
|
|
|
|
|
2021-09-20 21:31:03 +08:00
|
|
|
if opt.master_key.is_some() {
|
2022-01-27 00:43:16 +08:00
|
|
|
eprintln!("A Master Key has been set. Requests to Meilisearch won't be authorized unless you provide an authentication key.");
|
2020-12-12 20:32:06 +08:00
|
|
|
} else {
|
|
|
|
eprintln!("No master key found; The server will accept unidentified requests. \
|
|
|
|
If you need some protection in development mode, please export a key: export MEILI_MASTER_KEY=xxx");
|
|
|
|
}
|
|
|
|
|
|
|
|
eprintln!();
|
|
|
|
eprintln!("Documentation:\t\thttps://docs.meilisearch.com");
|
|
|
|
eprintln!("Source code:\t\thttps://github.com/meilisearch/meilisearch");
|
2021-11-11 21:36:45 +08:00
|
|
|
eprintln!("Contact:\t\thttps://docs.meilisearch.com/resources/contact.html");
|
2020-12-12 20:32:06 +08:00
|
|
|
eprintln!();
|
|
|
|
}
|