Skip to content

Commit eb18990

Browse files
committed
fix: update Socket.IO configuration and improve API URL handling
- Updated the default API URL in nuxt.config.ts to use 127.0.0.1 for consistency. - Enhanced the resolveSocketApiOrigin function to ensure proper origin resolution for Socket.IO connections. - Removed redundant proxy configuration for Socket.IO in route rules. - Added comments in docker-compose.prod.yml to clarify Socket.IO and reverse-proxy setup instructions.
1 parent 5f25d48 commit eb18990

6 files changed

Lines changed: 37 additions & 12 deletions

File tree

apps/web/nuxt.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as consola from 'consola'
66
import setupApp from './src/server/extension.setup'
77
import { loadingBarHijackFilter } from './src/composables/useLoadingBarHijackFilter'
88

9-
const SESAME_APP_API_URL = process.env.SESAME_APP_API_URL || 'http://localhost:4002'
9+
const SESAME_APP_API_URL = process.env.SESAME_APP_API_URL || 'http://127.0.0.1:4000'
1010
/** URL API exposée au navigateur (WebSocket). Ex. http://mactacx:4002 si l'API est joignable sur ce host. */
1111
const SESAME_APP_PUBLIC_API_URL = process.env.SESAME_APP_PUBLIC_API_URL || ''
1212
const SESAME_ALLOWED_HOSTS = process.env.SESAME_ALLOWED_HOSTS ? process.env.SESAME_ALLOWED_HOSTS.split(',') : []
@@ -263,9 +263,6 @@ export default defineNuxtConfig({
263263
},
264264
},
265265
routeRules: {
266-
'/socket.io': {
267-
proxy: `${SOCKET_IO_PROXY_TARGET}/socket.io`,
268-
},
269266
'/api/**': {
270267
proxy: `${SESAME_APP_API_URL}/**`,
271268
},
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
/**
22
* Origine Socket.IO côté navigateur.
3-
* En dev/prod, le client se connecte à la même origine que le front ; Nitro/Vite proxifient `/socket.io` vers l'API.
3+
* Par défaut : même origine que le front (Nitro/Vite proxifient `/socket.io` vers l'API).
4+
* Si `SESAME_APP_PUBLIC_API_URL` est défini : connexion directe à l'API (requis quand le
5+
* reverse-proxy route `/socket.io` vers le port 4000 ou que le proxy Nitro n'est pas joignable).
46
*/
57
export function resolveSocketApiOrigin(): string {
6-
if (import.meta.client) {
7-
return window.location.origin
8-
}
9-
10-
const appConfig = useAppConfig()
118
const runtimeConfig = useRuntimeConfig()
129
const configuredPublic = `${runtimeConfig.public.socketApiUrl || ''}`.trim()
1310

1411
if (configuredPublic) {
1512
return new URL(configuredPublic).origin
1613
}
1714

15+
if (import.meta.client) {
16+
return window.location.origin
17+
}
18+
19+
const appConfig = useAppConfig()
1820
return `${appConfig.baseUrl || ''}`.replace(/\/$/, '')
1921
}

apps/web/src/layouts/default.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { IdentityState } from '~/constants/enums'
2424
import { useIdentityStateStore } from '~/stores/identityState'
2525
import { loadingBarDefaults } from '~/composables/useLoadingBarHijackFilter'
2626
import { attachSocketIoDebug } from '~/composables/useSocketIoDebug'
27+
import { resolveSocketApiOrigin } from '~/composables/useSocketApiOrigin'
2728
import { io, type Socket } from 'socket.io-client'
2829
2930
export default defineNuxtComponent({
@@ -158,7 +159,7 @@ export default defineNuxtComponent({
158159
159160
this.disconnectBackendsSocket()
160161
161-
this.socket = io('/core/backends', {
162+
this.socket = io(`${resolveSocketApiOrigin()}/core/backends`, {
162163
path: '/socket.io',
163164
query: { id: String(id), key: String(key) },
164165
transports: ['polling'],

apps/web/src/pages/settings/cron.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
import type { LocationQueryValue } from 'vue-router'
221221
import { reactive, ref } from 'vue'
222222
import { attachSocketIoDebug } from '~/composables/useSocketIoDebug'
223+
import { resolveSocketApiOrigin } from '~/composables/useSocketApiOrigin'
223224
import { io, type Socket } from 'socket.io-client'
224225
import { NewTargetId } from '~/constants/variables'
225226
@@ -596,7 +597,7 @@ export default defineNuxtComponent({
596597
this.logsFollowTail = true
597598
this.logsLoading = true
598599
599-
this.logsSocket = io('/core/cron', {
600+
this.logsSocket = io(`${resolveSocketApiOrigin()}/core/cron`, {
600601
path: '/socket.io',
601602
query: { id: String(id), key: String(key) },
602603
transports: ['polling'],
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineEventHandler, proxyRequest } from 'h3'
2+
3+
const DEFAULT_API_URL = 'http://127.0.0.1:4000'
4+
5+
function resolveApiBaseUrl(): string {
6+
return (process.env.SESAME_APP_API_URL || DEFAULT_API_URL).replace(/\/$/, '')
7+
}
8+
9+
export default defineEventHandler(async (event) => {
10+
const url = event.node.req.url || ''
11+
if (!url.startsWith('/socket.io')) {
12+
return
13+
}
14+
15+
return proxyRequest(event, `${resolveApiBaseUrl()}${url}`)
16+
})

docker-compose.prod.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
# docker network create sesame
66
# docker network create reverse # si reverse-proxy externe
77
#
8+
# Socket.IO (apps/web/.env) :
9+
# SESAME_APP_API_URL=http://127.0.0.1:4000
10+
# # Si le reverse-proxy route /socket.io vers le port 4000 (API) au lieu du 3000 (Nuxt) :
11+
# # SESAME_APP_PUBLIC_API_URL=https://votre-domaine
12+
#
13+
# Reverse-proxy : router /socket.io vers le port 3000 (Nuxt proxifie vers l'API)
14+
# ou directement vers le port 4000 (définir SESAME_APP_PUBLIC_API_URL ci-dessus).
15+
#
816
# Démarrage :
917
# docker compose -f docker-compose.prod.yml up -d
1018

0 commit comments

Comments
 (0)