Skip to content

Commit

Permalink
加入setting
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Jul 8, 2024
1 parent 879fa2c commit 43b640a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
91 changes: 91 additions & 0 deletions api/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package api

import (
"github.com/gin-gonic/gin"
"github.com/god-jason/bucket/config"
"github.com/god-jason/bucket/setting"
"github.com/spf13/viper"
)

// @Summary 查询配置
// @Schemes
// @Description 查询配置
// @Tags setting
// @Param module path string true "模块,web database log ..."
// @Accept json
// @Produce json
// @Success 200 {object} curd.ReplyData[map[string]any] 返回配置
// @Router /setting/:module [get]
func settingGet(ctx *gin.Context) {
OK(ctx, viper.GetStringMap(ctx.Param("module")))
}

// @Summary 修改配置
// @Schemes
// @Description 修改配置
// @Tags setting
// @Param module path string true "模块,web database log ..."
// @Param cfg body map[string]any true "配置内容"
// @Accept json
// @Produce json
// @Success 200 {object} curd.ReplyData[int]
// @Router /setting/:module [post]
func settingSet(ctx *gin.Context) {
m := ctx.Param("module")

var conf map[string]any
err := ctx.ShouldBindJSON(&conf)
if err != nil {
Error(ctx, err)
return
}
for k, v := range conf {
viper.Set(m+"."+k, v)
}

err = config.Store()
if err != nil {
Error(ctx, err)
return
}
OK(ctx, nil)
}

// @Summary 查询配置表单
// @Schemes
// @Description 查询配置表单
// @Tags setting
// @Param module path string true "模块,web database log ..."
// @Accept json
// @Produce json
// @Success 200 {object} curd.ReplyData[Module] 返回配置表单
// @Router /setting/:module/form [get]
func settingForm(ctx *gin.Context) {
m := ctx.Param("module")
md := setting.Load(m)
if md == nil {
Fail(ctx, "模块不存在")
return
}
OK(ctx, md.Form)
}

// @Summary 查询所有配置
// @Schemes
// @Description 查询所有配置
// @Tags setting
// @Accept json
// @Produce json
// @Success 200 {object} curd.ReplyData[[]Module] 返回配置表单
// @Router /setting/modules [get]
func settingModules(ctx *gin.Context) {
ms := setting.Modules()
OK(ctx, ms)
}

func init() {
Register("POST", "setting/:module", settingSet)
Register("GET", "setting/:module", settingGet)
Register("GET", "setting/:module/form", settingForm)
Register("GET", "setting/modules", settingModules)
}
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ func GetFloat(module string, key string) float64 {

func GetStringSlice(module string, key string) []string {
return viper.GetStringSlice(module + "." + key)
}

func Set(module string, key string, value any) {
viper.Set(module+"."+key, value)
}

0 comments on commit 43b640a

Please sign in to comment.