reorganize backend

This commit is contained in:
Joseph Montanaro
2022-11-28 16:16:33 -08:00
parent c19b573b26
commit 397928b8f1
6 changed files with 134 additions and 136 deletions

View File

@ -3,28 +3,24 @@
windows_subsystem = "windows"
)]
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Mutex;
// use tokio::runtime::Runtime;
use serde::{Serialize, Deserialize};
use tauri::{Manager, State};
use tokio::sync::oneshot;
mod errors;
mod ipc;
mod state;
mod server;
mod storage;
mod http;
fn main() {
let initial_state = state::AppState::new(state::SessionStatus::Locked, None);
tauri::Builder::default()
.manage(CurrentSession)
.manage(RequestCount)
.manage(OpenRequests)
.invoke_handler(tauri::generate_handler![unlock])
.manage(initial_state)
.invoke_handler(tauri::generate_handler![ipc::unlock, ipc::respond])
.setup(|app| {
let addr = std::net::SocketAddrV4::from_str("127.0.0.1:12345").unwrap();
tauri::async_runtime::spawn(http::serve(addr, app.handle()));
tauri::async_runtime::spawn(server::serve(addr, app.handle()));
Ok(())
})
.run(tauri::generate_context!())
@ -37,36 +33,4 @@ fn main() {
// let creds = std::fs::read_to_string("credentials.json").unwrap();
// storage::save(&creds, "correct horse battery staple");
}
#[derive(Serialize, Deserialize)]
pub enum Session {
Unlocked(String),
Locked,
Empty,
}
type CurrentSession = Mutex<Session>;
type RequestCount = Mutex<u64>;
type OpenRequests = Mutex<HashMap<u64, oneshot::Sender>>;
#[derive(Clone, Serialize, Deserialize)]
pub struct CredentialsRequest {
pub request_id: u64,
}
// struct Session {
// key_id: String,
// secret_key: String,
// token: String,
// expires: u64,
// }
#[tauri::command]
fn unlock(passphrase: String, current_session: State<'_, CurrentSession>) -> bool {
let credentials = storage::load(&passphrase);
*current_session.lock().unwrap() = CredentialStatus::Unlocked(credentials);
true
}
}