1
0

settings.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package settings
  2. import (
  3. "log"
  4. "strings"
  5. "time"
  6. "github.com/0xJacky/Nginx-UI/internal/helper"
  7. "github.com/caarlos0/env/v11"
  8. "github.com/elliotchance/orderedmap/v3"
  9. "github.com/spf13/cast"
  10. "github.com/uozi-tech/cosy/settings"
  11. )
  12. var (
  13. buildTime string
  14. LastModified string
  15. EnvPrefix = "NGINX_UI_"
  16. )
  17. var sections = orderedmap.NewOrderedMap[string, any]()
  18. var envPrefixMap = map[string]interface{}{
  19. // Cosy
  20. "APP": settings.AppSettings,
  21. "SERVER": settings.ServerSettings,
  22. // Nginx UI
  23. "DB": DatabaseSettings,
  24. "AUTH": AuthSettings,
  25. "CASDOOR": CasdoorSettings,
  26. "CERT": CertSettings,
  27. "CLUSTER": ClusterSettings,
  28. "CRYPTO": CryptoSettings,
  29. "HTTP": HTTPSettings,
  30. "LOGROTATE": LogrotateSettings,
  31. "NGINX": NginxSettings,
  32. "NODE": NodeSettings,
  33. "OPENAI": OpenAISettings,
  34. "TERMINAL": TerminalSettings,
  35. "WEBAUTHN": WebAuthnSettings,
  36. }
  37. func init() {
  38. t := time.Unix(cast.ToInt64(buildTime), 0)
  39. LastModified = strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
  40. sections.Set("database", DatabaseSettings)
  41. sections.Set("auth", AuthSettings)
  42. sections.Set("casdoor", CasdoorSettings)
  43. sections.Set("cert", CertSettings)
  44. sections.Set("cluster", ClusterSettings)
  45. sections.Set("crypto", CryptoSettings)
  46. sections.Set("http", HTTPSettings)
  47. sections.Set("logrotate", LogrotateSettings)
  48. sections.Set("nginx", NginxSettings)
  49. sections.Set("node", NodeSettings)
  50. sections.Set("openai", OpenAISettings)
  51. sections.Set("terminal", TerminalSettings)
  52. sections.Set("webauthn", WebAuthnSettings)
  53. for k, v := range sections.AllFromFront() {
  54. settings.Register(k, v)
  55. }
  56. settings.WithoutRedis()
  57. settings.WithoutSonyflake()
  58. }
  59. func Init(confPath string) {
  60. migrate(confPath)
  61. settings.Init(confPath)
  62. // Set Default Port
  63. if settings.ServerSettings.Port == 0 {
  64. settings.ServerSettings.Port = 9000
  65. }
  66. for prefix, ptr := range envPrefixMap {
  67. parseEnv(ptr, prefix+"_")
  68. }
  69. // if in official docker, set the restart cmd of nginx to "nginx -s stop",
  70. // then the supervisor of s6-overlay will start the nginx again.
  71. if helper.InNginxUIOfficialDocker() {
  72. NginxSettings.RestartCmd = "nginx -s stop"
  73. }
  74. if AuthSettings.BanThresholdMinutes <= 0 {
  75. AuthSettings.BanThresholdMinutes = 10
  76. }
  77. if AuthSettings.MaxAttempts <= 0 {
  78. AuthSettings.MaxAttempts = 10
  79. }
  80. }
  81. func Save() (err error) {
  82. // fix unable to save empty slice
  83. if len(CertSettings.RecursiveNameservers) == 0 {
  84. settings.Conf.Section("cert").Key("RecursiveNameservers").SetValue("")
  85. }
  86. err = settings.Save()
  87. if err != nil {
  88. return
  89. }
  90. return
  91. }
  92. func parseEnv(ptr interface{}, prefix string) {
  93. err := env.ParseWithOptions(ptr, env.Options{
  94. Prefix: EnvPrefix + prefix,
  95. UseFieldNameByDefault: true,
  96. })
  97. if err != nil {
  98. log.Fatalf("settings.parseEnv: %v\n", err)
  99. }
  100. }