From 915e0e86138290dcf209519dedd39d65d3571266 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 1 Mar 2023 10:31:50 -0800 Subject: [PATCH] Fix migrations for build and iframe rendering --- src-tauri/src/main.rs | 24 +++++++++++++++--------- src-tauri/tauri.toml | 2 +- src-web/components/ResponsePane.tsx | 9 +++++---- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 51eb1d20..94b5005a 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -23,7 +23,6 @@ use tauri::{AppHandle, State, Wry}; use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowEvent}; use tokio::sync::Mutex; -use crate::models::{update_response, HttpResponse}; use window_ext::WindowExt; mod models; @@ -42,11 +41,16 @@ pub struct CustomResponse { pub status_reason: Option<&'static str>, } -async fn migrate_db(db_instance: &Mutex>) -> Result<(), String> { +async fn migrate_db( + app_handle: AppHandle, + db_instance: &Mutex>, +) -> Result<(), String> { let pool = &*db_instance.lock().await; - let m = Migrator::new(Path::new("./migrations")) - .await - .expect("Failed to load migrations"); + let p = app_handle + .path_resolver() + .resolve_resource("migrations") + .expect("failed to resolve resource"); + let m = Migrator::new(p).await.expect("Failed to load migrations"); m.run(pool).await.expect("Failed to run migrations"); println!("Migrations ran"); Ok(()) @@ -148,7 +152,7 @@ async fn send_request( response.url = v.url().to_string(); response.body = v.text().await.expect("Failed to get body"); response.elapsed = start.elapsed().as_millis() as i64; - response = update_response(response, pool) + response = models::update_response(response, pool) .await .expect("Failed to update response"); app_handle.emit_all("updated_response", &response).unwrap(); @@ -159,13 +163,13 @@ async fn send_request( } async fn response_err( - mut response: HttpResponse, + mut response: models::HttpResponse, error: String, app_handle: AppHandle, pool: &Pool, ) -> Result { response.error = Some(error.clone()); - response = update_response(response, pool) + response = models::update_response(response, pool) .await .expect("Failed to update response"); app_handle.emit_all("updated_response", &response).unwrap(); @@ -339,7 +343,9 @@ fn main() { .await .expect("Failed to connect to database"); let m = Mutex::new(pool); - migrate_db(&m).await.expect("Failed to migrate database"); + migrate_db(app.handle(), &m) + .await + .expect("Failed to migrate database"); app.manage(m); Ok(()) }) diff --git a/src-tauri/tauri.toml b/src-tauri/tauri.toml index b8a5eb6f..39e5d3d9 100644 --- a/src-tauri/tauri.toml +++ b/src-tauri/tauri.toml @@ -36,7 +36,7 @@ icon = [ ] identifier = "co.schier.twosomnia" longDescription = "" -resources = [ "plugins/*" ] +resources = [ "plugins/*", "migrations/*" ] shortDescription = "" targets = "all" diff --git a/src-web/components/ResponsePane.tsx b/src-web/components/ResponsePane.tsx index 326a856c..524a70c9 100644 --- a/src-web/components/ResponsePane.tsx +++ b/src-web/components/ResponsePane.tsx @@ -34,13 +34,14 @@ export function ResponsePane({ requestId, error }: Props) { [response], ); - const contentForIframe: string = useMemo(() => { - if (response == null) return ''; + const contentForIframe: string | null = useMemo(() => { + if (!contentType.includes('html')) return null; + if (response == null) return null; if (response.body.includes('')) { return response.body.replace(//gi, ``); } return response.body; - }, [response?.id]); + }, [response?.body, contentType]); return ( @@ -97,7 +98,7 @@ export function ResponsePane({ requestId, error }: Props) { )} - {viewMode === 'pretty' && contentType.includes('html') ? ( + {viewMode === 'pretty' && contentForIframe !== null ? (