1
0

settings.go 2.8 KB

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