-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89b842c
commit f5a485b
Showing
24 changed files
with
1,138 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
### JWT Secret | ||
JWT_SECRET=helloworld | ||
|
||
### User Database Configuration | ||
USER_DB_HOST=localhost | ||
USER_DB_PORT=5435 | ||
USER_DB_USER=admin | ||
USER_DB_PASSWORD=password | ||
USER_DB_NAME=mydatabase | ||
|
||
### Holding Database Configuration | ||
HOLDING_DB_HOST=localhost | ||
HOLDING_DB_PORT=5434 | ||
HOLDING_DB_USER=admin | ||
HOLDING_DB_PASSWORD=password | ||
HOLDING_DB_NAME=mydatabase | ||
|
||
### User Redis Configuration | ||
USER_REDIS_HOST=localhost | ||
USER_REDIS_PORT=6382 | ||
USER_REDIS_PASSWORD=password | ||
|
||
### Holding Redis Configuration | ||
HOLDING_REDIS_HOST=localhost | ||
HOLDING_REDIS_PORT=6381 | ||
HOLDING_REDIS_PASSWORD=password | ||
|
||
### RabbitMQ Configuration | ||
RABBITMQ_USER=admin | ||
RABBITMQ_PASSWORD=password | ||
RABBITMQ_HOST=localhost | ||
RABBITMQ_PORT=5672 | ||
|
||
### Application Port | ||
PORT=8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "holding", | ||
"scripts": { | ||
"build": "go build -o cmd/main ./cmd", | ||
"dev": "go run cmd/main.go" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"holding/pkg/config" | ||
"sync" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
var ( | ||
onceHolding sync.Once | ||
redisClientHolding *redis.Client | ||
ctxHolding = context.Background() | ||
) | ||
|
||
// Init initializes the Redis client as a singleton | ||
func InitHolding(redisURL, redisPassword string) error { | ||
var initErr error | ||
|
||
onceHolding.Do(func() { | ||
redisClientHolding = redis.NewClient(&redis.Options{ | ||
Addr: redisURL, | ||
Password: redisPassword, | ||
DB: 0, | ||
}) | ||
|
||
// Retry connection with exponential backoff | ||
for attempt := 1; attempt <= 5; attempt++ { | ||
_, err := redisClientHolding.Ping(ctxHolding).Result() | ||
if err == nil { | ||
initErr = nil | ||
return | ||
} | ||
initErr = err | ||
time.Sleep(time.Duration(attempt) * 2 * time.Second) // Exponential backoff | ||
} | ||
}) | ||
|
||
return initErr | ||
} | ||
|
||
// GetRedisClient returns the singleton Redis client | ||
func GetRedisClientHolding() *redis.Client { | ||
if redisClientHolding == nil { | ||
InitHolding(config.AppConfig.HOLDING_RedisURL, config.AppConfig.HOLDING_RedisPassword) | ||
} | ||
return redisClientHolding | ||
} | ||
|
||
// Get retrieves a value from Redis | ||
func GetHolding(key string) (map[string]interface{}, error) { | ||
val, err := GetRedisClientHolding().Get(ctxHolding, key).Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data map[string]interface{} | ||
err = json.Unmarshal([]byte(val), &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
// Set stores a value in Redis | ||
func SetHolding(key string, value map[string]interface{}, expire time.Duration) error { | ||
jsonValue, err := json.Marshal(value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return GetRedisClientHolding().Set(ctxHolding, key, jsonValue, expire).Err() | ||
} | ||
|
||
// Close closes the Redis connection | ||
func CloseHolding() { | ||
if redisClientHolding != nil { | ||
_ = redisClientHolding.Close() | ||
} | ||
} |
Oops, something went wrong.