variable.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package global
  5. import (
  6. "fmt"
  7. resource "github.com/SongZihuan/BackendServerTemplate"
  8. "github.com/SongZihuan/BackendServerTemplate/src/utils/envutils"
  9. "github.com/SongZihuan/BackendServerTemplate/src/utils/timeutils"
  10. "strings"
  11. "time"
  12. )
  13. var (
  14. // Version SemanticVersioning License Report BuildTime GitCommitHash GitTag GitTagCommitHash EnvPrefix 继承自resource(程序init完成后即可调用)
  15. Version = resource.Version
  16. SemanticVersioning = resource.SemanticVersioning
  17. License = resource.License
  18. Report = resource.Report
  19. BuildTime = resource.BuildTime
  20. GitCommitHash = resource.GitCommitHash
  21. GitTag = resource.GitTag
  22. GitTagCommitHash = resource.GitTagCommitHash
  23. EnvPrefix = resource.EnvPrefix
  24. // Name 继承自resource
  25. // 注意:命令行参数或配置文件加载时可能会被更改
  26. Name = resource.Name
  27. NameFlagChanged = false
  28. )
  29. // Location 以下变量需要在配置文件加载完毕后才可调用
  30. var (
  31. UTCLocation = time.UTC
  32. LocalLocation = timeutils.GetLocalTimezone()
  33. Location = time.UTC
  34. )
  35. func init() {
  36. if EnvPrefix == "" {
  37. return
  38. }
  39. newEnvPrefix := envutils.ToEnvName(EnvPrefix)
  40. if EnvPrefix != newEnvPrefix {
  41. panic(fmt.Errorf("bad %s; good %s", EnvPrefix, newEnvPrefix))
  42. } else if strings.HasSuffix(EnvPrefix, "_") {
  43. panic("EnvPrefix End With '_'")
  44. }
  45. }