Skip to content

Commit e0366f9

Browse files
committed
feat: reload cluster node settings from settings file #169
1 parent 29f1b7d commit e0366f9

File tree

13 files changed

+110
-34
lines changed

13 files changed

+110
-34
lines changed

api/cluster/environment.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package cluster
33
import (
44
"github.com/0xJacky/Nginx-UI/api"
55
"github.com/0xJacky/Nginx-UI/internal/analytic"
6+
"github.com/0xJacky/Nginx-UI/internal/cluster"
67
"github.com/0xJacky/Nginx-UI/internal/cosy"
78
"github.com/0xJacky/Nginx-UI/model"
89
"github.com/0xJacky/Nginx-UI/query"
10+
"github.com/0xJacky/Nginx-UI/settings"
911
"github.com/gin-gonic/gin"
1012
"github.com/spf13/cast"
1113
"net/http"
@@ -75,3 +77,19 @@ func DeleteEnvironment(c *gin.Context) {
7577

7678
c.JSON(http.StatusNoContent, nil)
7779
}
80+
81+
func LoadEnvironmentFromSettings(c *gin.Context) {
82+
err := settings.ReloadCluster()
83+
if err != nil {
84+
api.ErrHandler(c, err)
85+
return
86+
}
87+
88+
cluster.RegisterPredefinedNodes()
89+
90+
go analytic.RestartRetrieveNodesStatus()
91+
92+
c.JSON(http.StatusOK, gin.H{
93+
"message": "ok",
94+
})
95+
}

api/cluster/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "github.com/gin-gonic/gin"
55
func InitRouter(r *gin.RouterGroup) {
66
// Environment
77
r.GET("environments", GetEnvironmentList)
8+
r.POST("environments/load_from_settings", LoadEnvironmentFromSettings)
89
envGroup := r.Group("environment")
910
{
1011
envGroup.GET("/:id", GetEnvironment)

app/src/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import en_US from 'ant-design-vue/es/locale/en_US'
1010
1111
import { useSettingsStore } from '@/pinia'
1212
import gettext from '@/gettext'
13+
import loadTranslations from '@/api/translations'
1314
1415
const media = window.matchMedia('(prefers-color-scheme: dark)')
1516
@@ -49,6 +50,8 @@ const lang = computed(() => {
4950
5051
const settings = useSettingsStore()
5152
const is_theme_dark = computed(() => settings.theme === 'dark')
53+
54+
loadTranslations()
5255
</script>
5356

5457
<template>

app/src/api/environment.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import http from '@/lib/http'
12
import type { ModelBase } from '@/api/curd'
23
import Curd from '@/api/curd'
34

@@ -14,6 +15,17 @@ export interface Node {
1415
token: string
1516
response_at?: Date
1617
}
17-
const environment: Curd<Environment> = new Curd('/environment')
18+
19+
class EnvironmentCurd extends Curd<Environment> {
20+
constructor() {
21+
super('/environment')
22+
}
23+
24+
load_from_settings() {
25+
return http.post(`${this.plural}/load_from_settings`)
26+
}
27+
}
28+
29+
const environment: EnvironmentCurd = new EnvironmentCurd()
1830

1931
export default environment

app/src/api/translations.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import gettext from '@/gettext'
2+
3+
export default async function loadTranslations() {
4+
const route = useRoute()
5+
6+
if (gettext.current !== 'en') {
7+
await fetch(`${import.meta.env.VITE_API_ROOT}/translation/${gettext.current}`).then(async r => {
8+
gettext.translations[gettext.current] = await r.json()
9+
})
10+
11+
if (route?.meta?.name)
12+
document.title = `${route.meta.name?.()} | Nginx UI`
13+
}
14+
15+
watch(route, () => {
16+
if (route?.meta?.name)
17+
document.title = `${route.meta.name?.()} | Nginx UI`
18+
})
19+
}

app/src/components/SetLanguage/SetLanguage.vue

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
<script setup lang="ts">
2-
import { ref, watch } from 'vue'
2+
import { watch } from 'vue'
33
44
import { useSettingsStore } from '@/pinia'
5-
import http from '@/lib/http'
65
import gettext from '@/gettext'
6+
import loadTranslations from '@/api/translations'
77
88
const settings = useSettingsStore()
99
1010
const route = useRoute()
1111
12-
const current = ref(gettext.current)
12+
const current = computed({
13+
get() {
14+
return gettext.current
15+
},
16+
set(v) {
17+
gettext.current = v
18+
},
19+
})
1320
1421
const languageAvailable = gettext.available
1522
16-
async function init() {
17-
if (current.value !== 'en') {
18-
await http.get(`/translation/${current.value}`).then(r => {
19-
gettext.translations[current.value] = r
20-
})
21-
22-
document.title = `${route.meta.name?.()} | Nginx UI`
23-
}
24-
}
25-
26-
init()
27-
2823
watch(current, v => {
29-
init()
24+
loadTranslations()
3025
settings.set_language(v)
3126
gettext.current = v
3227
3328
const name = route.meta.name as never as () => string
3429
3530
document.title = `${name()} | Nginx UI`
3631
})
37-
3832
</script>
3933

4034
<template>

app/src/views/environment/Environment.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="tsx">
22
import { h } from 'vue'
3-
import { Badge, Tag } from 'ant-design-vue'
3+
import { Badge, Tag, message } from 'ant-design-vue'
44
import type { customRender } from '@/components/StdDesign/StdDataDisplay/StdTableTransformer'
55
import { datetime } from '@/components/StdDesign/StdDataDisplay/StdTableTransformer'
66
import environment from '@/api/environment'
@@ -130,14 +130,28 @@ const columns: Column[] = [{
130130
dataIndex: 'action',
131131
}]
132132
133+
const curd = ref()
134+
function load_from_settings() {
135+
environment.load_from_settings().then(() => {
136+
curd.value.get_list()
137+
message.success($gettext('Load successfully'))
138+
}).catch(e => {
139+
message.error(`${$gettext('Server error')} ${e?.message}`)
140+
})
141+
}
133142
</script>
134143

135144
<template>
136145
<StdCurd
146+
ref="curd"
137147
:title="$gettext('Environment')"
138148
:api="environment"
139149
:columns="columns"
140-
/>
150+
>
151+
<template #extra>
152+
<a @click="load_from_settings">{{ $gettext('Load from settings') }}</a>
153+
</template>
154+
</StdCurd>
141155
</template>
142156

143157
<style lang="less" scoped>

app/src/views/other/Error.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const route = useRoute()
55
const info = computed(() => {
66
if (typeof route.meta.error === 'function')
77
return route.meta.error()
8-
else if (typeof route.meta.error === 'string')
9-
return route.meta.error
108
else
119
return $gettext('File Not Found')
1210
})

internal/kernal/cluster.go renamed to internal/cluster/cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kernal
1+
package cluster
22

33
import (
44
"github.com/0xJacky/Nginx-UI/internal/logger"
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111
)
1212

13-
func registerPredefinedClusterNodes() {
13+
func RegisterPredefinedNodes() {
1414
if len(settings.ClusterSettings.Node) == 0 {
1515
return
1616
}

internal/kernal/cluster_test.go renamed to internal/cluster/cluster_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kernal
1+
package cluster
22

33
import (
44
"github.com/0xJacky/Nginx-UI/settings"

0 commit comments

Comments
 (0)