Skip to content

Commit

Permalink
Allow loading config from file
Browse files Browse the repository at this point in the history
  • Loading branch information
cofob committed Aug 1, 2024
1 parent ba016d7 commit 6588914
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/.direnv/
/result*
/*.local
/*.local*
/debug/
/target/
30 changes: 20 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Application configuration.

use std::time::Duration;
use std::{path::PathBuf, time::Duration};

use anyhow::{Context, Result};
use config::Config;
Expand Down Expand Up @@ -48,15 +48,25 @@ pub struct AppConfig {
}

/// Load application configuration.
pub fn load_config() -> Result<AppConfig> {
let config = Config::builder()
.add_source(
config::Environment::with_prefix("FS")
.separator("__")
.list_separator(","),
)
.build()
.context("failed to load config")?;
pub fn load_config(config_path: &Option<PathBuf>) -> Result<AppConfig> {
let mut config_builder = Config::builder().add_source(
config::Environment::with_prefix("FS")
.separator("__")
.list_separator(","),
);

match config_path {
Some(path) => {
config_builder =
config_builder.add_source(config::File::from(path.clone()).required(true));
}
None => {
config_builder =
config_builder.add_source(config::File::with_name("config").required(false));
}
}

let config = config_builder.build().context("failed to load config")?;

debug!("Raw configuration: {:#?}", config);

Expand Down
6 changes: 4 additions & 2 deletions src/log_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ use std::env;
/// # Panics
///
/// Function should not panic. On error, logging is just disabled.
pub fn configure_logging() -> Result<()> {
pub fn configure_logging(log_level: &Option<String>) -> Result<()> {
let mut builder = formatted_builder();

if let Ok(s) = env::var("FS__LOG") {
if let Some(s) = log_level {
builder.parse_filters(s);
} else if let Ok(s) = env::var("FS__LOG") {
builder.parse_filters(&s);
} else {
builder.filter_level(LevelFilter::Info);
Expand Down
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
/// Path to the configuration file.
#[arg(short, long, default_value = "None")]
config: Option<PathBuf>,
/// Log level. Takes precedence over the FS__LOG env variable. Default is INFO.
#[arg(long, default_value = "None")]
log_level: Option<String>,
}
#[derive(Subcommand)]
enum Commands {
Expand Down Expand Up @@ -73,17 +79,17 @@ async fn crawler_loop(crawler: Arc<Crawler>) {

#[tokio::main]
async fn main() -> Result<()> {
configure_logging().ok();

let cli = Cli::parse();

configure_logging(&cli.log_level).ok();

match &cli.command {
Some(Commands::Serve {
services,
listen,
workers,
}) => {
let config = load_config().context("failed to load config")?;
let config = load_config(&cli.config).context("failed to load config")?;

let listen: SocketAddr = listen
.unwrap_or_else(|| SocketAddr::V4(SocketAddrV4::new([127, 0, 0, 1].into(), 8080)));
Expand Down

0 comments on commit 6588914

Please sign in to comment.