config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package config
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. )
  11. //go:embed version
  12. var version string
  13. //go:embed name
  14. var name string
  15. type LogLevel string
  16. const (
  17. Debug LogLevel = "debug"
  18. Info LogLevel = "info"
  19. Notice LogLevel = "notice"
  20. Warn LogLevel = "warn"
  21. Error LogLevel = "error"
  22. )
  23. func GetVersion() string {
  24. return strings.TrimSpace(version)
  25. }
  26. func GetName() string {
  27. return strings.TrimSpace(name)
  28. }
  29. func GetLogLevel() LogLevel {
  30. if IsDebug() {
  31. return Debug
  32. }
  33. logLevel := os.Getenv("XUI_LOG_LEVEL")
  34. if logLevel == "" {
  35. return Info
  36. }
  37. return LogLevel(logLevel)
  38. }
  39. func IsDebug() bool {
  40. return os.Getenv("XUI_DEBUG") == "true"
  41. }
  42. func GetBinFolderPath() string {
  43. binFolderPath := os.Getenv("XUI_BIN_FOLDER")
  44. if binFolderPath == "" {
  45. binFolderPath = "bin"
  46. }
  47. return binFolderPath
  48. }
  49. func getBaseDir() string {
  50. exePath, err := os.Executable()
  51. if err != nil {
  52. return "."
  53. }
  54. exeDir := filepath.Dir(exePath)
  55. exeDirLower := strings.ToLower(filepath.ToSlash(exeDir))
  56. if strings.Contains(exeDirLower, "/appdata/local/temp/") || strings.Contains(exeDirLower, "/go-build") {
  57. wd, err := os.Getwd()
  58. if err != nil {
  59. return "."
  60. }
  61. return wd
  62. }
  63. return exeDir
  64. }
  65. func GetDBFolderPath() string {
  66. dbFolderPath := os.Getenv("XUI_DB_FOLDER")
  67. if dbFolderPath != "" {
  68. return dbFolderPath
  69. }
  70. if runtime.GOOS == "windows" {
  71. return getBaseDir()
  72. }
  73. return "/etc/x-ui"
  74. }
  75. func GetDBPath() string {
  76. return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
  77. }
  78. func GetLogFolder() string {
  79. logFolderPath := os.Getenv("XUI_LOG_FOLDER")
  80. if logFolderPath != "" {
  81. return logFolderPath
  82. }
  83. if runtime.GOOS == "windows" {
  84. return getBaseDir()
  85. }
  86. return "/var/log"
  87. }
  88. func copyFile(src, dst string) error {
  89. in, err := os.Open(src)
  90. if err != nil {
  91. return err
  92. }
  93. defer in.Close()
  94. out, err := os.Create(dst)
  95. if err != nil {
  96. return err
  97. }
  98. defer out.Close()
  99. _, err = io.Copy(out, in)
  100. if err != nil {
  101. return err
  102. }
  103. return out.Sync()
  104. }
  105. func init() {
  106. if runtime.GOOS != "windows" {
  107. return
  108. }
  109. if os.Getenv("XUI_DB_FOLDER") != "" {
  110. return
  111. }
  112. oldDBFolder := "/etc/x-ui"
  113. oldDBPath := fmt.Sprintf("%s/%s.db", oldDBFolder, GetName())
  114. newDBFolder := GetDBFolderPath()
  115. newDBPath := fmt.Sprintf("%s/%s.db", newDBFolder, GetName())
  116. _, err := os.Stat(newDBPath)
  117. if err == nil {
  118. return // new exists
  119. }
  120. _, err = os.Stat(oldDBPath)
  121. if os.IsNotExist(err) {
  122. return // old does not exist
  123. }
  124. _ = copyFile(oldDBPath, newDBPath) // ignore error
  125. }