Set tauri PUBLIC_BACKEND_URL from env
This commit is contained in:
parent
60321b19c5
commit
3b05cde543
|
@ -3,6 +3,12 @@
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
|
.invoke_handler(tauri::generate_handler![get_backend_url])
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.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())
|
||||||
|
}
|
|
@ -4,7 +4,8 @@
|
||||||
"beforeBuildCommand": "npm run build",
|
"beforeBuildCommand": "npm run build",
|
||||||
"beforeDevCommand": "npm run dev",
|
"beforeDevCommand": "npm run dev",
|
||||||
"devPath": "http://localhost:5173",
|
"devPath": "http://localhost:5173",
|
||||||
"distDir": "../build"
|
"distDir": "../build",
|
||||||
|
"withGlobalTauri": true
|
||||||
},
|
},
|
||||||
"package": {
|
"package": {
|
||||||
"productName": "Focus",
|
"productName": "Focus",
|
||||||
|
@ -56,10 +57,10 @@
|
||||||
"windows": [
|
"windows": [
|
||||||
{
|
{
|
||||||
"fullscreen": false,
|
"fullscreen": false,
|
||||||
"height": 600,
|
"height": 800,
|
||||||
"resizable": true,
|
"resizable": true,
|
||||||
"title": "Focus",
|
"title": "Focus",
|
||||||
"width": 800
|
"width": 1000
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,29 @@ import { toastAlert } from './toasts';
|
||||||
// import { env } from '$env/dynamic/public';
|
// import { env } from '$env/dynamic/public';
|
||||||
|
|
||||||
// const backend = env.PUBLIC_BACKEND_URL || 'http://localhost:3000';
|
// 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;
|
let pendingRequests = 0;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import Project, { projects } from '$lib/types/Project';
|
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 { toastAlert, toastWarning } from '$lib/utils/toasts';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ export default class WebSocketManager {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._socket = new WebSocket('ws://localhost:3000/api/v1/ws');
|
this._socket = new WebSocket(getBackendWsUrl() + '/api/v1/ws');
|
||||||
|
|
||||||
this._socket.onopen = () => {
|
this._socket.onopen = () => {
|
||||||
this._reconnectAttempts = 0;
|
this._reconnectAttempts = 0;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import projectsApi from '$lib/api/projectsApi';
|
import projectsApi from '$lib/api/projectsApi';
|
||||||
import Project, { projects } from '$lib/types/Project';
|
import Project, { projects } from '$lib/types/Project';
|
||||||
|
import { checkTauriUrl } from '$lib/utils/api';
|
||||||
import WebSocketManager from '$lib/utils/webSocketManager';
|
import WebSocketManager from '$lib/utils/webSocketManager';
|
||||||
import { SvelteToast } from '@zerodevx/svelte-toast';
|
import { SvelteToast } from '@zerodevx/svelte-toast';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
@ -9,6 +10,7 @@
|
||||||
const wsManager = new WebSocketManager();
|
const wsManager = new WebSocketManager();
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
|
await checkTauriUrl(window);
|
||||||
await projectsApi.getAll();
|
await projectsApi.getAll();
|
||||||
wsManager.connect();
|
wsManager.connect();
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
import currentView from '$lib/stores/currentView';
|
import currentView from '$lib/stores/currentView';
|
||||||
import type Project from '$lib/types/Project';
|
import type Project from '$lib/types/Project';
|
||||||
import { views } from '$lib/types/View';
|
import { views } from '$lib/types/View';
|
||||||
|
import { checkTauriUrl } from '$lib/utils/api';
|
||||||
import { SvelteToast } from '@zerodevx/svelte-toast';
|
import { SvelteToast } from '@zerodevx/svelte-toast';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
|
@ -13,6 +14,7 @@
|
||||||
let project: Project;
|
let project: Project;
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
|
await checkTauriUrl(window);
|
||||||
const projectId = parseInt($page.url.searchParams.get('id') || '0');
|
const projectId = parseInt($page.url.searchParams.get('id') || '0');
|
||||||
const res = await projectsApi.get(projectId);
|
const res = await projectsApi.get(projectId);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue