helpers.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package applicationconfig
  2. import (
  3. "database/sql"
  4. "strings"
  5. "time"
  6. "udeServer/internal/apierr"
  7. )
  8. // editFlag 语义与 t_config.edit_flag 一致(移植自 sdf-serve constants):
  9. // 1=默认配置,禁止修改配置项名与删除;2=允许编辑。
  10. const (
  11. editFlagProhibited = 1
  12. editFlagAllowEdit = 2
  13. )
  14. // configOperatorName 无用户体系时的操作人占位,与其他模块一致。
  15. const configOperatorName = "admin"
  16. // nullStr 非空字符串转合法 NullString,空串置 NULL。
  17. func nullStr(v string) sql.NullString {
  18. return sql.NullString{String: v, Valid: v != ""}
  19. }
  20. // trimSpace 去首尾空白。
  21. func trimSpace(v string) string {
  22. return strings.TrimSpace(v)
  23. }
  24. // normalizePage 页码/页大小兜底:1/10 起,上限 100。
  25. func normalizePage(page, pageSize int64) (int64, int64) {
  26. if page < 1 {
  27. page = 1
  28. }
  29. if pageSize < 10 {
  30. pageSize = 10
  31. }
  32. if pageSize > 100 {
  33. pageSize = 100
  34. }
  35. return page, pageSize
  36. }
  37. // checkId 主键合法性,与 logic 包同名 helper 行为一致。
  38. func checkId(id int64) error {
  39. if id <= 0 {
  40. return apierr.BadRequest("id must be greater than 0")
  41. }
  42. return nil
  43. }
  44. // formatConfigTime 时间展示格式与 sdf-serve 保持一致。
  45. func formatConfigTime(t time.Time) string {
  46. if t.IsZero() {
  47. return ""
  48. }
  49. return t.Format("2006-01-02 15:04:05")
  50. }
  51. // containsConfigKey 判断配置项是否在热加载白名单内(sdf ReloadableKeys 同款)。
  52. func containsConfigKey(slice []string, item string) bool {
  53. for _, v := range slice {
  54. if v == item {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. // reloadableKeys 修改后需要重载 MinIO 客户端与审计日志配置的键,
  61. // 与 audit 包读取的 config_item 完全对应。
  62. var reloadableKeys = []string{
  63. "file_endpoint",
  64. "file_bucket",
  65. "file_access_key",
  66. "file_secret_key",
  67. "file_use_ssl",
  68. "log_record_level",
  69. "archive_threshold",
  70. }