main file for internal/db package

master
Matthew 2024-10-04 17:50:08 -04:00
parent 29588e0829
commit 5489020ba1
Signed by: matthew
SSH Key Fingerprint: SHA256:piIXekA9q1p0ZGi4ogFbNY1embip5Ytbi3v8AZ8UYq4
1 changed files with 26 additions and 0 deletions

26
internal/db/main.go Normal file
View File

@ -0,0 +1,26 @@
package db
import (
"context"
"git.libraryofcode.org/engineering/bces/config"
"github.com/redis/go-redis/v9"
"log"
)
// NewRedisConnection takes a Config parameter and connects to the RedisURL provided in the config. It returns an instance of Redis Client.
// This function will exit the program if it cannot parse the URL properly nor connect successfully, and therefore doesn't return an error.
func NewRedisConnection(config config.Config) *redis.Client {
redisConnectionOptions, err := redis.ParseURL(config.RedisURL)
if err != nil {
log.Fatalf("Redis Connection URL is not valid or cannot parse given Connection URL: %s.\n\n%s", config.RedisURL, err)
}
redisClient := redis.NewClient(redisConnectionOptions)
redisPingResponse, err := redisClient.Ping(context.Background()).Result()
if err != nil {
log.Fatalf("Could not ping Redis.\n\n%s", err)
}
log.Printf("Connected to Redis DB at <%s>: %s", config.RedisURL, redisPingResponse)
return redisClient
}