config nginx to fix sse not found bug

This commit is contained in:
Do Manh Phuong 2025-08-25 09:40:11 +07:00
parent 075bcb40ed
commit c9726d00a0
5 changed files with 38 additions and 36 deletions

2
.gitignore vendored
View File

@ -4,7 +4,7 @@ dist
dist-ssr dist-ssr
*.local *.local
count.txt count.txt
.env .env*
.nitro .nitro
.tanstack .tanstack
.vscode/ .vscode/

View File

@ -25,7 +25,7 @@ server {
} }
location /api/ { location /api/ {
proxy_pass http://backend_api; proxy_pass http://backend/;
# CORS headers # CORS headers
add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Origin' '*' always;
@ -36,4 +36,14 @@ server {
return 204; 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;
}
} }

View File

@ -1,38 +1,22 @@
const hostname = window.location.hostname; const isDev = import.meta.env.MODE === "development";
console.log("Current hostname:", hostname);
var tailscaleIP = import.meta.env.VITE_IP_TAILSCALE;
var localIP = import.meta.env.VITE_IP_LOCAL;
let API_MODE: "tailscale" | "local" | "ip"; export const BASE_URL = isDev
? import.meta.env.VITE_API_URL_DEV
if (hostname.includes(tailscaleIP)) { : "/api";
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 API_ENDPOINTS = { export const API_ENDPOINTS = {
APP_VERSION: { APP_VERSION: {
GET_VERSION: `/AppVersion/version`, GET_VERSION: `${BASE_URL}/AppVersion/version`,
UPLOAD: `/AppVersion/upload`, UPLOAD: `${BASE_URL}/AppVersion/upload`,
}, },
DEVICE_COMM: { DEVICE_COMM: {
UPDATE_AGENT: `/DeviceComm/updateagent`, UPDATE_AGENT: `${BASE_URL}/DeviceComm/updateagent`,
GET_ROOM_LIST: `/DeviceComm/rooms`, GET_ROOM_LIST: `${BASE_URL}/DeviceComm/rooms`,
GET_DEVICE_FROM_ROOM: (roomName: string) => `/DeviceComm/room/${roomName}`, GET_DEVICE_FROM_ROOM: (roomName: string) =>
`${BASE_URL}/DeviceComm/room/${roomName}`,
}, },
SSE_EVENTS: { SSE_EVENTS: {
DEVICE_ONLINE: `/Sse/events/onlineDevices`, DEVICE_ONLINE: `${BASE_URL}/Sse/events/onlineDevices`,
DEVICE_OFFLINE: `/Sse/events/offlineDevices`, DEVICE_OFFLINE: `${BASE_URL}/Sse/events/offlineDevices`,
}, },
}; };

View File

@ -16,8 +16,8 @@ interface UseDeviceEventsOptions {
export function useDeviceEvents(options: UseDeviceEventsOptions) { export function useDeviceEvents(options: UseDeviceEventsOptions) {
useEffect(() => { useEffect(() => {
const onlineES = new EventSource(`${BASE_URL}${API_ENDPOINTS.SSE_EVENTS.DEVICE_ONLINE}`); const onlineES = new EventSource(API_ENDPOINTS.SSE_EVENTS.DEVICE_ONLINE);
const offlineES = new EventSource(`${BASE_URL}${API_ENDPOINTS.SSE_EVENTS.DEVICE_OFFLINE}`); const offlineES = new EventSource(API_ENDPOINTS.SSE_EVENTS.DEVICE_OFFLINE);
onlineES.addEventListener("online", (event) => { onlineES.addEventListener("online", (event) => {
try { try {

View File

@ -45,7 +45,13 @@ function RoomComponent() {
queryClient.setQueryData(["rooms"], (oldRooms: any[] = []) => queryClient.setQueryData(["rooms"], (oldRooms: any[] = []) =>
oldRooms.map((r) => oldRooms.map((r) =>
r.name === room r.name === room
? { ...r, offlineCount: Math.max((r.offlineCount || 0) - 1, 0) } ? {
...r,
numberOfOfflineDevices: Math.max(
(r.numberOfOfflineDevices || 0) - 1,
0
),
}
: r : r
) )
); );
@ -54,7 +60,10 @@ function RoomComponent() {
queryClient.setQueryData(["rooms"], (oldRooms: any[] = []) => queryClient.setQueryData(["rooms"], (oldRooms: any[] = []) =>
oldRooms.map((r) => oldRooms.map((r) =>
r.name === room r.name === room
? { ...r, offlineCount: (r.offlineCount || 0) + 1 } ? {
...r,
numberOfOfflineDevices: (r.numberOfOfflineDevices || 0) + 1,
}
: r : r
) )
); );
@ -76,8 +85,7 @@ function RoomComponent() {
}, },
{ {
header: "Thiết bị offline", header: "Thiết bị offline",
accessorKey: "offlineCount", accessorKey: "numberOfOfflineDevices",
cell: ({ getValue }) => getValue() || 0,
}, },
]; ];