1
0
Fork 0

Move config into its own file

This commit is contained in:
Honbra 2023-07-14 00:57:35 +02:00
parent e50187da2e
commit 79fe0d23c8
Signed by: honbra
GPG key ID: B61CC9ADABE2D952
2 changed files with 22 additions and 16 deletions

19
src/config.rs Normal file
View file

@ -0,0 +1,19 @@
use figment::{
providers::{Env, Format, Toml},
Figment,
};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Config {
pub meaning_of_life: String,
}
impl Config {
pub fn load() -> Result<Self, figment::Error> {
Figment::new()
.merge(Toml::file("config.toml"))
.merge(Env::raw())
.extract()
}
}

View file

@ -1,29 +1,16 @@
mod config;
mod tracing; mod tracing;
use ::tracing::info; use ::tracing::info;
use eyre::Context; use eyre::Context;
use figment::{
providers::{Env, Format, Toml},
Figment,
};
use serde::Deserialize;
use crate::tracing::set_up_tracing; use crate::{config::Config, tracing::set_up_tracing};
#[derive(Deserialize)]
struct Config {
meaning_of_life: String,
}
#[tokio::main] #[tokio::main]
async fn main() -> eyre::Result<()> { async fn main() -> eyre::Result<()> {
set_up_tracing("rust_tokio_service_template").context("Failed to set up tracing")?; set_up_tracing("rust_tokio_service_template").context("Failed to set up tracing")?;
let Config { meaning_of_life } = Figment::new() let Config { meaning_of_life } = Config::load().context("Failed to load config")?;
.merge(Toml::file("config.toml"))
.merge(Env::raw())
.extract()
.context("Failed to extract config")?;
info!("The meaning of life is {meaning_of_life}"); info!("The meaning of life is {meaning_of_life}");