From 2fc6f929f21e83f0e7c16d7eb56dc76cd1bb76ad Mon Sep 17 00:00:00 2001 From: jordojordo Date: Mon, 27 May 2024 15:06:08 -0400 Subject: [PATCH] Add proxy for websocket connection --- .env.production | 5 +++- Containerfile | 2 +- env.d.ts | 4 +++ nginx.conf.template | 5 +++- scripts/start.sh | 18 ++++++++++--- src/App.vue | 16 ++++++----- src/components/Config.vue | 46 ++++++++++++++++++++++---------- src/services/websocket.ts | 36 ++++++++++++++++++++++--- src/stores/useToolConfigStore.ts | 33 +++++++++++++++++++---- src/types/chat.ts | 8 ++++++ src/utils/service.ts | 15 +++++++++++ 11 files changed, 154 insertions(+), 34 deletions(-) create mode 100644 src/utils/service.ts diff --git a/.env.production b/.env.production index fd7c7dd..63aea35 100644 --- a/.env.production +++ b/.env.production @@ -1 +1,4 @@ -VITE_THOTHSCRIPT_API=CLIENT_THOTHSCRIPT_API \ No newline at end of file +VITE_WS_SCHEME=CLIENT_THOTHSCRIPT_PROXY_SCHEME +VITE_WS_HOST=CLIENT_THOTHSCRIPT_PROXY_HOST +VITE_WS_PORT=CLIENT_THOTHSCRIPT_PROXY_PORT +VITE_WS_PATH=CLIENT_THOTHSCRIPT_PROXY_PATH diff --git a/Containerfile b/Containerfile index 907c8c9..fd42d6e 100644 --- a/Containerfile +++ b/Containerfile @@ -1,4 +1,4 @@ -FROM node:latest as build-stage +FROM node:22-alpine as build-stage WORKDIR /app COPY package*.json ./ RUN yarn diff --git a/env.d.ts b/env.d.ts index e26bddf..0aac3c7 100644 --- a/env.d.ts +++ b/env.d.ts @@ -2,6 +2,10 @@ interface ImportMetaEnv { readonly VITE_THOTHSCRIPT_API: string; + readonly VITE_WS_SECURE: boolean; + readonly VITE_WS_HOST: string; + readonly VITE_WS_PORT: string; + readonly VITE_WS_PATH: string; readonly BASE_URL: string; readonly PROD: boolean } diff --git a/nginx.conf.template b/nginx.conf.template index e35857f..e233918 100644 --- a/nginx.conf.template +++ b/nginx.conf.template @@ -31,11 +31,14 @@ http { } location /ws/ { - proxy_pass __CLIENT_THOTHSCRIPT_API__; + proxy_pass http://__CLIENT_THOTHSCRIPT_OPERATOR_HOST__.__CLIENT_THOTHSCRIPT_OPERATOR_NAMESPACE__.svc.cluster.local:__CLIENT_THOTHSCRIPT_OPERATOR_PORT__; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; access_log /var/log/nginx/websocket_access.log main; } diff --git a/scripts/start.sh b/scripts/start.sh index 712e2c2..e5eea6f 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,10 +1,22 @@ #!/bin/sh -# Provide default values for CLIENT_THOTHSCRIPT_API if it's not set -: "${CLIENT_THOTHSCRIPT_API:=localhost:3000}" + +# Provide default values for CLIENT_THOTHSCRIPT_API_SCHEME, CLIENT_THOTHSCRIPT_API_HOST, CLIENT_THOTHSCRIPT_API_PORT, and CLIENT_THOTHSCRIPT_API_PATH if not set +: "${CLIENT_THOTHSCRIPT_PROXY_SCHEME:=ws}" +: "${CLIENT_THOTHSCRIPT_PROXY_HOST:=localhost}" +: "${CLIENT_THOTHSCRIPT_PROXY_PORT:=80}" +: "${CLIENT_THOTHSCRIPT_PROXY_PATH:=/ws/}" + +# Provide default values for CLIENT_THOTHSCRIPT_OPERATOR_HOST, CLIENT_THOTHSCRIPT_OPERATOR_NAMESPACE, and CLIENT_THOTHSCRIPT_OPERATOR_PORT if not set +: "${CLIENT_THOTHSCRIPT_OPERATOR_HOST:=thothscript-operator}" +: "${CLIENT_THOTHSCRIPT_OPERATOR_NAMESPACE:=thothscript}" +: "${CLIENT_THOTHSCRIPT_OPERATOR_PORT:=3000}" # Substitute environment variables in nginx.conf.template and create nginx.conf -sed "s|__CLIENT_THOTHSCRIPT_API__|http:\/\/$CLIENT_THOTHSCRIPT_API|g" /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf +sed -e "s|__CLIENT_THOTHSCRIPT_OPERATOR_HOST__|${CLIENT_THOTHSCRIPT_OPERATOR_HOST}|g" \ + -e "s|__CLIENT_THOTHSCRIPT_OPERATOR_NAMESPACE__|${CLIENT_THOTHSCRIPT_OPERATOR_NAMESPACE}|g" \ + -e "s|__CLIENT_THOTHSCRIPT_OPERATOR_PORT__|${CLIENT_THOTHSCRIPT_OPERATOR_PORT}|g" \ + /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf for i in $(env | grep CLIENT_); do key=$(echo $i | cut -d '=' -f 1) diff --git a/src/App.vue b/src/App.vue index da12d53..f4fcbbf 100644 --- a/src/App.vue +++ b/src/App.vue @@ -7,21 +7,25 @@ import websocket from '@/services/websocket'; const toolConfigStore = useToolConfigStore(); +toolConfigStore.loadFromLocalStorage(); + onMounted(() => { - websocket.connect(toolConfigStore.websocketUrl); + websocket.connect(); }); onUnmounted(() => { websocket.disconnect(); }); -// Watch for changes in the WebSocket URL and reconnect if it changes -watch(() => toolConfigStore.websocketUrl, (newUrl, oldUrl) => { - if ( newUrl !== oldUrl ) { +// Watch for changes in the WebSocket config and reconnect if it changes +watch(() => toolConfigStore.websocket, () => { + if ( toolConfigStore.shouldReconnect ) { websocket.disconnect(); - websocket.connect(newUrl); + websocket.connect(); + + toolConfigStore.setShouldReconnect(false); } -}); +}, { deep: true });