Refactor
This commit is contained in:
parent
934c704cbf
commit
bd40e5c8d8
4 changed files with 47 additions and 48 deletions
35
src-tauri/src/app_state.rs
Normal file
35
src-tauri/src/app_state.rs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
use rand::prelude::*;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use rusqlite::Connection;
|
||||||
|
use ring_compat::signature::ed25519::SigningKey;
|
||||||
|
|
||||||
|
pub struct AppState {
|
||||||
|
pub db: Connection,
|
||||||
|
pub last_sync: i64,
|
||||||
|
pub id: u64,
|
||||||
|
pub key: SigningKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AppState {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let db = Connection::open_in_memory()
|
||||||
|
.expect("Failed to create DB.");
|
||||||
|
db.execute(
|
||||||
|
"CREATE TABLE swap (
|
||||||
|
rand INTEGER,
|
||||||
|
store INTEGER,
|
||||||
|
account INTEGER,
|
||||||
|
submitter INTEGER,
|
||||||
|
time INTEGER,
|
||||||
|
cancellation BOOL
|
||||||
|
)",
|
||||||
|
(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let last_sync = i64::MIN;
|
||||||
|
let id = rng.gen();
|
||||||
|
let key = SigningKey::generate(&mut rng);
|
||||||
|
AppState { db, last_sync, id, key }
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,21 +5,7 @@ use ring_compat::signature::ed25519::SigningKey;
|
||||||
use tauri::{Manager, State};
|
use tauri::{Manager, State};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
pub struct DoorState {
|
use crate::app_state::AppState;
|
||||||
last_sync: i64,
|
|
||||||
id: u64,
|
|
||||||
key: SigningKey,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DoorState {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
let last_sync = i64::MIN;
|
|
||||||
let id = rng.gen();
|
|
||||||
let key = SigningKey::generate(&mut rng);
|
|
||||||
DoorState { last_sync, id, key }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Collector(Vec<u8>, Vec<u8>, usize);
|
struct Collector(Vec<u8>, Vec<u8>, usize);
|
||||||
|
@ -71,7 +57,6 @@ impl easy::Handler for Collector {
|
||||||
pub fn data_client(file: &str) -> Result<Easy2<Collector>, ()> {
|
pub fn data_client(file: &str) -> Result<Easy2<Collector>, ()> {
|
||||||
let mut client = Easy2::new(Collector(Vec::new(), Vec::new(), 0));
|
let mut client = Easy2::new(Collector(Vec::new(), Vec::new(), 0));
|
||||||
let url = format!("https://cloud.seebruecke.org/public.php/webdav/data/{}", file);
|
let url = format!("https://cloud.seebruecke.org/public.php/webdav/data/{}", file);
|
||||||
println!("{}", url);
|
|
||||||
client.url(&url)
|
client.url(&url)
|
||||||
.map_err(|_| ())?;
|
.map_err(|_| ())?;
|
||||||
client.username(include_str!("cloud_user.txt")).map_err(|_| ())?;
|
client.username(include_str!("cloud_user.txt")).map_err(|_| ())?;
|
||||||
|
@ -82,7 +67,7 @@ pub fn data_client(file: &str) -> Result<Easy2<Collector>, ()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn pull_data(state: State<'_, Mutex<DoorState>>) -> Result<String, ()> {
|
pub async fn pull_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
|
||||||
let mut client = data_client("")?;
|
let mut client = data_client("")?;
|
||||||
client.custom_request("PROPFIND").map_err(|_| ())?;
|
client.custom_request("PROPFIND").map_err(|_| ())?;
|
||||||
client.perform().map_err(|_| ())?;
|
client.perform().map_err(|_| ())?;
|
||||||
|
@ -91,7 +76,7 @@ pub async fn pull_data(state: State<'_, Mutex<DoorState>>) -> Result<String, ()>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn push_data(state: State<'_, Mutex<DoorState>>) -> Result<String, ()> {
|
pub async fn push_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
|
||||||
let state = state.lock().await;
|
let state = state.lock().await;
|
||||||
let key_file = format!("{:016X}.key", state.id);
|
let key_file = format!("{:016X}.key", state.id);
|
||||||
let mut client = data_client(&key_file)?;
|
let mut client = data_client(&key_file)?;
|
||||||
|
@ -100,10 +85,7 @@ pub async fn push_data(state: State<'_, Mutex<DoorState>>) -> Result<String, ()>
|
||||||
client.get_mut().1.extend_from_slice(v_key.as_ref());
|
client.get_mut().1.extend_from_slice(v_key.as_ref());
|
||||||
client.in_filesize(v_key.as_ref().len() as u64);
|
client.in_filesize(v_key.as_ref().len() as u64);
|
||||||
client.upload(true);
|
client.upload(true);
|
||||||
println!("{:?}", client);
|
let perf = client.perform().map_err(|_| ())?;
|
||||||
let perf = client.perform();
|
|
||||||
println!("{:?}", perf);
|
|
||||||
println!("{:?}", client);
|
|
||||||
let content = &client.get_ref().0;
|
let content = &client.get_ref().0;
|
||||||
Ok(String::from_utf8_lossy(content).to_string())
|
Ok(String::from_utf8_lossy(content).to_string())
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,9 @@ use tauri::{Manager, State};
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
mod data_door;
|
mod data_door;
|
||||||
|
mod app_state;
|
||||||
|
|
||||||
use data_door::DoorState;
|
use app_state::AppState;
|
||||||
|
|
||||||
struct AppState {
|
|
||||||
db: Connection,
|
|
||||||
acc: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum Store {
|
enum Store {
|
||||||
|
@ -55,17 +51,19 @@ impl ToSql for Store {
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn swap(
|
async fn swap(
|
||||||
store: &str,
|
store: &str,
|
||||||
|
acc: i64,
|
||||||
state: State<'_, Mutex<AppState>>,
|
state: State<'_, Mutex<AppState>>,
|
||||||
) -> Result<(), ()> {
|
) -> Result<(), ()> {
|
||||||
let state = state.lock().await;
|
let state = state.lock().await;
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let store: Store = store.try_into()?;
|
let store: Store = store.try_into()?;
|
||||||
state.db.execute(
|
state.db.execute(
|
||||||
"INSERT INTO swap VALUES (?1, ?2, ?3, ?4, ?5)",
|
"INSERT INTO swap VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||||
(
|
(
|
||||||
rng.gen::<i64>(),
|
rng.gen::<i64>(),
|
||||||
store,
|
store,
|
||||||
state.acc,
|
acc,
|
||||||
|
state.id,
|
||||||
Utc::now().timestamp(),
|
Utc::now().timestamp(),
|
||||||
false,
|
false,
|
||||||
),
|
),
|
||||||
|
@ -86,26 +84,10 @@ async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
|
||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
let mut rng = rand::thread_rng();
|
let state = AppState::new();
|
||||||
let db = Connection::open_in_memory().expect("Failed to create DB.");
|
|
||||||
let acc = rng.gen::<i64>();
|
|
||||||
db.execute(
|
|
||||||
"CREATE TABLE swap (
|
|
||||||
rand INTEGER,
|
|
||||||
store INTEGER,
|
|
||||||
account INTEGER,
|
|
||||||
time INTEGER,
|
|
||||||
cancellation BOOL
|
|
||||||
)",
|
|
||||||
(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let state = AppState { db, acc };
|
|
||||||
let door = DoorState::new();
|
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.plugin(tauri_plugin_fs::init())
|
.plugin(tauri_plugin_fs::init())
|
||||||
.setup(|app| {
|
.setup(|app| {
|
||||||
app.manage(Mutex::new(door));
|
|
||||||
app.manage(Mutex::new(state));
|
app.manage(Mutex::new(state));
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,7 +4,7 @@ let cntMsgEl;
|
||||||
let pullMsgEl;
|
let pullMsgEl;
|
||||||
|
|
||||||
async function swap(s) {
|
async function swap(s) {
|
||||||
await invoke("swap", { store: s });
|
await invoke("swap", { store: s, acc: 1 });
|
||||||
cntMsgEl.textContent = await invoke("count", {});
|
cntMsgEl.textContent = await invoke("count", {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue