diff --git a/.gitignore b/.gitignore index caa130f..d125dd8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ dist dist-ssr *.local count.txt -.env +.env* .nitro .tanstack .vscode/ diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 8a4c668..d97a2a7 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -25,7 +25,7 @@ server { } location /api/ { - proxy_pass http://backend_api; + proxy_pass http://backend/; # CORS headers add_header 'Access-Control-Allow-Origin' '*' always; @@ -36,4 +36,14 @@ server { return 204; } } + location /api/Sse/events { + proxy_pass http://backend/api/Sse/events; + proxy_http_version 1.1; + + # cần thiết cho SSE + proxy_set_header Connection ''; + proxy_buffering off; + proxy_cache off; + proxy_read_timeout 1h; + } } diff --git a/src/config/api.ts b/src/config/api.ts index dd63873..4fa3675 100644 --- a/src/config/api.ts +++ b/src/config/api.ts @@ -1,38 +1,22 @@ -const hostname = window.location.hostname; -console.log("Current hostname:", hostname); -var tailscaleIP = import.meta.env.VITE_IP_TAILSCALE; -var localIP = import.meta.env.VITE_IP_LOCAL; +const isDev = import.meta.env.MODE === "development"; -let API_MODE: "tailscale" | "local" | "ip"; - -if (hostname.includes(tailscaleIP)) { - API_MODE = "tailscale"; -} else if (hostname === localIP) { - API_MODE = "local"; -} else { - API_MODE = "ip"; -} - -export const API_ROOT = { - tailscale: import.meta.env.VITE_API_URL_TAILSCALE, - local: import.meta.env.VITE_API_URL_LOCAL, - ip: import.meta.env.VITE_API_URL_IP, -}; - -export const BASE_URL = API_ROOT[API_MODE]; +export const BASE_URL = isDev + ? import.meta.env.VITE_API_URL_DEV + : "/api"; export const API_ENDPOINTS = { APP_VERSION: { - GET_VERSION: `/AppVersion/version`, - UPLOAD: `/AppVersion/upload`, + GET_VERSION: `${BASE_URL}/AppVersion/version`, + UPLOAD: `${BASE_URL}/AppVersion/upload`, }, DEVICE_COMM: { - UPDATE_AGENT: `/DeviceComm/updateagent`, - GET_ROOM_LIST: `/DeviceComm/rooms`, - GET_DEVICE_FROM_ROOM: (roomName: string) => `/DeviceComm/room/${roomName}`, + UPDATE_AGENT: `${BASE_URL}/DeviceComm/updateagent`, + GET_ROOM_LIST: `${BASE_URL}/DeviceComm/rooms`, + GET_DEVICE_FROM_ROOM: (roomName: string) => + `${BASE_URL}/DeviceComm/room/${roomName}`, }, SSE_EVENTS: { - DEVICE_ONLINE: `/Sse/events/onlineDevices`, - DEVICE_OFFLINE: `/Sse/events/offlineDevices`, + DEVICE_ONLINE: `${BASE_URL}/Sse/events/onlineDevices`, + DEVICE_OFFLINE: `${BASE_URL}/Sse/events/offlineDevices`, }, }; diff --git a/src/hooks/useDeviceEvents.ts b/src/hooks/useDeviceEvents.ts index a0cdcfd..0533a37 100644 --- a/src/hooks/useDeviceEvents.ts +++ b/src/hooks/useDeviceEvents.ts @@ -16,8 +16,8 @@ interface UseDeviceEventsOptions { export function useDeviceEvents(options: UseDeviceEventsOptions) { useEffect(() => { - const onlineES = new EventSource(`${BASE_URL}${API_ENDPOINTS.SSE_EVENTS.DEVICE_ONLINE}`); - const offlineES = new EventSource(`${BASE_URL}${API_ENDPOINTS.SSE_EVENTS.DEVICE_OFFLINE}`); + const onlineES = new EventSource(API_ENDPOINTS.SSE_EVENTS.DEVICE_ONLINE); + const offlineES = new EventSource(API_ENDPOINTS.SSE_EVENTS.DEVICE_OFFLINE); onlineES.addEventListener("online", (event) => { try { diff --git a/src/routes/_authenticated/room/index.tsx b/src/routes/_authenticated/room/index.tsx index df53889..52359e3 100644 --- a/src/routes/_authenticated/room/index.tsx +++ b/src/routes/_authenticated/room/index.tsx @@ -45,7 +45,13 @@ function RoomComponent() { queryClient.setQueryData(["rooms"], (oldRooms: any[] = []) => oldRooms.map((r) => r.name === room - ? { ...r, offlineCount: Math.max((r.offlineCount || 0) - 1, 0) } + ? { + ...r, + numberOfOfflineDevices: Math.max( + (r.numberOfOfflineDevices || 0) - 1, + 0 + ), + } : r ) ); @@ -54,7 +60,10 @@ function RoomComponent() { queryClient.setQueryData(["rooms"], (oldRooms: any[] = []) => oldRooms.map((r) => r.name === room - ? { ...r, offlineCount: (r.offlineCount || 0) + 1 } + ? { + ...r, + numberOfOfflineDevices: (r.numberOfOfflineDevices || 0) + 1, + } : r ) ); @@ -76,8 +85,7 @@ function RoomComponent() { }, { header: "Thiết bị offline", - accessorKey: "offlineCount", - cell: ({ getValue }) => getValue() || 0, + accessorKey: "numberOfOfflineDevices", }, ];