settings.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. err = Save()
  61. if err != nil {
  62. log.Fatalf("settings.Setup: %v\n", err)
  63. }
  64. }
  65. func MapTo() {
  66. for k, v := range sections {
  67. err := mapTo(k, v)
  68. if err != nil {
  69. log.Fatalf("Cfg.MapTo %s err: %v", k, err)
  70. }
  71. }
  72. }
  73. func Save() (err error) {
  74. for k, v := range sections {
  75. reflectFrom(k, v)
  76. }
  77. err = Conf.SaveTo(ConfPath)
  78. if err != nil {
  79. return
  80. }
  81. return
  82. }
  83. func ProtectedFill(targetSettings interface{}, newSettings interface{}) {
  84. s := reflect.TypeOf(targetSettings).Elem()
  85. vt := reflect.ValueOf(targetSettings).Elem()
  86. vn := reflect.ValueOf(newSettings).Elem()
  87. // copy the values from new to target settings if it is not protected
  88. for i := 0; i < s.NumField(); i++ {
  89. if s.Field(i).Tag.Get("protected") != "true" {
  90. vt.Field(i).Set(vn.Field(i))
  91. }
  92. }
  93. }
  94. func mapTo(section string, v interface{}) error {
  95. return Conf.Section(section).MapTo(v)
  96. }
  97. func reflectFrom(section string, v interface{}) {
  98. err := Conf.Section(section).ReflectFrom(v)
  99. if err != nil {
  100. log.Fatalf("Cfg.ReflectFrom %s err: %v", section, err)
  101. }
  102. }
  103. func parseEnv(ptr interface{}, prefix string) {
  104. err := env.ParseWithOptions(ptr, env.Options{
  105. Prefix: EnvPrefix + prefix,
  106. UseFieldNameByDefault: true,
  107. })
  108. if err != nil {
  109. log.Fatalf("settings.parseEnv: %v\n", err)
  110. }
  111. }