Actual error logging, don't fail if vrchat api token is empty
All checks were successful
Build Docker Image / nix-flake-check (push) Successful in 2m0s
Build Docker Image / docker-build (push) Successful in 2m31s
Build Docker Image / arm-docker-build (push) Successful in 6m26s

This commit is contained in:
Gabriel Simmer 2023-11-17 16:55:19 +00:00
parent 8dd6399416
commit a82b21b2ce
Signed by: arch
SSH key fingerprint: SHA256:m3OEcdtrnBpMX+2BDGh/byv3hrCekCLzDYMdvGEKPPQ

View file

@ -17,7 +17,7 @@ use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::sync::OnceLock; use std::sync::OnceLock;
use std::{env, fmt, fs}; use std::{env, fmt, fs, io};
use url::Url; use url::Url;
static PLAYER_COUNT: OnceLock<GaugeVec> = OnceLock::new(); static PLAYER_COUNT: OnceLock<GaugeVec> = OnceLock::new();
@ -30,6 +30,9 @@ static WORLD_FAVORITES: OnceLock<IntCounterVec> = OnceLock::new();
enum WsError { enum WsError {
Reqwest(reqwest::Error), Reqwest(reqwest::Error),
Url(url::ParseError), Url(url::ParseError),
Io(io::Error),
Toml(toml::de::Error),
Var(env::VarError),
} }
impl From<url::ParseError> for WsError { impl From<url::ParseError> for WsError {
@ -44,13 +47,34 @@ impl From<reqwest::Error> for WsError {
} }
} }
impl From<io::Error> for WsError {
fn from(value: io::Error) -> Self {
Self::Io(value)
}
}
impl From<toml::de::Error> for WsError {
fn from(value: toml::de::Error) -> Self {
Self::Toml(value)
}
}
impl From<env::VarError> for WsError {
fn from(value: env::VarError) -> Self {
Self::Var(value)
}
}
impl Error for WsError {} impl Error for WsError {}
impl fmt::Display for WsError { impl fmt::Display for WsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
WsError::Reqwest(e) => write!(f, "Reqwest error: {}", e), WsError::Reqwest(e) => write!(f, "reqwest error: {}", e),
WsError::Url(_) => todo!(), 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<Config, WsError> {
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] #[tokio::main]
async fn main() -> Result<(), ()> { async fn main() -> Result<(), ()> {
let env = Env::default().filter_or("LOG_LEVEL", "info"); let env = Env::default().filter_or("LOG_LEVEL", "info");
env_logger::init_from_env(env); 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(); 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() let app = Router::new()
.route("/", get(homepage)) .route("/", get(homepage))
@ -214,7 +250,7 @@ async fn metrics(config: Config) -> Result<Vec<u8>, WsError> {
group_metrics(&client, &auth_cookie, name, group).await?; 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() { for (name, id) in config.worlds.unwrap() {
debug!("scanning world {}", &name); debug!("scanning world {}", &name);
world_metrics(&client, &auth_cookie, name, id).await?; world_metrics(&client, &auth_cookie, name, id).await?;
@ -233,7 +269,7 @@ async fn group_metrics(
name: String, name: String,
group: VrcGroup, group: VrcGroup,
) -> Result<(), WsError> { ) -> Result<(), WsError> {
if !group.id.is_empty() { if !group.id.is_empty() && auth_cookie != "auth=" {
let instance_list_url = format!( let instance_list_url = format!(
"https://api.vrchat.cloud/api/1/groups/{}/instances", "https://api.vrchat.cloud/api/1/groups/{}/instances",
group.id group.id