diff --git a/api/setting.go b/api/setting.go new file mode 100644 index 0000000..f0ee074 --- /dev/null +++ b/api/setting.go @@ -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) +} diff --git a/config/config.go b/config/config.go index d277922..afc0b14 100644 --- a/config/config.go +++ b/config/config.go @@ -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) }