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;
|
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}");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue