193 lines
7.2 KiB
Rust
Raw Normal View History

2020-12-22 14:02:41 +01:00
use std::env;
2022-09-19 18:16:28 +02:00
use std::path::PathBuf;
use std::sync::Arc;
2020-12-12 13:32:06 +01:00
2022-06-07 03:38:23 +08:00
use actix_web::http::KeepAlive;
2022-09-27 16:33:37 +02:00
use actix_web::web::Data;
use actix_web::HttpServer;
2022-09-27 16:33:37 +02:00
use index_scheduler::IndexScheduler;
use meilisearch::analytics::Analytics;
use meilisearch::{analytics, create_app, setup_meilisearch, Opt};
2022-12-22 11:57:44 +01:00
use meilisearch_auth::{generate_master_key, AuthController, MASTER_KEY_MIN_SIZE};
2020-12-12 13:32:06 +01:00
#[global_allocator]
2022-08-23 12:58:10 +02:00
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
2020-12-12 13:32:06 +01:00
2021-09-27 16:48:03 +02:00
/// does all the setup before meilisearch is launched
2021-09-14 18:39:02 +02:00
fn setup(opt: &Opt) -> anyhow::Result<()> {
let mut log_builder = env_logger::Builder::new();
log_builder.parse_filters(&opt.log_level.to_string());
2020-12-12 13:32:06 +01:00
2021-08-24 12:27:30 +02:00
log_builder.init();
2021-09-27 16:48:03 +02:00
Ok(())
}
2021-09-14 18:39:02 +02:00
#[actix_web::main]
async fn main() -> anyhow::Result<()> {
2022-09-19 18:16:28 +02:00
let (opt, config_read_from) = Opt::try_build()?;
2022-09-06 09:23:16 +02:00
2021-09-14 18:39:02 +02:00
setup(&opt)?;
2022-12-22 11:57:44 +01:00
match (opt.env.as_ref(), &opt.master_key) {
("production", Some(master_key)) if master_key.len() < MASTER_KEY_MIN_SIZE => {
anyhow::bail!(
2023-01-03 14:50:50 +01:00
"In production mode, the master key must be of at least {MASTER_KEY_MIN_SIZE} bytes, but the provided key is only {} bytes long
2022-12-22 11:57:44 +01:00
2023-01-03 14:45:23 +01:00
We generated a secure master key for you (you can safely copy this token):
2022-12-22 11:57:44 +01:00
>> export MEILI_MASTER_KEY={} <<",
master_key.len(),
generate_master_key(),
)
2020-12-12 13:32:06 +01:00
}
2022-12-22 11:57:44 +01:00
("production", None) => {
anyhow::bail!(
"In production mode, you must provide a master key to secure your instance. It can be specified via the MEILI_MASTER_KEY environment variable or the --master-key launch option.
2022-12-22 11:57:44 +01:00
2023-01-03 14:45:23 +01:00
We generated a secure master key for you (you can safely copy this token):
2022-12-22 11:57:44 +01:00
>> export MEILI_MASTER_KEY={} <<
",
generate_master_key()
)
}
// No error; continue
_ => (),
2020-12-12 13:32:06 +01:00
}
let (index_scheduler, auth_controller) = setup_meilisearch(&opt)?;
2021-06-16 17:12:49 +02:00
#[cfg(all(not(debug_assertions), feature = "analytics"))]
2022-10-13 15:02:59 +02:00
let analytics = if !opt.no_analytics {
2022-10-18 12:45:06 +02:00
analytics::SegmentAnalytics::new(&opt, index_scheduler.clone()).await
} else {
2021-10-29 16:46:00 +02:00
analytics::MockAnalytics::new(&opt)
};
#[cfg(any(debug_assertions, not(feature = "analytics")))]
2022-10-13 15:02:59 +02:00
let analytics = analytics::MockAnalytics::new(&opt);
2020-12-12 13:32:06 +01:00
2022-10-13 15:02:59 +02:00
print_launch_resume(&opt, analytics.clone(), config_read_from);
2020-12-12 13:32:06 +01:00
2022-09-27 16:33:37 +02:00
run_http(index_scheduler, auth_controller, opt, analytics).await?;
2021-02-26 09:10:04 +01:00
Ok(())
}
async fn run_http(
2022-10-18 12:45:06 +02:00
index_scheduler: Arc<IndexScheduler>,
auth_controller: AuthController,
opt: Opt,
analytics: Arc<dyn Analytics>,
) -> anyhow::Result<()> {
2022-09-27 16:33:37 +02:00
let enable_dashboard = &opt.env == "development";
2021-09-20 15:31:03 +02:00
let opt_clone = opt.clone();
2022-10-18 12:45:06 +02:00
let index_scheduler = Data::from(index_scheduler);
2022-09-27 16:33:37 +02:00
let http_server = HttpServer::new(move || {
create_app(
index_scheduler.clone(),
auth_controller.clone(),
opt.clone(),
analytics.clone(),
enable_dashboard,
)
})
// Disable signals allows the server to terminate immediately when a user enter CTRL-C
2022-06-07 03:38:23 +08:00
.disable_signals()
.keep_alive(KeepAlive::Os);
2020-12-12 13:32:06 +01:00
2022-09-27 16:33:37 +02:00
if let Some(config) = opt_clone.get_ssl_config()? {
2022-10-20 18:00:07 +02:00
http_server.bind_rustls(opt_clone.http_addr, config)?.run().await?;
2021-06-23 14:48:33 +02:00
} else {
2022-09-27 16:33:37 +02:00
http_server.bind(&opt_clone.http_addr)?.run().await?;
2020-12-12 13:32:06 +01:00
}
Ok(())
}
2022-10-20 18:00:07 +02:00
pub fn print_launch_resume(
opt: &Opt,
analytics: Arc<dyn Analytics>,
config_read_from: Option<PathBuf>,
) {
2021-08-30 17:41:24 +02:00
let commit_sha = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
let commit_date = option_env!("VERGEN_GIT_COMMIT_TIMESTAMP").unwrap_or("unknown");
2022-10-20 18:00:07 +02:00
let protocol =
if opt.ssl_cert_path.is_some() && opt.ssl_key_path.is_some() { "https" } else { "http" };
2020-12-12 13:32:06 +01:00
let ascii_name = r#"
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 13:32:06 +01:00
"#;
eprintln!("{}", ascii_name);
2022-09-19 18:16:28 +02:00
eprintln!(
"Config file path:\t{:?}",
2022-09-19 18:16:28 +02:00
config_read_from
.map(|config_file_path| config_file_path.display().to_string())
.unwrap_or_else(|| "none".to_string())
);
2020-12-12 13:32:06 +01:00
eprintln!("Database path:\t\t{:?}", opt.db_path);
2022-08-12 16:16:23 +08:00
eprintln!("Server listening on:\t\"{}://{}\"", protocol, opt.http_addr);
2020-12-12 13:32:06 +01:00
eprintln!("Environment:\t\t{:?}", opt.env);
eprintln!("Commit SHA:\t\t{:?}", commit_sha.to_string());
eprintln!("Commit date:\t\t{:?}", commit_date.to_string());
2022-10-20 18:00:07 +02:00
eprintln!("Package version:\t{:?}", env!("CARGO_PKG_VERSION").to_string());
2020-12-12 13:32:06 +01:00
2021-06-16 17:12:49 +02:00
#[cfg(all(not(debug_assertions), feature = "analytics"))]
{
2022-01-12 15:57:31 +01:00
if !opt.no_analytics {
eprintln!(
"
Thank you for using Meilisearch!
2021-08-12 18:58:07 +02: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
Anonymous telemetry:\t\"Enabled\""
);
} else {
eprintln!("Anonymous telemetry:\t\"Disabled\"");
2020-12-12 13:32:06 +01:00
}
}
2022-10-13 15:02:59 +02:00
if let Some(instance_uid) = analytics.instance_uid() {
2022-10-22 16:35:42 +02:00
eprintln!("Instance UID:\t\t\"{}\"", instance_uid);
}
2020-12-12 13:32:06 +01:00
eprintln!();
2022-12-22 11:57:44 +01:00
match (opt.env.as_ref(), &opt.master_key) {
("production", Some(_)) => {
2023-01-03 14:45:23 +01:00
eprintln!("A master key has been set. Requests to Meilisearch won't be authorized unless you provide an authentication key.");
2022-12-22 11:57:44 +01:00
}
("development", Some(master_key)) => {
2023-01-03 14:45:23 +01:00
eprintln!("A master key has been set. Requests to Meilisearch won't be authorized unless you provide an authentication key.");
2022-12-22 11:57:44 +01:00
if master_key.len() < MASTER_KEY_MIN_SIZE {
eprintln!();
2023-01-03 14:50:50 +01:00
log::warn!("The provided master key is too short (< {MASTER_KEY_MIN_SIZE} bytes)");
eprintln!("A master key of at least {MASTER_KEY_MIN_SIZE} bytes will be required when switching to the production environment.");
2022-12-22 11:57:44 +01:00
}
}
("development", None) => {
log::warn!("No master key found; The server will accept unidentified requests");
eprintln!("If you need some protection in development mode, please export a key:\n\nexport MEILI_MASTER_KEY={}", generate_master_key());
2023-01-03 14:50:50 +01:00
eprintln!("\nA master key of at least {MASTER_KEY_MIN_SIZE} bytes will be required when switching to the production environment.");
2022-12-22 11:57:44 +01:00
}
// unreachable because Opt::try_build above would have failed already if any other value had been produced
_ => unreachable!(),
2020-12-12 13:32:06 +01:00
}
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");
2020-12-12 13:32:06 +01:00
eprintln!();
}