From e4c3f9fe53edc896060bdd75a8cb18e1e8e51ba5 Mon Sep 17 00:00:00 2001 From: isKonstantin Date: Sun, 28 Jul 2024 03:19:23 +0300 Subject: [PATCH] Added the ability to change the server --- components/serverChanger.vue | 31 +++++++++++++++++++++++++++++++ composables/useApi.ts | 4 +++- composables/useApiLoader.ts | 8 +++----- composables/useLazyApi.ts | 3 ++- composables/useServer.ts | 31 +++++++++++++++++++++++++++++++ libs/api/reports/ReportsApi.ts | 3 ++- middleware/auth.ts | 2 +- middleware/serverAvailable.ts | 10 ++++++++++ middleware/serverNotAvailable.ts | 10 ++++++++++ nuxt.config.ts | 1 + pages/login.vue | 12 +++++++++++- pages/register.vue | 1 + pages/serverNotAvailable.vue | 19 +++++++++++++++++-- plugins/2.api.ts | 9 +++++++-- 14 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 components/serverChanger.vue create mode 100644 composables/useServer.ts create mode 100644 middleware/serverAvailable.ts create mode 100644 middleware/serverNotAvailable.ts diff --git a/components/serverChanger.vue b/components/serverChanger.vue new file mode 100644 index 0000000..a2d25a5 --- /dev/null +++ b/components/serverChanger.vue @@ -0,0 +1,31 @@ + + + + + \ No newline at end of file diff --git a/composables/useApi.ts b/composables/useApi.ts index 0924323..42fe16a 100644 --- a/composables/useApi.ts +++ b/composables/useApi.ts @@ -1,3 +1,5 @@ +import {useServer} from "~/composables/useServer"; + export const useApi = function (request : any, opts : any = {}) { const config = useRuntimeConfig() const auth = useNuxtApp().$auth.state(); @@ -7,5 +9,5 @@ export const useApi = function (request : any, opts : any = {}) { opts.headers.Authorization = 'Bearer ' + auth.token; } - return useFetch(request, { baseURL: config.public.apiURL, ...opts }) + return useFetch(request, { baseURL: useServer.getUrl(), ...opts }) } \ No newline at end of file diff --git a/composables/useApiLoader.ts b/composables/useApiLoader.ts index f3b4eca..8bf75f3 100644 --- a/composables/useApiLoader.ts +++ b/composables/useApiLoader.ts @@ -18,6 +18,7 @@ import {AccumulationsApi} from "~/libs/api/accumulations/AccumulationsApi"; import {ReportsApi} from "~/libs/api/reports/ReportsApi"; import {ca} from "date-fns/locale"; import {useStorage} from "@vueuse/core"; +import {useServer} from "~/composables/useServer"; export const useApiLoader = new class ApiLoader { private readonly serverConfigs: ConfigManager; @@ -98,13 +99,10 @@ export const useApiLoader = new class ApiLoader { } public connectWebsocket() : void { - const config = useRuntimeConfig() const auth = useNuxtApp().$auth.state(); - this.websocketClient = new WebSocket(config.public.apiURL - .replace("https://", "wss://") - .replace("http://", "ws://") + - "websockets/events" + this.websocketClient = new WebSocket( + useServer.getWebSocketUrl() + "websockets/events" ); this.websocketClient.onmessage = (event) => { diff --git a/composables/useLazyApi.ts b/composables/useLazyApi.ts index e26e3af..835cb5e 100644 --- a/composables/useLazyApi.ts +++ b/composables/useLazyApi.ts @@ -1,4 +1,5 @@ import {useLazyFetch} from "#app"; +import {useServer} from "~/composables/useServer"; export const useLazyApi = function (request : any, opts : any = {}) { const config = useRuntimeConfig() @@ -9,5 +10,5 @@ export const useLazyApi = function (request : any, opts : any = {}) { opts.headers.authorization = 'Bearer ' + auth.token; } - return useLazyFetch(request, { baseURL: config.public.baseURL, ...opts }) + return useLazyFetch(request, { baseURL: useServer.getUrl(), ...opts }) } \ No newline at end of file diff --git a/composables/useServer.ts b/composables/useServer.ts new file mode 100644 index 0000000..90f7731 --- /dev/null +++ b/composables/useServer.ts @@ -0,0 +1,31 @@ +import {useStorage} from "@vueuse/core"; + +export const useServer = new class FinWaveServer { + public getUrl() : string { + const configValue = this.getConfigUrl(); + + if (this.allowCustomUrl()) + return useStorage("server_url", configValue).value; + + return configValue; + } + + public getWebSocketUrl() : string { + return this.getUrl() + .replace("https://", "wss://") + .replace("http://", "ws://"); + } + + public allowCustomUrl() : boolean { + return useRuntimeConfig().public.allowCustomApiURL; + } + + public getConfigUrl() : string { + return useRuntimeConfig().public.apiURL; + } + + public setUrl(url : string) : void { + if (this.allowCustomUrl()) + useStorage("server_url", "").value = url; + } +} \ No newline at end of file diff --git a/libs/api/reports/ReportsApi.ts b/libs/api/reports/ReportsApi.ts index 8ed7af1..aea6b1a 100644 --- a/libs/api/reports/ReportsApi.ts +++ b/libs/api/reports/ReportsApi.ts @@ -1,6 +1,7 @@ import {AbstractApi} from "~/libs/api/AbstractApi"; import {Ref} from "vue"; import {TransactionsFilter} from "~/libs/api/transactions/TransactionsFilter"; +import {useServer} from "~/composables/useServer"; export class ReportsApi extends AbstractApi { private notifyListeners : Array = new Array(); @@ -54,6 +55,6 @@ export class ReportsApi extends AbstractApi { public getDownloadURL(token: string) : string { const config = useRuntimeConfig(); - return config.public.apiURL + "files/reports/get?token=" + token; + return useServer.getUrl() + "files/reports/get?token=" + token; } } \ No newline at end of file diff --git a/middleware/auth.ts b/middleware/auth.ts index 5df1aef..51b91ff 100644 --- a/middleware/auth.ts +++ b/middleware/auth.ts @@ -3,7 +3,7 @@ import {navigateTo} from "#app"; export default defineNuxtRouteMiddleware((to, from) => { const {$serverConfigs, $auth} = useNuxtApp(); - const serverAvailable = $serverConfigs.serverAvailable; + const serverAvailable = $serverConfigs ? $serverConfigs.serverAvailable : false; if (!serverAvailable) return navigateTo("/serverNotAvailable"); diff --git a/middleware/serverAvailable.ts b/middleware/serverAvailable.ts new file mode 100644 index 0000000..6b8fe84 --- /dev/null +++ b/middleware/serverAvailable.ts @@ -0,0 +1,10 @@ +import {navigateTo} from "#app"; + +export default defineNuxtRouteMiddleware((to, from) => { + const {$serverConfigs} = useNuxtApp(); + + const serverAvailable = $serverConfigs ? $serverConfigs.serverAvailable : false; + + if (!serverAvailable) + return navigateTo("/serverNotAvailable"); +}) \ No newline at end of file diff --git a/middleware/serverNotAvailable.ts b/middleware/serverNotAvailable.ts new file mode 100644 index 0000000..ab2051c --- /dev/null +++ b/middleware/serverNotAvailable.ts @@ -0,0 +1,10 @@ +import {navigateTo} from "#app"; + +export default defineNuxtRouteMiddleware((to, from) => { + const {$serverConfigs} = useNuxtApp(); + + const serverAvailable = $serverConfigs ? $serverConfigs.serverAvailable : false; + + if (serverAvailable) + return navigateTo("/"); +}) \ No newline at end of file diff --git a/nuxt.config.ts b/nuxt.config.ts index c1c314d..1d24c14 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -65,6 +65,7 @@ const config: NuxtConfig = { runtimeConfig: { public: { apiURL: process.env.NUXT_PUBLIC_API_URL || 'http://localhost:8080/', + allowCustomApiURL: process.env.NUXT_ALLOW_CUSTOM_API_URL || false, }, } } diff --git a/pages/login.vue b/pages/login.vue index 6f545d4..3c8c769 100644 --- a/pages/login.vue +++ b/pages/login.vue @@ -1,6 +1,8 @@