mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-13 16:22:43 +10:00
Converting DB access to a trait
This commit is contained in:
@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use tauri::{AppHandle, Emitter, Manager};
|
use tauri::{AppHandle, Emitter, Manager};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::{db::{fetch_base_url, DatabaseAuth}, AppState, AppStatus, User, DB};
|
use crate::{db::{DatabaseAuth, DatabaseImpls}, AppState, AppStatus, User, DB};
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
#[serde(rename_all="camelCase")]
|
#[serde(rename_all="camelCase")]
|
||||||
@ -81,7 +81,7 @@ pub fn generate_authorization_header() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_user() -> Result<User, ()> {
|
pub fn fetch_user() -> Result<User, ()> {
|
||||||
let base_url = fetch_base_url();
|
let base_url = DB.fetch_base_url();
|
||||||
|
|
||||||
let endpoint = base_url.join("/api/v1/client/user").unwrap();
|
let endpoint = base_url.join("/api/v1/client/user").unwrap();
|
||||||
let header = generate_authorization_header();
|
let header = generate_authorization_header();
|
||||||
|
|||||||
@ -9,8 +9,6 @@ use rustbreak::{deser::Bincode, PathDatabase};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::DB;
|
|
||||||
|
|
||||||
#[derive(serde::Serialize, Clone, Deserialize)]
|
#[derive(serde::Serialize, Clone, Deserialize)]
|
||||||
#[serde(rename_all="camelCase")]
|
#[serde(rename_all="camelCase")]
|
||||||
pub struct DatabaseAuth {
|
pub struct DatabaseAuth {
|
||||||
@ -35,11 +33,13 @@ pub struct Database {
|
|||||||
|
|
||||||
pub type DatabaseInterface =
|
pub type DatabaseInterface =
|
||||||
rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
|
rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
|
||||||
|
pub trait DatabaseImpls {
|
||||||
pub static DATA_ROOT_DIR: LazyLock<PathBuf> =
|
fn set_up_database() -> DatabaseInterface;
|
||||||
LazyLock::new(|| BaseDirs::new().unwrap().data_dir().join("drop"));
|
fn database_is_set_up(&self) -> bool;
|
||||||
|
fn fetch_base_url(&self) -> Url;
|
||||||
pub fn set_up_database() -> DatabaseInterface {
|
}
|
||||||
|
impl DatabaseImpls for DatabaseInterface {
|
||||||
|
fn set_up_database() -> DatabaseInterface {
|
||||||
let db_path = DATA_ROOT_DIR.join("drop.db");
|
let db_path = DATA_ROOT_DIR.join("drop.db");
|
||||||
let apps_base_dir = DATA_ROOT_DIR.join("apps");
|
let apps_base_dir = DATA_ROOT_DIR.join("apps");
|
||||||
|
|
||||||
@ -60,13 +60,16 @@ pub fn set_up_database() -> DatabaseInterface {
|
|||||||
};
|
};
|
||||||
|
|
||||||
db
|
db
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn database_is_set_up() -> bool {
|
fn database_is_set_up(&self) -> bool {
|
||||||
!DB.borrow_data().unwrap().base_url.is_empty()
|
!self.borrow_data().unwrap().base_url.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_base_url() -> Url {
|
fn fetch_base_url(&self) -> Url {
|
||||||
let handle = DB.borrow_data().unwrap();
|
let handle = self.borrow_data().unwrap();
|
||||||
Url::parse(&handle.base_url).unwrap()
|
Url::parse(&handle.base_url).unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
pub static DATA_ROOT_DIR: LazyLock<PathBuf> =
|
||||||
|
LazyLock::new(|| BaseDirs::new().unwrap().data_dir().join("drop"));
|
||||||
|
|||||||
@ -5,7 +5,7 @@ mod remote;
|
|||||||
mod unpacker;
|
mod unpacker;
|
||||||
|
|
||||||
use auth::{auth_initiate, generate_authorization_header, recieve_handshake};
|
use auth::{auth_initiate, generate_authorization_header, recieve_handshake};
|
||||||
use db::{fetch_base_url, DatabaseInterface, DATA_ROOT_DIR};
|
use db::{DatabaseInterface, DATA_ROOT_DIR};
|
||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
use http::{header::*, response::Builder as ResponseBuilder};
|
use http::{header::*, response::Builder as ResponseBuilder};
|
||||||
use library::{fetch_game, fetch_library, Game};
|
use library::{fetch_game, fetch_library, Game};
|
||||||
@ -16,6 +16,7 @@ use std::{
|
|||||||
collections::HashMap, sync::{LazyLock, Mutex}
|
collections::HashMap, sync::{LazyLock, Mutex}
|
||||||
};
|
};
|
||||||
use tauri_plugin_deep_link::DeepLinkExt;
|
use tauri_plugin_deep_link::DeepLinkExt;
|
||||||
|
use crate::db::DatabaseImpls;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Serialize)]
|
#[derive(Clone, Copy, Serialize)]
|
||||||
pub enum AppStatus {
|
pub enum AppStatus {
|
||||||
@ -53,7 +54,7 @@ fn fetch_state(state: tauri::State<'_, Mutex<AppState>>) -> Result<AppState, Str
|
|||||||
fn setup() -> AppState {
|
fn setup() -> AppState {
|
||||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||||
|
|
||||||
let is_set_up = db::database_is_set_up();
|
let is_set_up = DB.database_is_set_up();
|
||||||
if !is_set_up {
|
if !is_set_up {
|
||||||
return AppState {
|
return AppState {
|
||||||
status: AppStatus::NotConfigured,
|
status: AppStatus::NotConfigured,
|
||||||
@ -70,7 +71,7 @@ fn setup() -> AppState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(db::set_up_database);
|
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(DatabaseInterface::set_up_database);
|
||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
@ -135,7 +136,7 @@ pub fn run() {
|
|||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.register_asynchronous_uri_scheme_protocol("object", move |_ctx, request, responder| {
|
.register_asynchronous_uri_scheme_protocol("object", move |_ctx, request, responder| {
|
||||||
let base_url = fetch_base_url();
|
let base_url = DB.fetch_base_url();
|
||||||
|
|
||||||
// Drop leading /
|
// Drop leading /
|
||||||
let object_id = &request.uri().path()[1..];
|
let object_id = &request.uri().path()[1..];
|
||||||
|
|||||||
@ -4,7 +4,8 @@ use serde::{Deserialize, Serialize};
|
|||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tauri::{AppHandle, Manager};
|
use tauri::{AppHandle, Manager};
|
||||||
|
|
||||||
use crate::{auth::generate_authorization_header, db::fetch_base_url, AppState};
|
use crate::{auth::generate_authorization_header, AppState, DB};
|
||||||
|
use crate::db::DatabaseImpls;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
#[serde(rename_all="camelCase")]
|
#[serde(rename_all="camelCase")]
|
||||||
@ -24,7 +25,7 @@ pub struct Game {
|
|||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn fetch_library(app: AppHandle) -> Result<String, String> {
|
pub fn fetch_library(app: AppHandle) -> Result<String, String> {
|
||||||
let base_url = fetch_base_url();
|
let base_url = DB.fetch_base_url();
|
||||||
let library_url = base_url.join("/api/v1/client/user/library").unwrap();
|
let library_url = base_url.join("/api/v1/client/user/library").unwrap();
|
||||||
|
|
||||||
let header = generate_authorization_header();
|
let header = generate_authorization_header();
|
||||||
|
|||||||
Reference in New Issue
Block a user