✨ Añade lectura de configuración global y modular
- Soporta jerarquía de ficheros TOML que mapean ajustes a estructuras fuertemente tipadas con valores predefinidos. - Permite definir configuraciones distintas para cada entorno. - Añade la macro `include_config!` para facilitar la asignación modular de ajustes de configuración. - Añade documentación detallada y tests de verificación.
This commit is contained in:
parent
cbee4c2cb8
commit
f7dbd90af2
14 changed files with 4938 additions and 4 deletions
41
tests/config.rs
Normal file
41
tests/config.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use std::env;
|
||||
|
||||
include_config!(SETTINGS: Settings => [
|
||||
"test.string_value" => "Test String",
|
||||
"test.int_value" => -321,
|
||||
"test.float_value" => 2.3456,
|
||||
]);
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Settings {
|
||||
pub test: Test,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Test {
|
||||
pub string_value: String,
|
||||
pub int_value: i32,
|
||||
pub float_value: f32,
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn check_global_config() {
|
||||
env::set_var("PAGETOP_RUN_MODE", "test");
|
||||
|
||||
assert_eq!(global::SETTINGS.app.run_mode, "test");
|
||||
assert_eq!(global::SETTINGS.app.name, "Testing");
|
||||
assert_eq!(global::SETTINGS.server.bind_port, 9000);
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn check_local_config() {
|
||||
env::set_var("PAGETOP_RUN_MODE", "test");
|
||||
|
||||
assert_eq!(SETTINGS.test.string_value, "Modified value");
|
||||
assert_eq!(SETTINGS.test.int_value, -321);
|
||||
assert_eq!(SETTINGS.test.float_value, 8.7654);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue