Files
SingBox-Gopanel/internal/config/config.go
CN-JS-HuiBai e7b123ea59
Some checks failed
build / build (api, amd64, linux) (push) Failing after -49s
build / build (api, arm64, linux) (push) Failing after -51s
build / build (api.exe, amd64, windows) (push) Failing after -51s
修复跨域请求问题
2026-04-17 22:50:46 +08:00

86 lines
1.7 KiB
Go

package config
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
type Config struct {
DBHost string
DBPort string
DBUser string
DBPass string
DBName string
RedisHost string
RedisPort string
RedisPass string
RedisDB int
JWTSecret string
AppPort string
AppURL string
PluginRoot string
AppDebug bool
}
var AppConfig *Config
func LoadConfig() {
err := godotenv.Load()
if err != nil {
log.Println("No .env file found, using environment variables")
}
AppConfig = &Config{
DBHost: getEnv("DB_HOST", "localhost"),
DBPort: getEnv("DB_PORT", "3306"),
DBUser: getEnv("DB_USER", "root"),
DBPass: getEnv("DB_PASS", ""),
DBName: getEnv("DB_NAME", "xboard"),
RedisHost: getEnv("REDIS_HOST", "localhost"),
RedisPort: getEnv("REDIS_PORT", "6379"),
RedisPass: getEnv("REDIS_PASS", ""),
RedisDB: getEnvInt("REDIS_DB", 0),
JWTSecret: getEnv("JWT_SECRET", "secret"),
AppPort: getEnv("APP_PORT", "8080"),
AppURL: getEnv("APP_URL", ""),
PluginRoot: getEnv("PLUGIN_ROOT", "reference\\LDNET-GA-Theme\\plugin"),
AppDebug: getEnvBool("APP_DEBUG", false),
}
}
func getEnvBool(key string, defaultValue bool) bool {
raw := getEnv(key, "")
if raw == "" {
return defaultValue
}
value, err := strconv.ParseBool(raw)
if err != nil {
return defaultValue
}
return value
}
func getEnv(key, defaultValue string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultValue
}
func getEnvInt(key string, defaultValue int) int {
raw := getEnv(key, "")
if raw == "" {
return defaultValue
}
value, err := strconv.Atoi(raw)
if err != nil {
return defaultValue
}
return value
}