settings.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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("auth", AuthSettings)
  41. sections.Set("casdoor", CasdoorSettings)
  42. sections.Set("cert", CertSettings)
  43. sections.Set("cluster", ClusterSettings)
  44. sections.Set("crypto", CryptoSettings)
  45. sections.Set("http", HTTPSettings)
  46. sections.Set("logrotate", LogrotateSettings)
  47. sections.Set("nginx", NginxSettings)
  48. sections.Set("node", NodeSettings)
  49. sections.Set("openai", OpenAISettings)
  50. sections.Set("terminal", TerminalSettings)
  51. sections.Set("webauthn", WebAuthnSettings)
  52. for k, v := range sections.Iterator() {
  53. settings.Register(k, v)
  54. }
  55. settings.WithoutRedis()
  56. settings.WithoutSonyflake()
  57. }
  58. func Init() {
  59. for prefix, ptr := range envPrefixMap {
  60. parseEnv(ptr, prefix+"_")
  61. }
  62. // if in official docker, set the restart cmd of nginx to "nginx -s stop",
  63. // then the supervisor of s6-overlay will start the nginx again.
  64. if cast.ToBool(os.Getenv("NGINX_UI_OFFICIAL_DOCKER")) {
  65. NginxSettings.RestartCmd = "nginx -s stop"
  66. }
  67. if AuthSettings.BanThresholdMinutes <= 0 {
  68. AuthSettings.BanThresholdMinutes = 10
  69. }
  70. if AuthSettings.MaxAttempts <= 0 {
  71. AuthSettings.MaxAttempts = 10
  72. }
  73. }
  74. func Save() (err error) {
  75. // fix unable to save empty slice
  76. if len(CertSettings.RecursiveNameservers) == 0 {
  77. settings.Conf.Section("server").Key("RecursiveNameservers").SetValue("")
  78. }
  79. err = settings.Save()
  80. if err != nil {
  81. return
  82. }
  83. return
  84. }
  85. func parseEnv(ptr interface{}, prefix string) {
  86. err := env.ParseWithOptions(ptr, env.Options{
  87. Prefix: EnvPrefix + prefix,
  88. UseFieldNameByDefault: true,
  89. })
  90. if err != nil {
  91. log.Fatalf("settings.parseEnv: %v\n", err)
  92. }
  93. }