Set tauri PUBLIC_BACKEND_URL from env

This commit is contained in:
Brieuc Dubois 2024-01-12 13:35:42 +01:00
parent 60321b19c5
commit 3b05cde543
6 changed files with 39 additions and 6 deletions

View File

@ -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())
}

View File

@ -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
}
]
}

View File

@ -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;

View File

@ -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;

View File

@ -1,6 +1,7 @@
<script lang="ts">
import projectsApi from '$lib/api/projectsApi';
import Project, { projects } from '$lib/types/Project';
import { checkTauriUrl } from '$lib/utils/api';
import WebSocketManager from '$lib/utils/webSocketManager';
import { SvelteToast } from '@zerodevx/svelte-toast';
import { onMount } from 'svelte';
@ -9,6 +10,7 @@
const wsManager = new WebSocketManager();
onMount(async () => {
await checkTauriUrl(window);
await projectsApi.getAll();
wsManager.connect();
});

View File

@ -6,6 +6,7 @@
import currentView from '$lib/stores/currentView';
import type Project from '$lib/types/Project';
import { views } from '$lib/types/View';
import { checkTauriUrl } from '$lib/utils/api';
import { SvelteToast } from '@zerodevx/svelte-toast';
import { onMount } from 'svelte';
import { get } from 'svelte/store';
@ -13,6 +14,7 @@
let project: Project;
onMount(async () => {
await checkTauriUrl(window);
const projectId = parseInt($page.url.searchParams.get('id') || '0');
const res = await projectsApi.get(projectId);