Move config into its own file
This commit is contained in:
parent
e50187da2e
commit
79fe0d23c8
19
src/config.rs
Normal file
19
src/config.rs
Normal 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()
|
||||
}
|
||||
}
|
19
src/main.rs
19
src/main.rs
|
@ -1,29 +1,16 @@
|
|||
mod config;
|
||||
mod tracing;
|
||||
|
||||
use ::tracing::info;
|
||||
use eyre::Context;
|
||||
use figment::{
|
||||
providers::{Env, Format, Toml},
|
||||
Figment,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::tracing::set_up_tracing;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
meaning_of_life: String,
|
||||
}
|
||||
use crate::{config::Config, tracing::set_up_tracing};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> eyre::Result<()> {
|
||||
set_up_tracing("rust_tokio_service_template").context("Failed to set up tracing")?;
|
||||
|
||||
let Config { meaning_of_life } = Figment::new()
|
||||
.merge(Toml::file("config.toml"))
|
||||
.merge(Env::raw())
|
||||
.extract()
|
||||
.context("Failed to extract config")?;
|
||||
let Config { meaning_of_life } = Config::load().context("Failed to load config")?;
|
||||
|
||||
info!("The meaning of life is {meaning_of_life}");
|
||||
|
||||
|
|
Loading…
Reference in a new issue