config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package audit
  2. import (
  3. "context"
  4. "strconv"
  5. "udeServer/internal/model"
  6. )
  7. // FileConfigManager holds the MinIO connection settings loaded from t_config.
  8. type FileConfigManager struct {
  9. endpoint string
  10. bucket string
  11. accessKey string
  12. secretKey string
  13. useSSL bool
  14. }
  15. // LogConfigManager holds the audit logging settings loaded from t_config.
  16. type LogConfigManager struct {
  17. LogRecordLevel string
  18. ArchiveThreshold int64
  19. HmacSecret string
  20. }
  21. // Reload reads the whole t_config table and extracts the audit-related
  22. // settings. It never fails hard: a missing table or empty rows simply
  23. // yields zero-valued managers (the callers degrade gracefully).
  24. func Reload(configModel model.TConfigModel) (fileConfig FileConfigManager, logConfig LogConfigManager) {
  25. configList, err := configModel.FindAll(context.Background())
  26. if err != nil {
  27. return
  28. }
  29. var useSSLStr string
  30. handlers := map[string]func(string){
  31. ItemFileEndpoint: func(v string) { fileConfig.endpoint = v },
  32. ItemFileBucket: func(v string) { fileConfig.bucket = v },
  33. ItemFileAccessKey: func(v string) { fileConfig.accessKey = v },
  34. ItemFileSecretKey: func(v string) { fileConfig.secretKey = v },
  35. ItemFileUseSsl: func(v string) { useSSLStr = v },
  36. ItemLogRecordLevel: func(v string) { logConfig.LogRecordLevel = v },
  37. ItemArchiveThresh: func(v string) {
  38. count, _ := strconv.ParseInt(v, 10, 64)
  39. logConfig.ArchiveThreshold = count
  40. },
  41. ItemLogHmacSecret: func(v string) { logConfig.HmacSecret = v },
  42. }
  43. for _, v := range configList {
  44. if handler, ok := handlers[v.ConfigItem]; ok {
  45. handler(v.ConfigValue)
  46. }
  47. }
  48. fileConfig.useSSL = useSSLStr == "1"
  49. return
  50. }
  51. // GetGlobalConfig reloads the dynamic configuration and (re)builds the
  52. // global MinIO client when needed. It returns the currently loaded client
  53. // (nil when configuration is incomplete/failed) and the log settings.
  54. func GetGlobalConfig(configModel model.TConfigModel) (*MinioClient, *LogConfigManager) {
  55. fileConfig, logConfig := Reload(configModel)
  56. ReloadMinioClient(fileConfig)
  57. return GetMinioClient(), &LogConfigManager{
  58. LogRecordLevel: logConfig.LogRecordLevel,
  59. ArchiveThreshold: logConfig.ArchiveThreshold,
  60. HmacSecret: logConfig.HmacSecret,
  61. }
  62. }