Add server database interaction

This commit is contained in:
Michael Mikovsky
2025-12-01 16:38:33 -07:00
parent 97bd45571b
commit d1a0050f45
10 changed files with 229 additions and 38 deletions
+14 -4
View File
@@ -1,6 +1,7 @@
use egui::{Align2, Area, Color32, Frame, Order, Sense, UiKind, Vec2, mutex::Mutex};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_json::json;
use std::sync::Arc;
use std::{marker::PhantomData, sync::Arc};
use wasm_bindgen::prelude::Closure;
#[derive(serde::Deserialize, serde::Serialize)]
@@ -114,7 +115,7 @@ impl Auth {
let state = self.auth_state.clone();
crate::httpPost(
"/auth",
"/api/auth",
&json!({
"username": self.username.clone(),
"password": self.password.clone()
@@ -149,15 +150,24 @@ impl Auth {
// });
}
pub fn test(&self) {
pub fn get<T, F>(&self, path: &str, ret: F)
where
F: FnOnce(Result<T, String>) + 'static,
T: DeserializeOwned,
{
if let Some(ref token) = self.token {
let state = self.auth_state.clone();
crate::httpGetAuth(
"/api/test1234/kjhejwer/kwherjwer/iuwehrhiwer/wiuerhjwer",
path,
format!("Bearer {}", token.token),
Closure::once_into_js(move |ok: bool, response: String| {
if ok {
crate::log(&response);
if let Ok(value) = serde_json::from_str::<Result<T, String>>(&response) {
ret(value)
} else {
*(state.lock()) = AuthState::Error("Malformed Response".into());
}
} else {
*(state.lock()) = AuthState::Error(response);
}