Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

startup_config.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package models
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // StartupConfig 启动配置表
  11. type StartupConfig struct {
  12. ID int64 `json:"id" db:"id"` // 技术主键
  13. ServiceName string `json:"service_name" db:"service_name"` // 服务名称
  14. Environment string `json:"environment" db:"environment"` // 环境:dev/test/prod
  15. Version int `json:"version" db:"version"` // 配置版本
  16. ConfigJSON json.RawMessage `json:"config_json" db:"config_json"` // 配置内容JSON
  17. IsActive bool `json:"is_active" db:"is_active"` // 是否生效
  18. Creator string `json:"creator" db:"creator"` // 创建人
  19. CreatedAt time.Time `json:"created_at" db:"created_at"` // 创建时间
  20. UpdatedAt sql.NullTime `json:"updated_at,omitempty" db:"updated_at"` // 更新时间
  21. }
  22. // TableName 返回表名
  23. func (StartupConfig) TableName() string {
  24. return "startup_config"
  25. }
  26. // ConfigKey 返回业务键:service_name.environment.vversion
  27. func (c *StartupConfig) ConfigKey() string {
  28. return fmt.Sprintf("%s.%s.v%d", c.ServiceName, c.Environment, c.Version)
  29. }
  30. // ParseConfigKey 解析业务键
  31. func ParseConfigKey(key string) (service, env string, version int, ok bool) {
  32. parts := strings.Split(key, ".")
  33. if len(parts) != 3 {
  34. return "", "", 0, false
  35. }
  36. // 检查版本格式
  37. if !strings.HasPrefix(parts[2], "v") {
  38. return "", "", 0, false
  39. }
  40. verStr := strings.TrimPrefix(parts[2], "v")
  41. version, err := strconv.Atoi(verStr)
  42. if err != nil {
  43. return "", "", 0, false
  44. }
  45. return parts[0], parts[1], version, true
  46. }