From f59f6d31ea6823da225ef2d8024d4d8bf20f3337 Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Wed, 3 Dec 2025 15:10:17 +0300 Subject: [PATCH 1/7] update cache implementations --- cache/redis.go | 23 ++++++++++++++++++++--- env/env.go | 32 ++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/cache/redis.go b/cache/redis.go index 2a2c777..a48b679 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -15,10 +15,27 @@ type redisCache struct { } func newRedisCache() Cache { + host := env.Get("REDIS_HOST") + port := env.Get("REDIS_PORT") + + url := "" + if host != "" && port != "" { + url = host + ":" + port + } + + if url == "" { + url = env.Get("REDIS_URL") + } + + if url == "" { + logs.Error("newRedisCache: REDIS_HOST, REDIS_PORT or REDIS_URL must be set") + return newMemoryCache() + } + cl := redis.NewClient(&redis.Options{ - Addr: env.Get("REDIS_URL", "localhost:6379"), - Password: env.Get("REDIS_PASSWORD", ""), - DB: env.Int("REDIS_DB_INDEX", 0), + Addr: host, + Password: env.Get("REDIS_PASSWORD"), + DB: env.Int("REDIS_DB_INDEX"), }) if err := cl.Ping(context.Background()).Err(); err != nil { diff --git a/env/env.go b/env/env.go index b211a8a..3f82fcb 100644 --- a/env/env.go +++ b/env/env.go @@ -8,61 +8,65 @@ import ( _ "github.com/joho/godotenv/autoload" ) -// Get returns env variable or the provided default value when variable not found -func Get(name string, defaults ...string) string { - defaultValue := "" +func defaultValue[T any](defaults ...T) T { if len(defaults) > 0 { - defaultValue = defaults[0] + return defaults[0] } + var zero T + return zero +} + +// Get returns env variable or the provided default value when variable not found +func Get(name string, defaults ...string) string { value, ok := os.LookupEnv(name) if !ok { - return defaultValue + return defaultValue(defaults...) } return value } // Int returns an integer from env variable or the provided default value when variable not found -func Int(name string, defaultValue int) int { +func Int(name string, defaults ...int) int { value, ok := os.LookupEnv(name) if !ok { - return defaultValue + return defaultValue(defaults...) } val, err := strconv.Atoi(value) if err != nil { - return defaultValue + return defaultValue(defaults...) } return val } // Bool returns a boolean from env variable or the provided default value when variable not found -func Bool(name string, defaultValue bool) bool { +func Bool(name string, defaults ...bool) bool { value, ok := os.LookupEnv(name) if !ok { - return defaultValue + return defaultValue(defaults...) } val, err := strconv.ParseBool(value) if err != nil { - return defaultValue + return defaultValue(defaults...) } return val } // Float returns a float from env variable or the provided default value when variable not found -func Float(name string, defaultValue float64) float64 { +func Float(name string, defaults ...float64) float64 { value, ok := os.LookupEnv(name) if !ok { - return defaultValue + return defaultValue(defaults...) } val, err := strconv.ParseFloat(value, 64) if err != nil { - return defaultValue + return defaultValue(defaults...) } return val From 472cf3e677ce470979d0e36b0ef65d5969dc100f Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Wed, 3 Dec 2025 15:17:48 +0300 Subject: [PATCH 2/7] update cache implementations --- cache/cache.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 70ada0e..1547870 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -19,10 +19,10 @@ var conn Cache // default to memory cache func init() { - switch env.Int("CACHE_DRIVER", 0) { - case Memory: + switch env.Get("CACHE_DRIVER", "redis") { + case "memory": conn = newMemoryCache() - case Redis: + case "redis": conn = newRedisCache() default: panic("unknown cache driver") From 84d2f585624ee3a103d5c00359f7784a9eb062d7 Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Wed, 3 Dec 2025 15:28:40 +0300 Subject: [PATCH 3/7] update cache implementations --- gttp/gttp.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/gttp/gttp.go b/gttp/gttp.go index e047777..ee0e1af 100644 --- a/gttp/gttp.go +++ b/gttp/gttp.go @@ -6,13 +6,6 @@ import ( "github.com/ochom/gutils/env" ) -type ClientType int - -const ( - DefaultHttp = iota - GoFiber -) - type Client interface { post(url string, headers M, body []byte, timeout ...time.Duration) (resp *Response, err error) get(url string, headers M, timeout ...time.Duration) (resp *Response, err error) @@ -22,10 +15,10 @@ type Client interface { var client Client func init() { - switch env.Int("HTTP_CLIENT", 1) { - case DefaultHttp: + switch env.Get("HTTP_CLIENT", "fiber") { + case "default": client = new(defaultClient) - case GoFiber: + case "fiber": client = new(fiberClient) default: panic("unknown http client") From fc872ed711824b72e4fdf0000a918dac49c11605 Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Thu, 4 Dec 2025 14:35:40 +0300 Subject: [PATCH 4/7] connect correctly to redis --- cache/redis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cache/redis.go b/cache/redis.go index a48b679..fd41047 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -33,7 +33,7 @@ func newRedisCache() Cache { } cl := redis.NewClient(&redis.Options{ - Addr: host, + Addr: url, Password: env.Get("REDIS_PASSWORD"), DB: env.Int("REDIS_DB_INDEX"), }) From 2fae492364f4dd0bf7f8d2fa309264c410b7c950 Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Thu, 4 Dec 2025 14:37:29 +0300 Subject: [PATCH 5/7] connect correctly to redis --- cache/redis.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cache/redis.go b/cache/redis.go index fd41047..b01e32d 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -21,6 +21,7 @@ func newRedisCache() Cache { url := "" if host != "" && port != "" { url = host + ":" + port + goto initRedis } if url == "" { @@ -32,6 +33,7 @@ func newRedisCache() Cache { return newMemoryCache() } +initRedis: cl := redis.NewClient(&redis.Options{ Addr: url, Password: env.Get("REDIS_PASSWORD"), From ea7701bf40170611aba5c905b627f4b3a481dc61 Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Thu, 4 Dec 2025 14:45:47 +0300 Subject: [PATCH 6/7] fix handle cache updates --- cache/cache.go | 2 +- gttp/gttp.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 1547870..9b62b66 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -25,7 +25,7 @@ func init() { case "redis": conn = newRedisCache() default: - panic("unknown cache driver") + conn = newRedisCache() } } diff --git a/gttp/gttp.go b/gttp/gttp.go index ee0e1af..6a87984 100644 --- a/gttp/gttp.go +++ b/gttp/gttp.go @@ -21,7 +21,7 @@ func init() { case "fiber": client = new(fiberClient) default: - panic("unknown http client") + client = new(fiberClient) } } From 43e2e3d3e1c6968afcfb41aac6335b1891cb5444 Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Thu, 4 Dec 2025 14:46:45 +0300 Subject: [PATCH 7/7] fix handle cache updates --- gttp/default.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gttp/default.go b/gttp/default.go index b6c6c9e..a2dbeb9 100644 --- a/gttp/default.go +++ b/gttp/default.go @@ -54,7 +54,9 @@ func (c *defaultClient) sendRequest(url, method string, headers M, body []byte, return } - defer res.Body.Close() + defer func() { + _ = res.Body.Close() + }() content, err := io.ReadAll(res.Body) if err != nil {