From ab8bfbbed3cda7f428cb6d878216850f8ba9838e Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Sat, 6 May 2023 21:10:12 -0700 Subject: [PATCH] start work on moving dev db path --- src-tauri/.env | 2 +- src-tauri/Cargo.lock | 13 +++++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/app.rs | 3 +-- src-tauri/src/config.rs | 14 ++++++++------ src-tauri/src/server.rs | 11 +---------- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src-tauri/.env b/src-tauri/.env index f275894..da27150 100644 --- a/src-tauri/.env +++ b/src-tauri/.env @@ -1 +1 @@ -DATABASE_URL=sqlite://creddy.db?mode=rwc +DATABASE_URL=sqlite://C:/Users/Joe/AppData/Roaming/creddy/creddy.dev.db diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 7f28c17..2f912bb 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -69,6 +69,7 @@ dependencies = [ "aws-types", "clap", "dirs 5.0.1", + "is-terminal", "netstat2", "once_cell", "serde", @@ -2015,6 +2016,18 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "is-terminal" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" +dependencies = [ + "hermit-abi 0.3.1", + "io-lifetimes", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "itertools" version = "0.10.5" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 9369712..1362a59 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -35,6 +35,7 @@ strum_macros = "0.24" auto-launch = "0.4.0" dirs = "5.0" clap = { version = "3.2.23", features = ["derive"] } +is-terminal = "0.4.7" [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rs index fd97686..26d645a 100644 --- a/src-tauri/src/app.rs +++ b/src-tauri/src/app.rs @@ -66,6 +66,7 @@ pub async fn connect_db() -> Result { .create_if_missing(true); let pool_opts = SqlitePoolOptions::new(); let pool: SqlitePool = pool_opts.connect_with(conn_opts).await?; + sqlx::migrate!().run(&pool).await?; Ok(pool) } @@ -74,8 +75,6 @@ async fn setup(app: &mut App) -> Result<(), Box> { APP.set(app.handle()).unwrap(); let pool = connect_db().await?; - sqlx::migrate!().run(&pool).await?; - let conf = AppConfig::load(&pool).await?; let session = Session::load(&pool).await?; let srv = Server::new(conf.listen_addr, conf.listen_port, app.handle()).await?; diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 2a169bb..7dbde54 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -2,6 +2,7 @@ use std::net::Ipv4Addr; use std::path::PathBuf; use auto_launch::AutoLaunchBuilder; +use is_terminal::IsTerminal; use serde::{Serialize, Deserialize}; use sqlx::SqlitePool; @@ -90,16 +91,17 @@ pub fn set_auto_launch(is_configured: bool) -> Result<(), SetupError> { pub fn get_or_create_db_path() -> Result { - // debug_assertions doesn't always mean we are running in dev - if cfg!(debug_assertions) && std::env::var("HOME").is_ok() { - return Ok(PathBuf::from("./creddy.db")); - } - let mut path = dirs::data_dir() .ok_or(DataDirError::NotFound)?; + path.push("creddy"); std::fs::create_dir_all(&path)?; - path.push("creddy.db"); + if cfg!(debug_assertions) && std::io::stdout().is_terminal() { + path.push("creddy.dev.db"); + } + else { + path.push("creddy.db"); + } Ok(path) } diff --git a/src-tauri/src/server.rs b/src-tauri/src/server.rs index 02fe4e3..85dc094 100644 --- a/src-tauri/src/server.rs +++ b/src-tauri/src/server.rs @@ -59,16 +59,7 @@ impl Handler { return Ok(()) } let base = req_path == b"/creddy/base-credentials"; - if base { - if clients.len() != 1 - || clients[0].is_none() - || clients[0].as_ref().unwrap().exe != std::env::current_exe()? - { - self.stream.write(b"HTTP/1.0 403 Access Denied\r\n\r\n").await?; - return Ok(()) - } - } - + let req = Request {id: self.request_id, clients, base}; self.app.emit_all("credentials-request", &req)?; let starting_visibility = self.show_window()?;