config.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package config
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. //go:embed version
  9. var version string
  10. //go:embed name
  11. var name string
  12. type LogLevel string
  13. const (
  14. Debug LogLevel = "debug"
  15. Info LogLevel = "info"
  16. Notice LogLevel = "notice"
  17. Warn LogLevel = "warn"
  18. Error LogLevel = "error"
  19. )
  20. func GetVersion() string {
  21. return strings.TrimSpace(version)
  22. }
  23. func GetName() string {
  24. return strings.TrimSpace(name)
  25. }
  26. func GetLogLevel() LogLevel {
  27. if IsDebug() {
  28. return Debug
  29. }
  30. logLevel := os.Getenv("XUI_LOG_LEVEL")
  31. if logLevel == "" {
  32. return Info
  33. }
  34. return LogLevel(logLevel)
  35. }
  36. func IsDebug() bool {
  37. return os.Getenv("XUI_DEBUG") == "true"
  38. }
  39. func GetBinFolderPath() string {
  40. binFolderPath := os.Getenv("XUI_BIN_FOLDER")
  41. if binFolderPath == "" {
  42. binFolderPath = "bin"
  43. }
  44. return binFolderPath
  45. }
  46. func GetDBFolderPath() string {
  47. dbFolderPath := os.Getenv("XUI_DB_FOLDER")
  48. if dbFolderPath == "" {
  49. dbFolderPath = "/etc/x-ui"
  50. }
  51. return dbFolderPath
  52. }
  53. func GetDBPath() string {
  54. return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
  55. }
  56. func GetLogFolder() string {
  57. logFolderPath := os.Getenv("XUI_LOG_FOLDER")
  58. if logFolderPath == "" {
  59. logFolderPath = "/var/log"
  60. }
  61. return logFolderPath
  62. }