config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package config
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. //go:embed version
  10. var version string
  11. //go:embed name
  12. var name string
  13. type LogLevel string
  14. const (
  15. Debug LogLevel = "debug"
  16. Info LogLevel = "info"
  17. Notice LogLevel = "notice"
  18. Warn LogLevel = "warn"
  19. Error LogLevel = "error"
  20. )
  21. func GetVersion() string {
  22. return strings.TrimSpace(version)
  23. }
  24. func GetName() string {
  25. return strings.TrimSpace(name)
  26. }
  27. func GetLogLevel() LogLevel {
  28. if IsDebug() {
  29. return Debug
  30. }
  31. logLevel := os.Getenv("XUI_LOG_LEVEL")
  32. if logLevel == "" {
  33. return Info
  34. }
  35. return LogLevel(logLevel)
  36. }
  37. func IsDebug() bool {
  38. return os.Getenv("XUI_DEBUG") == "true"
  39. }
  40. func GetBinFolderPath() string {
  41. binFolderPath := os.Getenv("XUI_BIN_FOLDER")
  42. if binFolderPath == "" {
  43. binFolderPath = "bin"
  44. }
  45. return binFolderPath
  46. }
  47. func GetDBFolderPath() string {
  48. dbFolderPath := os.Getenv("XUI_DB_FOLDER")
  49. if dbFolderPath == "" {
  50. dbFolderPath = "/etc/x-ui"
  51. }
  52. return dbFolderPath
  53. }
  54. // DatabaseConfig holds the database configuration
  55. type DatabaseConfig struct {
  56. Connection string
  57. Host string
  58. Port string
  59. Database string
  60. Username string
  61. Password string
  62. }
  63. // GetDatabaseConfig returns the database configuration from environment variables
  64. func GetDatabaseConfig() (*DatabaseConfig, error) {
  65. config := &DatabaseConfig{
  66. Connection: strings.ToLower(os.Getenv("XUI_DB_CONNECTION")),
  67. Host: os.Getenv("XUI_DB_HOST"),
  68. Port: os.Getenv("XUI_DB_PORT"),
  69. Database: os.Getenv("XUI_DB_DATABASE"),
  70. Username: os.Getenv("XUI_DB_USERNAME"),
  71. Password: os.Getenv("XUI_DB_PASSWORD"),
  72. }
  73. if config.Connection == "mysql" {
  74. if config.Host == "" || config.Database == "" || config.Username == "" {
  75. return nil, fmt.Errorf("missing required MySQL configuration: host, database, and username are required")
  76. }
  77. if config.Port == "" {
  78. config.Port = "3306"
  79. }
  80. }
  81. return config, nil
  82. }
  83. func GetDBPath() string {
  84. config, err := GetDatabaseConfig()
  85. if err != nil {
  86. log.Fatalf("Error getting database config: %v", err)
  87. }
  88. if config.Connection == "mysql" {
  89. return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
  90. config.Username,
  91. config.Password,
  92. config.Host,
  93. config.Port,
  94. config.Database)
  95. }
  96. // Connection is sqlite
  97. return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
  98. }
  99. func GetLogFolder() string {
  100. logFolderPath := os.Getenv("XUI_LOG_FOLDER")
  101. if logFolderPath == "" {
  102. logFolderPath = "/var/log"
  103. }
  104. return logFolderPath
  105. }