mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-13 08:12:44 +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 url::Url;
|
||||
|
||||
use crate::{db::{fetch_base_url, DatabaseAuth}, AppState, AppStatus, User, DB};
|
||||
use crate::{db::{DatabaseAuth, DatabaseImpls}, AppState, AppStatus, User, DB};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
@ -81,7 +81,7 @@ pub fn generate_authorization_header() -> String {
|
||||
}
|
||||
|
||||
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 header = generate_authorization_header();
|
||||
|
||||
@ -9,8 +9,6 @@ use rustbreak::{deser::Bincode, PathDatabase};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
use crate::DB;
|
||||
|
||||
#[derive(serde::Serialize, Clone, Deserialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
pub struct DatabaseAuth {
|
||||
@ -35,11 +33,13 @@ pub struct Database {
|
||||
|
||||
pub type DatabaseInterface =
|
||||
rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
|
||||
|
||||
pub static DATA_ROOT_DIR: LazyLock<PathBuf> =
|
||||
LazyLock::new(|| BaseDirs::new().unwrap().data_dir().join("drop"));
|
||||
|
||||
pub fn set_up_database() -> DatabaseInterface {
|
||||
pub trait DatabaseImpls {
|
||||
fn set_up_database() -> DatabaseInterface;
|
||||
fn database_is_set_up(&self) -> bool;
|
||||
fn fetch_base_url(&self) -> Url;
|
||||
}
|
||||
impl DatabaseImpls for DatabaseInterface {
|
||||
fn set_up_database() -> DatabaseInterface {
|
||||
let db_path = DATA_ROOT_DIR.join("drop.db");
|
||||
let apps_base_dir = DATA_ROOT_DIR.join("apps");
|
||||
|
||||
@ -62,11 +62,14 @@ pub fn set_up_database() -> DatabaseInterface {
|
||||
db
|
||||
}
|
||||
|
||||
pub fn database_is_set_up() -> bool {
|
||||
!DB.borrow_data().unwrap().base_url.is_empty()
|
||||
fn database_is_set_up(&self) -> bool {
|
||||
!self.borrow_data().unwrap().base_url.is_empty()
|
||||
}
|
||||
|
||||
pub fn fetch_base_url() -> Url {
|
||||
let handle = DB.borrow_data().unwrap();
|
||||
fn fetch_base_url(&self) -> Url {
|
||||
let handle = self.borrow_data().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;
|
||||
|
||||
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 http::{header::*, response::Builder as ResponseBuilder};
|
||||
use library::{fetch_game, fetch_library, Game};
|
||||
@ -16,6 +16,7 @@ use std::{
|
||||
collections::HashMap, sync::{LazyLock, Mutex}
|
||||
};
|
||||
use tauri_plugin_deep_link::DeepLinkExt;
|
||||
use crate::db::DatabaseImpls;
|
||||
|
||||
#[derive(Clone, Copy, Serialize)]
|
||||
pub enum AppStatus {
|
||||
@ -53,7 +54,7 @@ fn fetch_state(state: tauri::State<'_, Mutex<AppState>>) -> Result<AppState, Str
|
||||
fn setup() -> AppState {
|
||||
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 {
|
||||
return AppState {
|
||||
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)]
|
||||
pub fn run() {
|
||||
@ -135,7 +136,7 @@ pub fn run() {
|
||||
Ok(())
|
||||
})
|
||||
.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 /
|
||||
let object_id = &request.uri().path()[1..];
|
||||
|
||||
@ -4,7 +4,8 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
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)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
@ -24,7 +25,7 @@ pub struct Game {
|
||||
|
||||
#[tauri::command]
|
||||
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 header = generate_authorization_header();
|
||||
|
||||
Reference in New Issue
Block a user