settings.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package settings
  2. import (
  3. "github.com/caarlos0/env/v11"
  4. "github.com/elliotchance/orderedmap/v2"
  5. "github.com/spf13/cast"
  6. "github.com/uozi-tech/cosy/settings"
  7. "log"
  8. "os"
  9. "strings"
  10. "time"
  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.Iterator() {
  54. settings.Register(k, v)
  55. }
  56. settings.WithoutRedis()
  57. settings.WithoutSonyflake()
  58. }
  59. func Init() {
  60. for prefix, ptr := range envPrefixMap {
  61. parseEnv(ptr, prefix+"_")
  62. }
  63. // if in official docker, set the restart cmd of nginx to "nginx -s stop",
  64. // then the supervisor of s6-overlay will start the nginx again.
  65. if cast.ToBool(os.Getenv("NGINX_UI_OFFICIAL_DOCKER")) {
  66. NginxSettings.RestartCmd = "nginx -s stop"
  67. }
  68. if AuthSettings.BanThresholdMinutes <= 0 {
  69. AuthSettings.BanThresholdMinutes = 10
  70. }
  71. if AuthSettings.MaxAttempts <= 0 {
  72. AuthSettings.MaxAttempts = 10
  73. }
  74. }
  75. func Save() (err error) {
  76. // fix unable to save empty slice
  77. if len(CertSettings.RecursiveNameservers) == 0 {
  78. settings.Conf.Section("cert").Key("RecursiveNameservers").SetValue("")
  79. }
  80. err = settings.Save()
  81. if err != nil {
  82. return
  83. }
  84. return
  85. }
  86. func parseEnv(ptr interface{}, prefix string) {
  87. err := env.ParseWithOptions(ptr, env.Options{
  88. Prefix: EnvPrefix + prefix,
  89. UseFieldNameByDefault: true,
  90. })
  91. if err != nil {
  92. log.Fatalf("settings.parseEnv: %v\n", err)
  93. }
  94. }