settings.go 2.6 KB

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