tmtd/src/config.rs
2022-05-14 00:07:11 +02:00

42 lines
1.3 KiB
Rust

/*
* tmtd - Suckless To Do list
* Copyright (C) 2022 C4TG1RL5
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
use serde::Deserialize;
use std::env;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::fs;
#[derive(Deserialize)]
pub struct Config {
pub listen_addr: SocketAddr,
pub admin_pass: String,
pub connection_string: String,
pub log_level: Option<String>,
}
impl Config {
pub async fn load() -> anyhow::Result<Arc<Self>> {
let config_var = env::var("TMTD_CONFIG");
let config_path = config_var.as_deref().unwrap_or("tmtd.toml");
println!("Loading config from '{}'...", config_path);
let config_str = fs::read_to_string(config_path).await?;
Ok(Arc::new(toml::from_str(&config_str)?))
}
}