Некоторые проверки не удались
CI / test (push) Successful in 2m5s
Docker / Build and publish worker image (push) Failing after 31s
70 строки
1.6 KiB
Go
70 строки
1.6 KiB
Go
// Package credis provides functionality.
|
|
package credis
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/gorilla/sessions"
|
|
"github.com/rbcervilla/redisstore/v8"
|
|
|
|
_ "rocketgit.ru/rsmon/worker/config/env" // Import to ensure .env is loaded before reading env vars
|
|
)
|
|
|
|
// Redis provides functionality.
|
|
var Redis *redis.Client
|
|
|
|
// Store provides functionality.
|
|
var Store *redisstore.RedisStore
|
|
|
|
func envOrDefault(key, defaultVal string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return defaultVal
|
|
}
|
|
|
|
// Init provides functionality.
|
|
func Init() {
|
|
host := envOrDefault("REDIS_HOST", "localhost")
|
|
portStr := envOrDefault("REDIS_PORT", "6379")
|
|
password := os.Getenv("REDIS_PASSWORD")
|
|
dbStr := envOrDefault("REDIS_DATABASE", "0")
|
|
|
|
port, _ := strconv.Atoi(portStr)
|
|
database, _ := strconv.Atoi(dbStr)
|
|
|
|
options := redis.Options{
|
|
Addr: host + ":" + strconv.Itoa(port),
|
|
DB: database,
|
|
}
|
|
if password != "" {
|
|
options.Password = password
|
|
}
|
|
Redis = redis.NewClient(&options)
|
|
|
|
isTest := os.Getenv("RSMON_ENV") == "test" || os.Getenv("GO_ENV") == "test" || os.Getenv("CI") == "true"
|
|
|
|
var err error
|
|
Store, err = redisstore.NewRedisStore(context.Background(), Redis)
|
|
if err != nil {
|
|
if isTest {
|
|
log.Println("Warning: Redis not available in test mode, sessions will not work")
|
|
Store = nil
|
|
return
|
|
}
|
|
log.Fatal("failed to create redis store: ", err)
|
|
}
|
|
|
|
Store.KeyPrefix("session_")
|
|
Store.Options(sessions.Options{
|
|
Path: "/",
|
|
MaxAge: 86400 * 60,
|
|
})
|
|
|
|
log.Printf("Redis connected: %s:%d db=%d", host, port, database)
|
|
}
|