settings.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "NGINX_LOG": NginxLogSettings,
  33. "NODE": NodeSettings,
  34. "OPENAI": OpenAISettings,
  35. "TERMINAL": TerminalSettings,
  36. "WEBAUTHN": WebAuthnSettings,
  37. "BACKUP": BackupSettings,
  38. }
  39. func init() {
  40. t := time.Unix(cast.ToInt64(buildTime), 0)
  41. LastModified = strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
  42. sections.Set("database", DatabaseSettings)
  43. sections.Set("auth", AuthSettings)
  44. sections.Set("backup", BackupSettings)
  45. sections.Set("casdoor", CasdoorSettings)
  46. sections.Set("cert", CertSettings)
  47. sections.Set("cluster", ClusterSettings)
  48. sections.Set("crypto", CryptoSettings)
  49. sections.Set("http", HTTPSettings)
  50. sections.Set("logrotate", LogrotateSettings)
  51. sections.Set("nginx", NginxSettings)
  52. sections.Set("nginx_log", NginxLogSettings)
  53. sections.Set("node", NodeSettings)
  54. sections.Set("openai", OpenAISettings)
  55. sections.Set("terminal", TerminalSettings)
  56. sections.Set("webauthn", WebAuthnSettings)
  57. for k, v := range sections.AllFromFront() {
  58. settings.Register(k, v)
  59. }
  60. settings.WithoutRedis()
  61. settings.WithoutSonyflake()
  62. }
  63. func Init(confPath string) {
  64. migrate(confPath)
  65. settings.Init(confPath)
  66. // Set Default Port
  67. if settings.ServerSettings.Port == 0 {
  68. settings.ServerSettings.Port = 9000
  69. }
  70. for prefix, ptr := range envPrefixMap {
  71. parseEnv(ptr, prefix+"_")
  72. }
  73. // if in official docker, set the restart cmd of nginx to "nginx -s stop",
  74. // then the supervisor of s6-overlay will start the nginx again.
  75. if helper.InNginxUIOfficialDocker() {
  76. NginxSettings.RestartCmd = "nginx -s stop"
  77. }
  78. if AuthSettings.BanThresholdMinutes <= 0 {
  79. AuthSettings.BanThresholdMinutes = 10
  80. }
  81. if AuthSettings.MaxAttempts <= 0 {
  82. AuthSettings.MaxAttempts = 10
  83. }
  84. }
  85. func Save() (err error) {
  86. // fix unable to save empty slice
  87. if len(CertSettings.RecursiveNameservers) == 0 {
  88. settings.Conf.Section("cert").Key("RecursiveNameservers").SetValue("")
  89. }
  90. err = settings.Save()
  91. if err != nil {
  92. return
  93. }
  94. return
  95. }
  96. func parseEnv(ptr interface{}, prefix string) {
  97. err := env.ParseWithOptions(ptr, env.Options{
  98. Prefix: EnvPrefix + prefix,
  99. UseFieldNameByDefault: true,
  100. })
  101. if err != nil {
  102. log.Fatalf("settings.parseEnv: %v\n", err)
  103. }
  104. }