Simple bug fix
This commit is contained in:
+6
-1
@@ -2,8 +2,11 @@ use std::sync::Arc;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{ErrorContext, logger};
|
||||
use crate::{Server, utils::client::Client};
|
||||
|
||||
logger!(LOGGER "Auth");
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct AuthApiRes {
|
||||
user_id: u32,
|
||||
@@ -13,8 +16,10 @@ pub fn auth(_server: &Arc<Server>, client: &mut Client, token: &str) -> crate::R
|
||||
let mut res = ureq::get(format!(
|
||||
"http://localhost:3000/api/auth?intents=server&token={token}"
|
||||
))
|
||||
.call()?;
|
||||
.call()
|
||||
.context("Failed to authenticate")?;
|
||||
let api_res: AuthApiRes = serde_json::from_str(&res.body_mut().read_to_string()?)?;
|
||||
client.set_uuid(api_res.user_id);
|
||||
LOGGER.info(format!("{} successfully authenticated", api_res.user_id));
|
||||
Ok(api_res.user_id)
|
||||
}
|
||||
|
||||
+13
-4
@@ -11,6 +11,7 @@ pub mod requests;
|
||||
pub mod types;
|
||||
pub mod utils;
|
||||
|
||||
pub use anyhow::Context as ErrorContext;
|
||||
pub use anyhow::Result;
|
||||
|
||||
use crate::{utils::client::Client, utils::plugin::DynPlugin};
|
||||
@@ -89,10 +90,18 @@ impl Server {
|
||||
Ok(stream) => {
|
||||
std::thread::spawn({
|
||||
let srv = self.clone();
|
||||
let client = srv.init_client(stream)?;
|
||||
move || match srv.wrap_err(&client, srv.handle_client(&client)) {
|
||||
Ok(_) => {}
|
||||
Err(e) => Self::LOGGER.error(format!("Client handler failed: {e}")),
|
||||
|
||||
move || {
|
||||
let Some(client) = Self::LOGGER
|
||||
.extract(srv.init_client(stream), "Failed to initialize client")
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
Self::LOGGER.extract(
|
||||
srv.wrap_err(&client, srv.handle_client(&client)),
|
||||
"Client handler failed",
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -22,4 +22,24 @@ impl Logger {
|
||||
pub fn error<T: Display>(&self, message: T) {
|
||||
println!("\x1b[31mERROR\x1b[0m ({}) › {}", self.name, message);
|
||||
}
|
||||
|
||||
pub fn extract<T, E: Display, D: Display>(&self, v: Result<T, E>, m: D) -> Option<T> {
|
||||
match v {
|
||||
Ok(a) => Some(a),
|
||||
Err(e) => {
|
||||
self.error(format!("{m}: {e}"));
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_panic<T, E: Display, D: Display>(&self, v: Result<T, E>, m: D) -> T {
|
||||
match v {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
self.error(format!("{m}: {e}"));
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user