51 lines
980 B
Go
51 lines
980 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/joho/godotenv"
|
|
"github.com/topfans/backend/pkg/database"
|
|
)
|
|
|
|
func main() {
|
|
_ = godotenv.Load("../../.env", "../.env", ".env")
|
|
|
|
host := getenv("REDIS_HOST", "127.0.0.1")
|
|
port := getenvInt("REDIS_PORT", 6379)
|
|
password := os.Getenv("REDIS_PASSWORD")
|
|
db := getenvInt("REDIS_DB", 0)
|
|
|
|
if err := database.InitRedis(database.RedisConfig{
|
|
Host: host,
|
|
Port: port,
|
|
Password: password,
|
|
DB: db,
|
|
}); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Redis connection failed: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer database.CloseRedis()
|
|
|
|
fmt.Printf("Redis OK: %s:%d db=%d auth=%v\n", host, port, db, password != "")
|
|
}
|
|
|
|
func getenv(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func getenvInt(key string, fallback int) int {
|
|
v := os.Getenv(key)
|
|
if v == "" {
|
|
return fallback
|
|
}
|
|
var n int
|
|
if _, err := fmt.Sscanf(v, "%d", &n); err != nil {
|
|
return fallback
|
|
}
|
|
return n
|
|
}
|