settings.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package settings
  2. import (
  3. "github.com/caarlos0/env/v11"
  4. "github.com/spf13/cast"
  5. "gopkg.in/ini.v1"
  6. "log"
  7. "os"
  8. "reflect"
  9. "strings"
  10. "time"
  11. )
  12. var (
  13. buildTime string
  14. LastModified string
  15. Conf *ini.File
  16. ConfPath string
  17. EnvPrefix = "NGINX_UI_"
  18. )
  19. var sections = map[string]interface{}{
  20. "server": &ServerSettings,
  21. "nginx": &NginxSettings,
  22. "openai": &OpenAISettings,
  23. "casdoor": &CasdoorSettings,
  24. "logrotate": &LogrotateSettings,
  25. "cluster": &ClusterSettings,
  26. "auth": &AuthSettings,
  27. "crypto": &CryptoSettings,
  28. }
  29. func init() {
  30. t := time.Unix(cast.ToInt64(buildTime), 0)
  31. LastModified = strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
  32. }
  33. func Init(confPath string) {
  34. ConfPath = confPath
  35. Setup()
  36. }
  37. func load() (err error) {
  38. Conf, err = ini.LoadSources(ini.LoadOptions{
  39. Loose: true,
  40. AllowShadows: true,
  41. }, ConfPath)
  42. return
  43. }
  44. func Setup() {
  45. err := load()
  46. if err != nil {
  47. log.Fatalf("settings.Setup: %v\n", err)
  48. }
  49. MapTo()
  50. parseEnv(&ServerSettings, "SERVER_")
  51. parseEnv(&NginxSettings, "NGINX_")
  52. parseEnv(&OpenAISettings, "OPENAI_")
  53. parseEnv(&CasdoorSettings, "CASDOOR_")
  54. parseEnv(&LogrotateSettings, "LOGROTATE_")
  55. parseEnv(&AuthSettings, "AUTH_")
  56. parseEnv(&CryptoSettings, "CRYPTO_")
  57. // if in official docker, set the restart cmd of nginx to "nginx -s stop",
  58. // then the supervisor of s6-overlay will start the nginx again.
  59. if cast.ToBool(os.Getenv("NGINX_UI_OFFICIAL_DOCKER")) {
  60. NginxSettings.RestartCmd = "nginx -s stop"
  61. }
  62. if AuthSettings.BanThresholdMinutes <= 0 {
  63. AuthSettings.BanThresholdMinutes = 10
  64. }
  65. if AuthSettings.MaxAttempts <= 0 {
  66. AuthSettings.MaxAttempts = 10
  67. }
  68. err = Save()
  69. if err != nil {
  70. log.Fatalf("settings.Setup: %v\n", err)
  71. }
  72. }
  73. func MapTo() {
  74. for k, v := range sections {
  75. err := mapTo(k, v)
  76. if err != nil {
  77. log.Fatalf("Cfg.MapTo %s err: %v", k, err)
  78. }
  79. }
  80. }
  81. func Save() (err error) {
  82. for k, v := range sections {
  83. reflectFrom(k, v)
  84. }
  85. err = Conf.SaveTo(ConfPath)
  86. if err != nil {
  87. return
  88. }
  89. return
  90. }
  91. func ProtectedFill(targetSettings interface{}, newSettings interface{}) {
  92. s := reflect.TypeOf(targetSettings).Elem()
  93. vt := reflect.ValueOf(targetSettings).Elem()
  94. vn := reflect.ValueOf(newSettings).Elem()
  95. // copy the values from new to target settings if it is not protected
  96. for i := 0; i < s.NumField(); i++ {
  97. if s.Field(i).Tag.Get("protected") != "true" {
  98. vt.Field(i).Set(vn.Field(i))
  99. }
  100. }
  101. }
  102. func mapTo(section string, v interface{}) error {
  103. return Conf.Section(section).MapTo(v)
  104. }
  105. func reflectFrom(section string, v interface{}) {
  106. err := Conf.Section(section).ReflectFrom(v)
  107. if err != nil {
  108. log.Fatalf("Cfg.ReflectFrom %s err: %v", section, err)
  109. }
  110. }
  111. func parseEnv(ptr interface{}, prefix string) {
  112. err := env.ParseWithOptions(ptr, env.Options{
  113. Prefix: EnvPrefix + prefix,
  114. UseFieldNameByDefault: true,
  115. })
  116. if err != nil {
  117. log.Fatalf("settings.parseEnv: %v\n", err)
  118. }
  119. }