Skip to content

Commit 6fda8e5

Browse files
committed
feat: added error hook
1 parent 9d7c0df commit 6fda8e5

1 file changed

Lines changed: 46 additions & 15 deletions

File tree

src/main.rs

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,57 @@ pub mod sagittarius;
1010
pub mod server;
1111
pub mod startup;
1212

13+
const CONFIG_PATH_ENV: &str = "AQUILA_CONFIG_PATH";
14+
const SERVICE_CONFIG_PATH_ENV: &str = "AQUILA_SERVICE_CONFIG_PATH";
15+
1316
#[tokio::main]
1417
async fn main() {
15-
env_logger::Builder::from_default_env()
16-
.filter_level(log::LevelFilter::Debug)
17-
.init();
18+
// Load .env before config-rs applies environment overrides.
19+
load_env_file();
20+
let config_result = match std::env::var(CONFIG_PATH_ENV) {
21+
Ok(path) => AquilaConfig::try_from_path(path),
22+
Err(_) => AquilaConfig::try_new(),
23+
};
24+
25+
let log_level = config_result
26+
.as_ref()
27+
.map(|config| config.log_level.as_str())
28+
.unwrap_or("debug");
29+
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(log_level)).init();
30+
install_panic_logging();
31+
32+
let config = config_result
33+
.unwrap_or_else(|error| panic!("failed to load Aquila configuration: {error}"));
1834
log::info!("Starting Aquila");
1935

20-
// Load environment variables from .env file
21-
load_env_file();
22-
let config = AquilaConfig::new();
2336
let app_readiness = AppReadiness::new();
24-
let service_config = ServiceConfiguration::from_path(&config.service_config_path);
25-
log::debug!(
26-
"Configuration loaded mode={:?} environment={:?} grpc={}:{} health_service={}",
27-
config.mode,
28-
config.environment,
29-
config.grpc_host,
30-
config.grpc_port,
31-
config.with_health_service
32-
);
37+
let service_config = std::env::var_os(SERVICE_CONFIG_PATH_ENV)
38+
.map(ServiceConfiguration::from_path)
39+
.unwrap_or_default();
40+
log::debug!("{config}");
3341

3442
startup::run(config, app_readiness, service_config).await;
3543
}
44+
45+
fn install_panic_logging() {
46+
std::panic::set_hook(Box::new(move |panic_info| {
47+
let message = if let Some(message) = panic_info.payload().downcast_ref::<&str>() {
48+
*message
49+
} else if let Some(message) = panic_info.payload().downcast_ref::<String>() {
50+
message.as_str()
51+
} else {
52+
"<non-string panic payload>"
53+
};
54+
55+
match panic_info.location() {
56+
Some(location) => log::error!(
57+
"Process panic message={} file={} line={} column={}",
58+
message,
59+
location.file(),
60+
location.line(),
61+
location.column()
62+
),
63+
None => log::error!("Process panic message={} location=unknown", message),
64+
}
65+
}));
66+
}

0 commit comments

Comments
 (0)