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 config;
8pub mod context;
9pub mod dispatcher;
10pub mod env;
11pub mod error;
12pub mod events;
13pub mod json;
14pub mod log;
15
16pub use config::Config;
17pub use context::{Context, ContextBuilder};
18pub use dispatcher::{AppEvent, Dispatcher};
19pub use error::{Error, Result};
20pub use events::Event;
21pub use json::Json;
22
23/// Load `.env` and build the default configuration tree.
24///
25/// Called once during boot, before anything reads configuration.
26pub fn boot(root: impl AsRef<std::path::Path>) -> Result<Config> {
27    let root = root.as_ref();
28    env::load(root.join(".env"))?;
29
30    let config = Config::with_defaults();
31    config.load_dir(root.join("config"))?;
32
33    if let Some(level) = std::env::var("LOG_LEVEL").ok().and_then(|v| log::Level::parse(&v)) {
34        log::set_level(level);
35    } else if config.is_production() {
36        log::set_level(log::Level::Info);
37    } else {
38        log::set_level(log::Level::Debug);
39    }
40
41    // Production wants machine-readable logs; local development wants to read them.
42    log::set_json(config.is_production());
43
44    Ok(config)
45}