Skip to main content

rustlavel_core/
lib.rs

1//! rustlavel-core: the foundation every rustlavel package builds on.
2//!
3//! Configuration and `.env` loading, the JSON value model, the typed
4//! application context that replaces Laravel's service container, structured
5//! logging, and the instrumentation bus that Telescope and tracing listen on.
6
7pub mod base64;
8pub mod config;
9pub mod context;
10pub mod dispatcher;
11pub mod env;
12pub mod error;
13pub mod events;
14pub mod json;
15pub mod log;
16
17pub use config::Config;
18pub use context::{Context, ContextBuilder};
19pub use dispatcher::{AppEvent, Dispatcher};
20pub use error::{Error, Result};
21pub use events::Event;
22pub use json::Json;
23
24/// Load `.env` and build the default configuration tree.
25///
26/// Called once during boot, before anything reads configuration.
27pub fn boot(root: impl AsRef<std::path::Path>) -> Result<Config> {
28    let root = root.as_ref();
29    env::load(root.join(".env"))?;
30
31    let config = Config::with_defaults();
32    config.load_dir(root.join("config"))?;
33
34    if let Some(level) = std::env::var("LOG_LEVEL").ok().and_then(|v| log::Level::parse(&v)) {
35        log::set_level(level);
36    } else if config.is_production() {
37        log::set_level(log::Level::Info);
38    } else {
39        log::set_level(log::Level::Debug);
40    }
41
42    // Production wants machine-readable logs; local development wants to read them.
43    log::set_json(config.is_production());
44
45    Ok(config)
46}