reorganized files

This commit is contained in:
2025-12-05 12:50:25 +08:00
parent d9dbd5fe5d
commit a9068fe575
13 changed files with 18 additions and 22 deletions

88
src-tauri/src/state.rs Normal file
View File

@@ -0,0 +1,88 @@
// in app-core/src/state.rs
use crate::{
lock_w,
models::app_config::{AppConfig, AuthConfig},
services::auth::{load_auth_pass, AuthPass},
};
use std::{
env,
sync::{Arc, LazyLock, RwLock},
};
use tauri::async_runtime;
use tracing::{info, warn};
#[derive(Default, Clone)]
pub struct OAuthFlowTracker {
pub state: Option<String>,
pub code_verifier: Option<String>,
pub initiated_at: Option<u64>,
}
pub struct Clients {
pub http_client: reqwest::Client,
pub ws_client: Option<rust_socketio::client::Client>,
}
#[derive(Default)]
pub struct AppState {
pub app_config: AppConfig,
pub clients: Option<Clients>,
pub auth_pass: Option<AuthPass>,
pub oauth_flow: OAuthFlowTracker,
}
// Global application state
// Read / write this state via the `lock_r!` / `lock_w!` macros from `fdoll-core::utilities`
pub static FDOLL: LazyLock<Arc<RwLock<AppState>>> =
LazyLock::new(|| Arc::new(RwLock::new(AppState::default())));
pub fn init_fdoll_state() {
{
let mut guard = lock_w!(FDOLL);
dotenvy::dotenv().ok();
guard.app_config = AppConfig {
api_base_url: Some(env::var("API_BASE_URL").expect("API_BASE_URL must be set")),
auth: AuthConfig {
audience: env::var("JWT_AUDIENCE").expect("JWT_AUDIENCE must be set"),
auth_url: env::var("AUTH_URL").expect("AUTH_URL must be set"),
redirect_uri: env::var("REDIRECT_URI").expect("REDIRECT_URI must be set"),
redirect_host: env::var("REDIRECT_HOST").expect("REDIRECT_HOST must be set"),
},
};
guard.auth_pass = match load_auth_pass() {
Ok(pass) => pass,
Err(e) => {
warn!("Failed to load auth pass from keyring: {e}");
None
}
};
info!("Loaded auth pass");
// Initialize HTTP client immediately (non-blocking)
let http_client = reqwest::ClientBuilder::new()
.timeout(std::time::Duration::from_secs(30))
.connect_timeout(std::time::Duration::from_secs(10))
.user_agent("friendolls-desktop/0.1.0")
.build()
.expect("Client should build");
// Store HTTP client immediately - WebSocket client will be added later
guard.clients = Some(Clients {
http_client,
ws_client: None,
});
info!("Initialized HTTP client");
let has_auth = guard.auth_pass.is_some();
drop(guard);
if has_auth {
async_runtime::spawn(async move {
crate::services::ws::init_ws_client().await;
});
}
info!("Initialized FDOLL state (WebSocket client initializing asynchronously)");
}
}