configs.go 740 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package models
  2. var CustomConfigs []Config
  3. type Config struct {
  4. ID uint `gorm:"primaryKey" json:"id"`
  5. ConfName string `gorm:"varchar(255)" json:"conf_name"`
  6. ConfKey string `gorm:"varchar(255) index:uidx_conf_key,unique" json:"conf_key"`
  7. ConfValue string `json:"conf_value"`
  8. }
  9. func UpdateConfig(key string, value string) {
  10. c := &Config{
  11. ConfValue: value,
  12. }
  13. DB.Model(c).Where("conf_key = ?", key).Updates(c)
  14. InitConfig()
  15. }
  16. func FindConfigs() []Config {
  17. var config []Config
  18. DB.Find(&config)
  19. return config
  20. }
  21. func InitConfig() {
  22. CustomConfigs = FindConfigs()
  23. }
  24. func FindConfig(key string) string {
  25. for _, config := range CustomConfigs {
  26. if key == config.ConfKey {
  27. return config.ConfValue
  28. }
  29. }
  30. return ""
  31. }