package service import ( "encoding/json" "gorm.io/gorm" gdb "wireguard-ui/global/client" "wireguard-ui/model" "wireguard-ui/template/render_data" ) type setting struct{ *gorm.DB } func Setting() setting { return setting{gdb.DB} } func (s setting) SetData(data *model.Setting) error { // 判断code是否已经存在 var count int64 if err := s.Model(&model.Setting{}).Where("code = ?", data.Code).Count(&count).Error; err != nil { return err } // 存在就更新,反之新增 if count > 0 { return s.Save(&data).Error } return s.Create(&data).Error } // GetWGSetForConfig // @description: 获取全局配置 // @receiver s // @return data // @return err func (s setting) GetWGSetForConfig() (data *render_data.ServerSetting, err error) { var rs *model.Setting if err = s.Model(&model.Setting{}).Where("code = ?", "WG_SETTING").Take(&rs).Error; err != nil { return } if err = json.Unmarshal([]byte(rs.Data), &data); err != nil { return } return } // GetWGServerForConfig // @description: 获取wireguard服务端配置为了渲染数据 // @receiver s // @return render_data // @return err func (s setting) GetWGServerForConfig() (data *render_data.Server, err error) { var rs *model.Setting if err = s.Model(&model.Setting{}).Where("code = ?", "WG_SERVER").Take(&rs).Error; err != nil { return } if err = json.Unmarshal([]byte(rs.Data), &data); err != nil { return } // 获取一下全局的服务配置 gs, err := s.GetWGSetForConfig() if err != nil { return } data.MTU = gs.MTU data.Table = gs.Table return }