Fix migrations for build and iframe rendering

This commit is contained in:
Gregory Schier
2023-03-01 10:31:50 -08:00
parent 7490106014
commit c6ba978069
3 changed files with 21 additions and 14 deletions

View File

@@ -23,7 +23,6 @@ use tauri::{AppHandle, State, Wry};
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowEvent}; use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowEvent};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use crate::models::{update_response, HttpResponse};
use window_ext::WindowExt; use window_ext::WindowExt;
mod models; mod models;
@@ -42,11 +41,16 @@ pub struct CustomResponse {
pub status_reason: Option<&'static str>, pub status_reason: Option<&'static str>,
} }
async fn migrate_db(db_instance: &Mutex<Pool<Sqlite>>) -> Result<(), String> { async fn migrate_db(
app_handle: AppHandle<Wry>,
db_instance: &Mutex<Pool<Sqlite>>,
) -> Result<(), String> {
let pool = &*db_instance.lock().await; let pool = &*db_instance.lock().await;
let m = Migrator::new(Path::new("./migrations")) let p = app_handle
.await .path_resolver()
.expect("Failed to load migrations"); .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"); m.run(pool).await.expect("Failed to run migrations");
println!("Migrations ran"); println!("Migrations ran");
Ok(()) Ok(())
@@ -148,7 +152,7 @@ async fn send_request(
response.url = v.url().to_string(); response.url = v.url().to_string();
response.body = v.text().await.expect("Failed to get body"); response.body = v.text().await.expect("Failed to get body");
response.elapsed = start.elapsed().as_millis() as i64; response.elapsed = start.elapsed().as_millis() as i64;
response = update_response(response, pool) response = models::update_response(response, pool)
.await .await
.expect("Failed to update response"); .expect("Failed to update response");
app_handle.emit_all("updated_response", &response).unwrap(); app_handle.emit_all("updated_response", &response).unwrap();
@@ -159,13 +163,13 @@ async fn send_request(
} }
async fn response_err( async fn response_err(
mut response: HttpResponse, mut response: models::HttpResponse,
error: String, error: String,
app_handle: AppHandle<Wry>, app_handle: AppHandle<Wry>,
pool: &Pool<Sqlite>, pool: &Pool<Sqlite>,
) -> Result<String, String> { ) -> Result<String, String> {
response.error = Some(error.clone()); response.error = Some(error.clone());
response = update_response(response, pool) response = models::update_response(response, pool)
.await .await
.expect("Failed to update response"); .expect("Failed to update response");
app_handle.emit_all("updated_response", &response).unwrap(); app_handle.emit_all("updated_response", &response).unwrap();
@@ -339,7 +343,9 @@ fn main() {
.await .await
.expect("Failed to connect to database"); .expect("Failed to connect to database");
let m = Mutex::new(pool); 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); app.manage(m);
Ok(()) Ok(())
}) })

View File

@@ -36,7 +36,7 @@ icon = [
] ]
identifier = "co.schier.twosomnia" identifier = "co.schier.twosomnia"
longDescription = "" longDescription = ""
resources = [ "plugins/*" ] resources = [ "plugins/*", "migrations/*" ]
shortDescription = "" shortDescription = ""
targets = "all" targets = "all"

View File

@@ -34,13 +34,14 @@ export function ResponsePane({ requestId, error }: Props) {
[response], [response],
); );
const contentForIframe: string = useMemo(() => { const contentForIframe: string | null = useMemo(() => {
if (response == null) return ''; if (!contentType.includes('html')) return null;
if (response == null) return null;
if (response.body.includes('<head>')) { if (response.body.includes('<head>')) {
return response.body.replace(/<head>/gi, `<head><base href="${response.url}"/>`); return response.body.replace(/<head>/gi, `<head><base href="${response.url}"/>`);
} }
return response.body; return response.body;
}, [response?.id]); }, [response?.body, contentType]);
return ( return (
<VStack className="w-full"> <VStack className="w-full">
@@ -97,7 +98,7 @@ export function ResponsePane({ requestId, error }: Props) {
)} )}
</HStack> </HStack>
</HStack> </HStack>
{viewMode === 'pretty' && contentType.includes('html') ? ( {viewMode === 'pretty' && contentForIframe !== null ? (
<iframe <iframe
title="Response preview" title="Response preview"
srcDoc={contentForIframe} srcDoc={contentForIframe}