fix: config sync issues #685

This commit is contained in:
Jacky 2024-11-04 12:26:49 +08:00
parent b4add42039
commit 21be809ffa
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
10 changed files with 114 additions and 69 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai"
"net/http" "net/http"
"os" "os"
"path/filepath"
) )
type APIConfigResp struct { type APIConfigResp struct {
@ -19,30 +20,30 @@ type APIConfigResp struct {
} }
func GetConfig(c *gin.Context) { func GetConfig(c *gin.Context) {
name := c.Param("name") relativePath := c.Param("path")
path := nginx.GetConfPath("/", name) absPath := nginx.GetConfPath(relativePath)
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) { if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) {
c.JSON(http.StatusForbidden, gin.H{ c.JSON(http.StatusForbidden, gin.H{
"message": "path is not under the nginx conf path", "message": "path is not under the nginx conf path",
}) })
return return
} }
stat, err := os.Stat(path) stat, err := os.Stat(absPath)
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
} }
content, err := os.ReadFile(path) content, err := os.ReadFile(absPath)
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
} }
q := query.Config q := query.Config
g := query.ChatGPTLog g := query.ChatGPTLog
chatgpt, err := g.Where(g.Name.Eq(path)).FirstOrCreate() chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate()
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
@ -52,7 +53,7 @@ func GetConfig(c *gin.Context) {
chatgpt.Content = make([]openai.ChatCompletionMessage, 0) chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
} }
cfg, err := q.Where(q.Filepath.Eq(path)).FirstOrInit() cfg, err := q.Where(q.Filepath.Eq(absPath)).FirstOrInit()
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
@ -63,8 +64,9 @@ func GetConfig(c *gin.Context) {
Name: stat.Name(), Name: stat.Name(),
Content: string(content), Content: string(content),
ChatGPTMessages: chatgpt.Content, ChatGPTMessages: chatgpt.Content,
FilePath: path, FilePath: absPath,
ModifiedAt: stat.ModTime(), ModifiedAt: stat.ModTime(),
Dir: filepath.Dir(relativePath),
}, },
SyncNodeIds: cfg.SyncNodeIds, SyncNodeIds: cfg.SyncNodeIds,
SyncOverwrite: cfg.SyncOverwrite, SyncOverwrite: cfg.SyncOverwrite,

View file

@ -11,6 +11,7 @@ import (
"github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai"
"net/http" "net/http"
"os" "os"
"path/filepath"
"time" "time"
) )
@ -19,11 +20,9 @@ type EditConfigJson struct {
} }
func EditConfig(c *gin.Context) { func EditConfig(c *gin.Context) {
name := c.Param("name") relativePath := c.Param("path")
var json struct { var json struct {
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
Filepath string `json:"filepath" binding:"required"`
NewFilepath string `json:"new_filepath" binding:"required"`
Content string `json:"content"` Content string `json:"content"`
SyncOverwrite bool `json:"sync_overwrite"` SyncOverwrite bool `json:"sync_overwrite"`
SyncNodeIds []uint64 `json:"sync_node_ids"` SyncNodeIds []uint64 `json:"sync_node_ids"`
@ -32,22 +31,8 @@ func EditConfig(c *gin.Context) {
return return
} }
path := json.Filepath absPath := nginx.GetConfPath(relativePath)
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) { if !helper.FileExists(absPath) {
c.JSON(http.StatusForbidden, gin.H{
"message": "filepath is not under the nginx conf path",
})
return
}
if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) {
c.JSON(http.StatusForbidden, gin.H{
"message": "new filepath is not under the nginx conf path",
})
return
}
if !helper.FileExists(path) {
c.JSON(http.StatusNotFound, gin.H{ c.JSON(http.StatusNotFound, gin.H{
"message": "file not found", "message": "file not found",
}) })
@ -55,14 +40,14 @@ func EditConfig(c *gin.Context) {
} }
content := json.Content content := json.Content
origContent, err := os.ReadFile(path) origContent, err := os.ReadFile(absPath)
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
} }
if content != "" && content != string(origContent) { if content != "" && content != string(origContent) {
err = os.WriteFile(path, []byte(content), 0644) err = os.WriteFile(absPath, []byte(content), 0644)
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
@ -70,17 +55,15 @@ func EditConfig(c *gin.Context) {
} }
q := query.Config q := query.Config
cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate() cfg, err := q.Where(q.Filepath.Eq(absPath)).FirstOrCreate()
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
} }
_, err = q.Where(q.Filepath.Eq(json.Filepath)). _, err = q.Where(q.Filepath.Eq(absPath)).
Select(q.Name, q.Filepath, q.SyncNodeIds, q.SyncOverwrite). Select(q.SyncNodeIds, q.SyncOverwrite).
Updates(&model.Config{ Updates(&model.Config{
Name: json.Name,
Filepath: json.NewFilepath,
SyncNodeIds: json.SyncNodeIds, SyncNodeIds: json.SyncNodeIds,
SyncOverwrite: json.SyncOverwrite, SyncOverwrite: json.SyncOverwrite,
}) })
@ -89,27 +72,12 @@ func EditConfig(c *gin.Context) {
return return
} }
// use the new values
cfg.SyncNodeIds = json.SyncNodeIds
cfg.SyncOverwrite = json.SyncOverwrite
g := query.ChatGPTLog g := query.ChatGPTLog
// handle rename err = config.SyncToRemoteServer(cfg, absPath)
if path != json.NewFilepath {
if helper.FileExists(json.NewFilepath) {
c.JSON(http.StatusNotAcceptable, gin.H{
"message": "File exists",
})
return
}
err := os.Rename(json.Filepath, json.NewFilepath)
if err != nil {
api.ErrHandler(c, err)
return
}
// update ChatGPT record
_, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
_, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
}
err = config.SyncToRemoteServer(cfg, json.NewFilepath)
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
@ -123,7 +91,7 @@ func EditConfig(c *gin.Context) {
return return
} }
chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate() chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate()
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
@ -134,10 +102,11 @@ func EditConfig(c *gin.Context) {
} }
c.JSON(http.StatusOK, config.Config{ c.JSON(http.StatusOK, config.Config{
Name: name, Name: filepath.Base(absPath),
Content: content, Content: content,
ChatGPTMessages: chatgpt.Content, ChatGPTMessages: chatgpt.Content,
FilePath: json.NewFilepath, FilePath: absPath,
ModifiedAt: time.Now(), ModifiedAt: time.Now(),
Dir: filepath.Dir(relativePath),
}) })
} }

View file

@ -5,11 +5,13 @@ import (
"github.com/0xJacky/Nginx-UI/internal/config" "github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/helper" "github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx" "github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query" "github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy/logger" "github.com/uozi-tech/cosy/logger"
"net/http" "net/http"
"os" "os"
"strings"
) )
func Rename(c *gin.Context) { func Rename(c *gin.Context) {
@ -77,7 +79,10 @@ func Rename(c *gin.Context) {
_, _ = g.Where(g.Name.Like(origFullPath+"%")).Update(g.Name, g.Name.Replace(origFullPath, newFullPath)) _, _ = g.Where(g.Name.Like(origFullPath+"%")).Update(g.Name, g.Name.Replace(origFullPath, newFullPath))
} }
_, err = q.Where(q.Filepath.Eq(origFullPath)).Update(q.Filepath, newFullPath) _, err = q.Where(q.Filepath.Eq(origFullPath)).Updates(&model.Config{
Filepath: newFullPath,
Name: json.NewName,
})
if err != nil { if err != nil {
api.ErrHandler(c, err) api.ErrHandler(c, err)
return return
@ -92,6 +97,6 @@ func Rename(c *gin.Context) {
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"message": "ok", "path": strings.TrimLeft(newFullPath, nginx.GetConfPath()),
}) })
} }

View file

@ -9,9 +9,9 @@ func InitRouter(r *gin.RouterGroup) {
r.GET("config_base_path", GetBasePath) r.GET("config_base_path", GetBasePath)
r.GET("configs", GetConfigs) r.GET("configs", GetConfigs)
r.GET("configs/*name", GetConfig) r.GET("configs/*path", GetConfig)
r.POST("configs", AddConfig) r.POST("configs", AddConfig)
r.POST("configs/*name", EditConfig) r.POST("configs/*path", EditConfig)
o := r.Group("", middleware.RequireSecureSession()) o := r.Group("", middleware.RequireSecureSession())
{ {

View file

@ -10,6 +10,7 @@ export interface Config {
modified_at: string modified_at: string
sync_node_ids?: number[] sync_node_ids?: number[]
sync_overwrite?: false sync_overwrite?: false
dir: string
} }
class ConfigCurd extends Curd<Config> { class ConfigCurd extends Curd<Config> {
@ -25,7 +26,7 @@ class ConfigCurd extends Curd<Config> {
return http.post('/config_mkdir', { base_path: basePath, folder_name: name }) return http.post('/config_mkdir', { base_path: basePath, folder_name: name })
} }
rename(basePath: string, origName: string, newName: string, syncNodeIds: number[]) { rename(basePath: string, origName: string, newName: string, syncNodeIds?: number[]) {
return http.post('/config_rename', { return http.post('/config_rename', {
base_path: basePath, base_path: basePath,
orig_name: origName, orig_name: origName,

View file

@ -11,13 +11,13 @@ export function syncConfigError(text: string) {
return $gettext('Please upgrade the remote Nginx UI to the latest version') return $gettext('Please upgrade the remote Nginx UI to the latest version')
} }
return $gettext('Sync config %{config_name} to %{env_name} failed, response: %{resp}', { config_name: data.cert_name, env_name: data.env_name, resp: data.resp_body }, true) return $gettext('Sync config %{config_name} to %{env_name} failed, response: %{resp}', { config_name: data.config_name, env_name: data.env_name, resp: data.resp_body }, true)
} }
export function syncRenameConfigSuccess(text: string) { export function syncRenameConfigSuccess(text: string) {
const data = JSON.parse(text) const data = JSON.parse(text)
return $gettext('Rename %{orig_path} to %{new_path} on %{env_name} successfully', { orig_path: data.orig_path, new_path: data.orig_path, env_name: data.env_name }) return $gettext('Rename %{orig_path} to %{new_path} on %{env_name} successfully', { orig_path: data.orig_path, new_path: data.new_path, env_name: data.env_name })
} }
export function syncRenameConfigError(text: string) { export function syncRenameConfigError(text: string) {
@ -27,7 +27,7 @@ export function syncRenameConfigError(text: string) {
return $gettext('Please upgrade the remote Nginx UI to the latest version') return $gettext('Please upgrade the remote Nginx UI to the latest version')
} }
return $gettext('Rename %{orig_path} to %{new_path} on %{env_name} failed, response: %{resp}', { orig_path: data.orig_path, new_path: data.orig_path, resp: data.resp_body, env_name: data.env_name }, true) return $gettext('Rename %{orig_path} to %{new_path} on %{env_name} failed, response: %{resp}', { orig_path: data.orig_path, new_path: data.new_path, resp: data.resp_body, env_name: data.env_name }, true)
} }
export function saveSiteSuccess(text: string) { export function saveSiteSuccess(text: string) {

View file

@ -11,6 +11,7 @@ import NodeSelector from '@/components/NodeSelector/NodeSelector.vue'
import { useBreadcrumbs } from '@/composables/useBreadcrumbs' import { useBreadcrumbs } from '@/composables/useBreadcrumbs'
import { formatDateTime } from '@/lib/helper' import { formatDateTime } from '@/lib/helper'
import { useSettingsStore } from '@/pinia' import { useSettingsStore } from '@/pinia'
import ConfigName from '@/views/config/components/ConfigName.vue'
import InspectConfig from '@/views/config/InspectConfig.vue' import InspectConfig from '@/views/config/InspectConfig.vue'
import { InfoCircleOutlined } from '@ant-design/icons-vue' import { InfoCircleOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
@ -255,7 +256,7 @@ function goBack() {
name="name" name="name"
:label="$gettext('Name')" :label="$gettext('Name')"
> >
<AInput v-model:value="data.name" /> <ConfigName :name="data.name" :dir="data.dir" />
</AFormItem> </AFormItem>
<AFormItem <AFormItem
v-if="!addMode" v-if="!addMode"

View file

@ -7,7 +7,6 @@ import Mkdir from '@/views/config/components/Mkdir.vue'
import Rename from '@/views/config/components/Rename.vue' import Rename from '@/views/config/components/Rename.vue'
import configColumns from '@/views/config/configColumns' import configColumns from '@/views/config/configColumns'
import InspectConfig from '@/views/config/InspectConfig.vue' import InspectConfig from '@/views/config/InspectConfig.vue'
import { $gettext } from '../../gettext'
const table = ref() const table = ref()
const route = useRoute() const route = useRoute()
@ -143,12 +142,12 @@ const refRename = ref()
size="small" size="small"
@click="() => { @click="() => {
if (!record.is_dir) { if (!record.is_dir) {
$router.push({ router.push({
path: `/config/${basePath}${record.name}/edit`, path: `/config/${basePath}${record.name}/edit`,
}) })
} }
else { else {
$router.push({ router.push({
query: { query: {
dir: basePath + record.name, dir: basePath + record.name,
}, },

View file

@ -0,0 +1,67 @@
<script setup lang="ts">
import config from '@/api/config'
import use2FAModal from '@/components/TwoFA/use2FAModal'
import { message } from 'ant-design-vue'
const props = defineProps<{
dir?: string
}>()
const name = defineModel<string>('name', { default: '' })
const router = useRouter()
const modify = ref(false)
const buffer = ref('')
const loading = ref(false)
function clickModify() {
buffer.value = name.value
modify.value = true
}
function save() {
loading.value = true
const otpModal = use2FAModal()
otpModal.open().then(() => {
config.rename(props.dir!, name.value, buffer.value).then(r => {
modify.value = false
message.success($gettext('Renamed successfully'))
router.push({
path: `/config/${r.path}/edit`,
})
}).catch(e => {
message.error($gettext(e?.message ?? 'Server error'))
}).finally(() => {
loading.value = false
})
})
}
</script>
<template>
<div v-if="!modify" class="flex items-center">
<div class="mr-2">
{{ name }}
</div>
<div>
<AButton type="link" size="small" @click="clickModify">
{{ $gettext('Rename') }}
</AButton>
</div>
</div>
<div v-else>
<AInput v-model:value="buffer">
<template #suffix>
<AButton :disabled="buffer === name" type="link" size="small" :loading @click="save">
{{ $gettext('Save') }}
</AButton>
</template>
</AInput>
</div>
</template>
<style scoped lang="less">
</style>

View file

@ -17,4 +17,5 @@ type Config struct {
SiteCategoryID uint64 `json:"site_category_id"` SiteCategoryID uint64 `json:"site_category_id"`
SiteCategory *model.SiteCategory `json:"site_category,omitempty"` SiteCategory *model.SiteCategory `json:"site_category,omitempty"`
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
Dir string `json:"dir"`
} }