2020-12-22 21:02:41 +08:00
|
|
|
use std::env;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
2021-03-10 18:56:51 +08:00
|
|
|
use actix_web::HttpServer;
|
2020-12-12 20:32:06 +08:00
|
|
|
use main_error::MainError;
|
2021-06-23 20:48:33 +08:00
|
|
|
use meilisearch_http::{create_app, Data, Opt};
|
2020-12-12 20:32:06 +08:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2021-06-16 23:12:49 +08:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
2021-06-15 21:36:30 +08:00
|
|
|
use meilisearch_http::analytics;
|
2021-06-28 19:23:13 +08:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
|
|
|
use std::sync::Arc;
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
|
|
|
|
2021-06-15 21:36:30 +08:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
|
|
|
const SENTRY_DSN: &str = "https://5ddfa22b95f241198be2271aaf028653@sentry.io/3060337";
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> Result<(), MainError> {
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
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
|
|
|
|
|
|
|
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(),
|
|
|
|
);
|
|
|
|
}
|
2021-06-15 23:39:07 +08:00
|
|
|
|
2021-06-15 21:36:30 +08:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
|
|
|
if !opt.no_analytics {
|
|
|
|
let logger =
|
|
|
|
sentry::integrations::log::SentryLogger::with_dest(log_builder.build());
|
2021-05-31 19:59:31 +08:00
|
|
|
log::set_boxed_logger(Box::new(logger))
|
|
|
|
.map(|()| log::set_max_level(log::LevelFilter::Info))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let sentry = sentry::init(sentry::ClientOptions {
|
|
|
|
release: sentry::release_name!(),
|
2021-06-15 21:36:30 +08:00
|
|
|
dsn: Some(SENTRY_DSN.parse()?),
|
2021-06-28 19:23:13 +08:00
|
|
|
before_send: Some(Arc::new(|event| {
|
2021-07-21 17:50:54 +08:00
|
|
|
if matches!(event.message, Some(ref msg) if msg.to_lowercase().contains("no space left on device"))
|
|
|
|
{
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(event)
|
|
|
|
}
|
2021-06-28 19:23:13 +08:00
|
|
|
})),
|
2021-05-31 19:59:31 +08:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
// sentry must stay alive as long as possible
|
|
|
|
std::mem::forget(sentry);
|
|
|
|
} else {
|
|
|
|
log_builder.init();
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"development" => {
|
2021-05-31 19:59:31 +08:00
|
|
|
log_builder.init();
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = Data::new(opt.clone())?;
|
|
|
|
|
2021-06-16 23:12:49 +08:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
2021-06-15 21:36:30 +08:00
|
|
|
if !opt.no_analytics {
|
|
|
|
let analytics_data = data.clone();
|
|
|
|
let analytics_opt = opt.clone();
|
|
|
|
tokio::task::spawn(analytics::analytics_sender(analytics_data, analytics_opt));
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
print_launch_resume(&opt, &data);
|
|
|
|
|
2021-04-22 18:39:23 +08:00
|
|
|
run_http(data, opt).await?;
|
2021-02-26 16:10:04 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:39:23 +08:00
|
|
|
async fn run_http(data: Data, opt: Opt) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let _enable_dashboard = &opt.env == "development";
|
2021-06-23 19:21:48 +08:00
|
|
|
let http_server = HttpServer::new(move || create_app!(data, _enable_dashboard))
|
2021-03-16 01:11:10 +08:00
|
|
|
// 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 {
|
|
|
|
http_server.bind(opt.http_addr)?.run().await?;
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_launch_resume(opt: &Opt, data: &Data) {
|
2021-04-09 14:03:25 +08:00
|
|
|
let commit_sha = match option_env!("COMMIT_SHA") {
|
2021-07-26 21:25:30 +08:00
|
|
|
Some("") | None => env!("VERGEN_GIT_SHA"),
|
2021-04-22 18:39:23 +08:00
|
|
|
Some(commit_sha) => commit_sha,
|
2021-04-09 14:03:25 +08:00
|
|
|
};
|
2021-07-29 01:12:40 +08:00
|
|
|
let commit_date = match option_env!("COMMIT_DATE") {
|
|
|
|
Some("") | None => env!("VERGEN_GIT_COMMIT_TIMESTAMP"),
|
2021-04-22 18:39:23 +08:00
|
|
|
Some(commit_date) => commit_date,
|
2021-04-09 14:03:25 +08:00
|
|
|
};
|
|
|
|
|
2020-12-12 20:32:06 +08:00
|
|
|
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
|
|
|
|
"#;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if opt.no_analytics {
|
|
|
|
eprintln!("Anonymous telemetry:\t\"Disabled\"");
|
2020-12-12 20:32:06 +08:00
|
|
|
} else {
|
2021-06-15 21:36:30 +08:00
|
|
|
eprintln!(
|
|
|
|
"
|
|
|
|
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-06-16 17:12:46 +08:00
|
|
|
Anonymous telemetry: \"Enabled\""
|
2021-06-15 21:36:30 +08:00
|
|
|
);
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
2021-06-15 21:36:30 +08:00
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
eprintln!();
|
|
|
|
|
2020-12-23 00:53:13 +08:00
|
|
|
if data.api_keys().master.is_some() {
|
2020-12-12 20:32:06 +08:00
|
|
|
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. \
|
|
|
|
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");
|
|
|
|
eprintln!("Contact:\t\thttps://docs.meilisearch.com/resources/contact.html or bonjour@meilisearch.com");
|
|
|
|
eprintln!();
|
|
|
|
}
|