settings.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "webauthn": &WebAuthnSettings,
  29. }
  30. func init() {
  31. t := time.Unix(cast.ToInt64(buildTime), 0)
  32. LastModified = strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
  33. }
  34. func Init(confPath string) {
  35. ConfPath = confPath
  36. Setup()
  37. }
  38. func load() (err error) {
  39. Conf, err = ini.LoadSources(ini.LoadOptions{
  40. Loose: true,
  41. AllowShadows: true,
  42. }, ConfPath)
  43. return
  44. }
  45. func Setup() {
  46. err := load()
  47. if err != nil {
  48. log.Fatalf("settings.Setup: %v\n", err)
  49. }
  50. MapTo()
  51. parseEnv(&ServerSettings, "SERVER_")
  52. parseEnv(&NginxSettings, "NGINX_")
  53. parseEnv(&OpenAISettings, "OPENAI_")
  54. parseEnv(&CasdoorSettings, "CASDOOR_")
  55. parseEnv(&LogrotateSettings, "LOGROTATE_")
  56. parseEnv(&AuthSettings, "AUTH_")
  57. parseEnv(&CryptoSettings, "CRYPTO_")
  58. parseEnv(&WebAuthnSettings, "WEBAUTHN_")
  59. // if in official docker, set the restart cmd of nginx to "nginx -s stop",
  60. // then the supervisor of s6-overlay will start the nginx again.
  61. if cast.ToBool(os.Getenv("NGINX_UI_OFFICIAL_DOCKER")) {
  62. NginxSettings.RestartCmd = "nginx -s stop"
  63. }
  64. if AuthSettings.BanThresholdMinutes <= 0 {
  65. AuthSettings.BanThresholdMinutes = 10
  66. }
  67. if AuthSettings.MaxAttempts <= 0 {
  68. AuthSettings.MaxAttempts = 10
  69. }
  70. err = Save()
  71. if err != nil {
  72. log.Fatalf("settings.Setup: %v\n", err)
  73. }
  74. }
  75. func MapTo() {
  76. for k, v := range sections {
  77. err := mapTo(k, v)
  78. if err != nil {
  79. log.Fatalf("Cfg.MapTo %s err: %v", k, err)
  80. }
  81. }
  82. }
  83. func Save() (err error) {
  84. for k, v := range sections {
  85. reflectFrom(k, v)
  86. }
  87. // fix unable to save empty slice
  88. if len(ServerSettings.RecursiveNameservers) == 0 {
  89. Conf.Section("server").Key("RecursiveNameservers").SetValue("")
  90. }
  91. err = Conf.SaveTo(ConfPath)
  92. if err != nil {
  93. return
  94. }
  95. return
  96. }
  97. func ProtectedFill(targetSettings interface{}, newSettings interface{}) {
  98. s := reflect.TypeOf(targetSettings).Elem()
  99. vt := reflect.ValueOf(targetSettings).Elem()
  100. vn := reflect.ValueOf(newSettings).Elem()
  101. // copy the values from new to target settings if it is not protected
  102. for i := 0; i < s.NumField(); i++ {
  103. if s.Field(i).Tag.Get("protected") != "true" {
  104. vt.Field(i).Set(vn.Field(i))
  105. }
  106. }
  107. }
  108. func mapTo(section string, v interface{}) error {
  109. return Conf.Section(section).MapTo(v)
  110. }
  111. func reflectFrom(section string, v interface{}) {
  112. err := Conf.Section(section).ReflectFrom(v)
  113. if err != nil {
  114. log.Fatalf("Cfg.ReflectFrom %s err: %v", section, err)
  115. }
  116. }
  117. func parseEnv(ptr interface{}, prefix string) {
  118. err := env.ParseWithOptions(ptr, env.Options{
  119. Prefix: EnvPrefix + prefix,
  120. UseFieldNameByDefault: true,
  121. })
  122. if err != nil {
  123. log.Fatalf("settings.parseEnv: %v\n", err)
  124. }
  125. }