Support MEILI_CONFIG_FILE_PATH to define config file path
Close
This is an alternative to the `--config-file-path` option. If both `--config-file-path` and `MEILI_CONFIG_FILE_PATH` are present, `--config-file-path` takes precedence according to the "Priority order" section of
+use std::env;
use std::fs;
use std::io::{BufReader, Read};
use std::path::PathBuf;
@@ -51,6 +52,7 @@ const MEILI_LOG_LEVEL: &str = "MEILI_LOG_LEVEL";
#[cfg(feature = "metrics")]
const MEILI_ENABLE_METRICS_ROUTE: &str = "MEILI_ENABLE_METRICS_ROUTE";
+const DEFAULT_CONFIG_FILE_PATH: &str = "./config.yml";
const DEFAULT_DB_PATH: &str = "./data.ms";
const DEFAULT_HTTP_ADDR: &str = "127.0.0.1:7700";
const DEFAULT_ENV: &str = "development";
@@ -261,11 +263,12 @@ impl Opt {
// Parse the args to get the config_file_path.
let mut opts = Opt::parse();
let mut config_read_from = None;
- if let Some(config_file_path) = opts
+ let config_file_path = opts
.config_file_path
.clone()
- .or_else(|| Some(PathBuf::from("./config.toml")))
- {
+ .or_else(|| env::var("MEILI_CONFIG_FILE_PATH").map(PathBuf::from).ok())
+ .unwrap_or_else(|| PathBuf::from(DEFAULT_CONFIG_FILE_PATH));
+
match std::fs::read(&config_file_path) {
Ok(config) => {
// If the file is successfully read, we deserialize it with `toml`.
@@ -288,7 +291,6 @@ impl Opt {
),
_ => (),
}
- }
Ok((opts, config_read_from))
}
