settings.go 2.8 KB

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