From 3b05cde5437db59d9e4d5d4ae7768ed7fde67083 Mon Sep 17 00:00:00 2001 From: Bhasher Date: Fri, 12 Jan 2024 13:35:42 +0100 Subject: [PATCH] Set tauri PUBLIC_BACKEND_URL from env --- frontend/src-tauri/src/main.rs | 6 ++++++ frontend/src-tauri/tauri.conf.json | 7 ++++--- frontend/src/lib/utils/api.ts | 24 +++++++++++++++++++++- frontend/src/lib/utils/webSocketManager.ts | 4 ++-- frontend/src/routes/+page.svelte | 2 ++ frontend/src/routes/project/+page.svelte | 2 ++ 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/frontend/src-tauri/src/main.rs b/frontend/src-tauri/src/main.rs index f5c5be2..ed2f42c 100644 --- a/frontend/src-tauri/src/main.rs +++ b/frontend/src-tauri/src/main.rs @@ -3,6 +3,12 @@ fn main() { tauri::Builder::default() + .invoke_handler(tauri::generate_handler![get_backend_url]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } + +#[tauri::command] +fn get_backend_url() -> String { + std::env::var("PUBLIC_BACKEND_URL").unwrap_or_else(|_| "http://localhost:3000".to_string()) +} \ No newline at end of file diff --git a/frontend/src-tauri/tauri.conf.json b/frontend/src-tauri/tauri.conf.json index 84eb104..35ac165 100644 --- a/frontend/src-tauri/tauri.conf.json +++ b/frontend/src-tauri/tauri.conf.json @@ -4,7 +4,8 @@ "beforeBuildCommand": "npm run build", "beforeDevCommand": "npm run dev", "devPath": "http://localhost:5173", - "distDir": "../build" + "distDir": "../build", + "withGlobalTauri": true }, "package": { "productName": "Focus", @@ -56,10 +57,10 @@ "windows": [ { "fullscreen": false, - "height": 600, + "height": 800, "resizable": true, "title": "Focus", - "width": 800 + "width": 1000 } ] } diff --git a/frontend/src/lib/utils/api.ts b/frontend/src/lib/utils/api.ts index cedf4a8..9fe4091 100644 --- a/frontend/src/lib/utils/api.ts +++ b/frontend/src/lib/utils/api.ts @@ -4,7 +4,29 @@ import { toastAlert } from './toasts'; // import { env } from '$env/dynamic/public'; // const backend = env.PUBLIC_BACKEND_URL || 'http://localhost:3000'; -const backendUrl = 'http://localhost:3000'; +let backendUrl = 'http://localhost:3000'; +let backendWsUrl = 'ws://localhost:3000'; + +export function getBackendWsUrl() { + return backendWsUrl; +} + +export async function checkTauriUrl(window: any) { + if (window.__TAURI__) { + console.log('Running in Tauri'); + await window.__TAURI__ + .invoke('get_backend_url') + .then((url: string) => { + if (url && url !== '') { + backendUrl = url; + backendWsUrl = url.replace('http', 'ws'); + axiosInstance.defaults.baseURL = backendUrl + '/api'; + console.log('Backend URL:', backendUrl); + } + }) + .catch(console.error); + } +} let pendingRequests = 0; diff --git a/frontend/src/lib/utils/webSocketManager.ts b/frontend/src/lib/utils/webSocketManager.ts index 0bac38e..6c9742e 100644 --- a/frontend/src/lib/utils/webSocketManager.ts +++ b/frontend/src/lib/utils/webSocketManager.ts @@ -1,5 +1,5 @@ import Project, { projects } from '$lib/types/Project'; -import { hasPendingRequests } from '$lib/utils/api'; +import { getBackendWsUrl, hasPendingRequests } from '$lib/utils/api'; import { toastAlert, toastWarning } from '$lib/utils/toasts'; import { get } from 'svelte/store'; @@ -27,7 +27,7 @@ export default class WebSocketManager { return; } - this._socket = new WebSocket('ws://localhost:3000/api/v1/ws'); + this._socket = new WebSocket(getBackendWsUrl() + '/api/v1/ws'); this._socket.onopen = () => { this._reconnectAttempts = 0; diff --git a/frontend/src/routes/+page.svelte b/frontend/src/routes/+page.svelte index 4533a71..1c8ce05 100644 --- a/frontend/src/routes/+page.svelte +++ b/frontend/src/routes/+page.svelte @@ -1,6 +1,7 @@