From bc57218bc842eb9f63cd596c29daff527b4fc133 Mon Sep 17 00:00:00 2001 From: Ada Date: Wed, 21 Feb 2024 14:22:40 +0100 Subject: [PATCH] Add redis connection test before start --- internal/database/db.go | 9 +++++++++ internal/database/error.go | 7 +++++++ main.go | 5 +++++ 3 files changed, 21 insertions(+) create mode 100644 internal/database/error.go diff --git a/internal/database/db.go b/internal/database/db.go index 1769dc0..6f1e6fc 100644 --- a/internal/database/db.go +++ b/internal/database/db.go @@ -32,6 +32,15 @@ func ConnectDB(dbConfig *redis.Options) *redis.Client { return localDb } +// Ping test connection to Redis database +func Ping(db *redis.Client) error { + status := db.Ping(ctx) + if status.String() != "ping: PONG" { + return &PingError{} + } + return nil +} + func (config DBConfig) InsertPaste(key string, content string, secret string, ttl time.Duration) { type dbSchema struct { content string diff --git a/internal/database/error.go b/internal/database/error.go new file mode 100644 index 0000000..3985065 --- /dev/null +++ b/internal/database/error.go @@ -0,0 +1,7 @@ +package database + +type PingError struct{} + +func (m *PingError) Error() string { + return "Connection to redis not work" +} diff --git a/main.go b/main.go index df25d23..8af2291 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "embed" + "log" "git.gnous.eu/gnouseu/plakken/internal/config" "git.gnous.eu/gnouseu/plakken/internal/database" @@ -19,6 +20,10 @@ func main() { initConfig := config.GetConfig() dbConfig := database.InitDB(initConfig.RedisAddress, initConfig.RedisUser, initConfig.RedisPassword, initConfig.RedisDB) db := database.ConnectDB(dbConfig) + err := database.Ping(db) + if err != nil { + log.Fatal(err) + } serverConfig := httpServer.ServerConfig{ HTTPServer: httpServer.Config(initConfig.ListenAddress),