From a82b21b2ce5dcd0ea9c8bd1d80876c423ad220af Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Fri, 17 Nov 2023 16:55:19 +0000 Subject: [PATCH] Actual error logging, don't fail if vrchat api token is empty --- src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index ed58270..bcfc123 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use serde::Deserialize; use std::collections::HashMap; use std::error::Error; use std::sync::OnceLock; -use std::{env, fmt, fs}; +use std::{env, fmt, fs, io}; use url::Url; static PLAYER_COUNT: OnceLock = OnceLock::new(); @@ -30,6 +30,9 @@ static WORLD_FAVORITES: OnceLock = OnceLock::new(); enum WsError { Reqwest(reqwest::Error), Url(url::ParseError), + Io(io::Error), + Toml(toml::de::Error), + Var(env::VarError), } impl From for WsError { @@ -44,13 +47,34 @@ impl From for WsError { } } +impl From for WsError { + fn from(value: io::Error) -> Self { + Self::Io(value) + } +} + +impl From for WsError { + fn from(value: toml::de::Error) -> Self { + Self::Toml(value) + } +} + +impl From for WsError { + fn from(value: env::VarError) -> Self { + Self::Var(value) + } +} + impl Error for WsError {} impl fmt::Display for WsError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - WsError::Reqwest(e) => write!(f, "Reqwest error: {}", e), - WsError::Url(_) => todo!(), + WsError::Reqwest(e) => write!(f, "reqwest error: {}", e), + WsError::Url(e) => write!(f, "url error: {}", e), + WsError::Io(e) => write!(f, "io error: {}", e), + WsError::Toml(e) => write!(f, "toml error: {}", e), + WsError::Var(e) => write!(f, "var error: {}", e), } } } @@ -160,15 +184,27 @@ fn register_metrics() { ); } +async fn load_config() -> Result { + let content = fs::read_to_string("config.toml")?; + let mut config: Config = toml::from_str(&content)?; + config.vrchat_token = Some(env::var("VRCHAT_AUTH_TOKEN")?); + + Ok(config) +} + #[tokio::main] async fn main() -> Result<(), ()> { let env = Env::default().filter_or("LOG_LEVEL", "info"); - env_logger::init_from_env(env); - let content = fs::read_to_string("config.toml").unwrap(); - let mut config: Config = toml::from_str(&content).unwrap(); - config.vrchat_token = Some(env::var("VRCHAT_AUTH_TOKEN").unwrap()); + register_metrics(); + let config = match load_config().await { + Ok(c) => c, + Err(e) => { + error!("could not load configuration: {}", e); + std::process::exit(1) + } + }; let app = Router::new() .route("/", get(homepage)) @@ -214,7 +250,7 @@ async fn metrics(config: Config) -> Result, WsError> { group_metrics(&client, &auth_cookie, name, group).await?; } } - if config.worlds.is_some() { + if config.worlds.is_some() && auth_cookie != "auth=" { for (name, id) in config.worlds.unwrap() { debug!("scanning world {}", &name); world_metrics(&client, &auth_cookie, name, id).await?; @@ -233,7 +269,7 @@ async fn group_metrics( name: String, group: VrcGroup, ) -> Result<(), WsError> { - if !group.id.is_empty() { + if !group.id.is_empty() && auth_cookie != "auth=" { let instance_list_url = format!( "https://api.vrchat.cloud/api/1/groups/{}/instances", group.id