Welcome to mirror list, hosted at ThFree Co, Russian Federation.

config.go « config - github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 942de7fbc1f0c53dcfc0614a100e99b53ea4193a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package config

import (
	_ "embed"
	"fmt"
	"log"
	"os"
	"strings"
)

//go:embed version
var version string

//go:embed name
var name string

type LogLevel string

const (
	Debug  LogLevel = "debug"
	Info   LogLevel = "info"
	Notice LogLevel = "notice"
	Warn   LogLevel = "warn"
	Error  LogLevel = "error"
)

func GetVersion() string {
	return strings.TrimSpace(version)
}

func GetName() string {
	return strings.TrimSpace(name)
}

func GetLogLevel() LogLevel {
	if IsDebug() {
		return Debug
	}
	logLevel := os.Getenv("XUI_LOG_LEVEL")
	if logLevel == "" {
		return Info
	}
	return LogLevel(logLevel)
}

func IsDebug() bool {
	return os.Getenv("XUI_DEBUG") == "true"
}

func GetBinFolderPath() string {
	binFolderPath := os.Getenv("XUI_BIN_FOLDER")
	if binFolderPath == "" {
		binFolderPath = "bin"
	}
	return binFolderPath
}

func GetDBFolderPath() string {
	dbFolderPath := os.Getenv("XUI_DB_FOLDER")
	if dbFolderPath == "" {
		dbFolderPath = "/etc/x-ui"
	}
	return dbFolderPath
}

// DatabaseConfig holds the database configuration
type DatabaseConfig struct {
	Connection string
	Host       string
	Port       string
	Database   string
	Username   string
	Password   string
}

// GetDatabaseConfig returns the database configuration from environment variables
func GetDatabaseConfig() (*DatabaseConfig, error) {
	config := &DatabaseConfig{
		Connection: strings.ToLower(os.Getenv("XUI_DB_CONNECTION")),
		Host:       os.Getenv("XUI_DB_HOST"),
		Port:       os.Getenv("XUI_DB_PORT"),
		Database:   os.Getenv("XUI_DB_DATABASE"),
		Username:   os.Getenv("XUI_DB_USERNAME"),
		Password:   os.Getenv("XUI_DB_PASSWORD"),
	}

	if config.Connection == "mysql" {
		if config.Host == "" || config.Database == "" || config.Username == "" {
			return nil, fmt.Errorf("missing required MySQL configuration: host, database, and username are required")
		}
		if config.Port == "" {
			config.Port = "3306"
		}
	}

	return config, nil
}

func GetDBPath() string {
	config, err := GetDatabaseConfig()
	if err != nil {
		log.Fatalf("Error getting database config: %v", err)
	}

	if config.Connection == "mysql" {
		return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
			config.Username,
			config.Password,
			config.Host,
			config.Port,
			config.Database)
	}

	// Connection is sqlite
	return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
}

func GetLogFolder() string {
	logFolderPath := os.Getenv("XUI_LOG_FOLDER")
	if logFolderPath == "" {
		logFolderPath = "/var/log"
	}
	return logFolderPath
}