Structuring

This commit is contained in:
2026-02-23 02:02:22 +01:00
parent 56fb719cca
commit 7b532981a1
6 changed files with 746 additions and 31 deletions
+36 -13
View File
@@ -1,8 +1,12 @@
mod cosmetics;
mod methods;
mod user;
use serde::{Deserialize, Serialize};
use session_rs::server::SessionServer;
use ureq::http::StatusCode;
use crate::methods::AuthResponse;
pub mod methods;
use crate::user::User;
#[tokio::main(flavor = "current_thread")]
async fn main() -> session_rs::Result<()> {
@@ -11,17 +15,36 @@ async fn main() -> session_rs::Result<()> {
server
.session_loop(async |session, _| {
println!("Connected");
session
.on::<methods::Auth, _>(|_, req| async {
Ok(AuthResponse {
cloak: String::new(),
hat: String::new(),
cloaks: Vec::new(),
hats: Vec::new(),
})
})
.await;
session.on::<methods::Auth, _>(authenticate).await;
Ok(())
})
.await
}
#[derive(Debug, Deserialize, Serialize)]
struct MinecraftAuthResponse {
pub id: String,
pub name: String,
}
async fn authenticate(_req_id: u32, session_token: String) -> Result<User, String> {
let mut response = ureq::get("https://api.minecraftservices.com/minecraft/profile")
.header("Authorization", &format!("Bearer {session_token}"))
.call()
.map_err(|_| format!("Failed to validate session"))?;
if response.status() != StatusCode::OK {
return Err(format!(
"Authentication failed with code {}",
response.status()
));
}
let auth: MinecraftAuthResponse = response
.body_mut()
.read_json()
.map_err(|_| format!("Unable to parse auth response"))?;
user::get_put(auth.id).await
}