Skip to content

Commit

Permalink
Merge pull request #272 from LerianStudio/feature/MIDAZ-256
Browse files Browse the repository at this point in the history
Feature/MIDAZ-256
  • Loading branch information
MartinezAvellan authored Nov 18, 2024
2 parents 128e332 + 1e7f12e commit 9ad6105
Show file tree
Hide file tree
Showing 24 changed files with 523 additions and 3 deletions.
2 changes: 1 addition & 1 deletion common/mmongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type MongoConnection struct {
Logger mlog.Logger
}

// Connect keeps a singleton connection with postgres.
// Connect keeps a singleton connection with mongodb.
func (mc *MongoConnection) Connect(ctx context.Context) error {
mc.Logger.Info("Connecting to mongodb...")

Expand Down
64 changes: 64 additions & 0 deletions common/mredis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package mredis

import (
"context"
"go.uber.org/zap"

"github.com/LerianStudio/midaz/common/mlog"
"github.com/redis/go-redis/v9"
)

const RedisTTL = 300

// RedisConnection is a hub which deal with redis connections.
type RedisConnection struct {
Addr string
User string
Password string
DB int
Protocol int
Client *redis.Client
Connected bool
Logger mlog.Logger
}

// Connect keeps a singleton connection with redis.
func (rc *RedisConnection) Connect(ctx context.Context) error {
rc.Logger.Info("Connecting to redis...")

rdb := redis.NewClient(&redis.Options{
Addr: rc.Addr,
Password: rc.Password,
DB: rc.DB,
Protocol: rc.Protocol,
})

_, err := rdb.Ping(ctx).Result()
if err != nil {
rc.Logger.Infof("RedisConnection.Ping %v",
zap.Error(err))

return err
}

rc.Logger.Info("Connected to redis ✅ \n")

rc.Connected = true

rc.Client = rdb

return nil
}

// GetClient returns a pointer to the redis connection, initializing it if necessary.
func (rc *RedisConnection) GetClient(ctx context.Context) (*redis.Client, error) {
if rc.Client == nil {
err := rc.Connect(ctx)
if err != nil {
rc.Logger.Infof("ERRCONECT %s", err)
return nil, err
}
}

return rc.Client, nil
}
3 changes: 3 additions & 0 deletions components/infra/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ MONGO_PASSWORD=lerian
MONGO_PORT=5703

# REDIS
REDIS_HOST=redis
REDIS_PORT=5704
REDIS_USER=midaz
REDIS_PASSWORD=lerian

# RABBITMQ
RABBITMQ_PORT_HOST=5672
Expand Down
8 changes: 8 additions & 0 deletions components/infra/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ services:
redis:
<<: *redis-common
container_name: midaz-redis
environment:
- REDIS_USER=${REDIS_USER}
- REDIS_PASSWORD=${REDIS_PASSWORD}
command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}", "--user", "${REDIS_USER}", "--port ${REDIS_PORT}"]
ports:
- ${REDIS_PORT}:${REDIS_PORT}
volumes:
- redis-data:/data
restart: always

primary-ledger:
<<: *postgres-ledger-common
Expand Down Expand Up @@ -133,6 +140,7 @@ services:

volumes:
mongodb_data_container:
redis-data:

networks:
infra_network:
Expand Down
3 changes: 3 additions & 0 deletions components/ledger/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ MONGO_PASSWORD=lerian
MONGO_PORT=5703

# REDIS
REDIS_HOST=redis
REDIS_PORT=5704
REDIS_USER=midaz
REDIS_PASSWORD=lerian

# LOG LEVEL
LOG_LEVEL=debug
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package redis

import (
"context"
"time"

"github.com/LerianStudio/midaz/common"
"github.com/LerianStudio/midaz/common/mopentelemetry"
"github.com/LerianStudio/midaz/common/mredis"
)

// RedisConsumerRepository is a Redis implementation of the Redis consumer.
type RedisConsumerRepository struct {
conn *mredis.RedisConnection
}

// NewConsumerRedis returns a new instance of RedisRepository using the given Redis connection.
func NewConsumerRedis(rc *mredis.RedisConnection) *RedisConsumerRepository {
r := &RedisConsumerRepository{
conn: rc,
}
if _, err := r.conn.GetClient(context.Background()); err != nil {
panic("Failed to connect on redis")
}

return r
}

func (rr *RedisConsumerRepository) Set(ctx context.Context, key, value string, ttl time.Duration) error {
logger := common.NewLoggerFromContext(ctx)
tracer := common.NewTracerFromContext(ctx)

ctx, span := tracer.Start(ctx, "redis.set")
defer span.End()

rds, err := rr.conn.GetClient(ctx)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to get redis", err)

return err
}

if ttl <= 0 {
ttl = mredis.RedisTTL
}

logger.Infof("value of ttl: %v", ttl)

statusCMD := rds.Set(ctx, key, value, ttl)
if statusCMD.Err() != nil {
mopentelemetry.HandleSpanError(&span, "Failed to set on redis", statusCMD.Err())

return statusCMD.Err()
}

return nil
}

func (rr *RedisConsumerRepository) Get(ctx context.Context, key string) error {
return nil
}

func (rr *RedisConsumerRepository) Del(ctx context.Context, key string) error {
return nil
}
4 changes: 4 additions & 0 deletions components/ledger/internal/app/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
p "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/portfolio"
r "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/product"
rmq "github.com/LerianStudio/midaz/components/ledger/internal/domain/rabbitmq"
rds "github.com/LerianStudio/midaz/components/ledger/internal/domain/redis"
)

// UseCase is a struct that aggregates various repositories for simplified access in use case implementations.
Expand Down Expand Up @@ -36,4 +37,7 @@ type UseCase struct {

// RabbitMQRepo provides an abstraction on top of the producer rabbitmq.
RabbitMQRepo rmq.ProducerRepository

// RedisRepo provides an abstraction on top of the redis consumer.
RedisRepo rds.RedisRepository
}
4 changes: 4 additions & 0 deletions components/ledger/internal/app/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
p "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/portfolio"
r "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/product"
rmq "github.com/LerianStudio/midaz/components/ledger/internal/domain/rabbitmq"
rds "github.com/LerianStudio/midaz/components/ledger/internal/domain/redis"
)

// UseCase is a struct that aggregates various repositories for simplified access in use case implementations.
Expand Down Expand Up @@ -36,4 +37,7 @@ type UseCase struct {

// RabbitMQRepo provides an abstraction on top of the consumer rabbitmq.
RabbitMQRepo rmq.ConsumerRepository

// RedisRepo provides an abstraction on top of the redis consumer.
RedisRepo rds.RedisRepository
}
15 changes: 15 additions & 0 deletions components/ledger/internal/domain/redis/redis_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package redis

import (
"context"
"time"
)

// RedisRepository provides an interface for redis.
//
//go:generate mockgen --destination=../../gen/mock/redis/redis_repository_mock.go --package=mock . RedisRepository
type RedisRepository interface {
Set(ctx context.Context, key, value string, ttl time.Duration) error
Get(ctx context.Context, key string) error
Del(ctx context.Context, key string) error
}
19 changes: 19 additions & 0 deletions components/ledger/internal/gen/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"github.com/LerianStudio/midaz/common/mopentelemetry"
"github.com/LerianStudio/midaz/common/mpostgres"
"github.com/LerianStudio/midaz/common/mrabbitmq"
"github.com/LerianStudio/midaz/common/mredis"
"github.com/LerianStudio/midaz/common/mzap"
"github.com/LerianStudio/midaz/components/ledger/internal/adapters/database/mongodb"
"github.com/LerianStudio/midaz/components/ledger/internal/adapters/database/postgres"
"github.com/LerianStudio/midaz/components/ledger/internal/adapters/database/redis"
rabbitmq "github.com/LerianStudio/midaz/components/ledger/internal/adapters/rabbitmq"
"github.com/LerianStudio/midaz/components/ledger/internal/app/command"
"github.com/LerianStudio/midaz/components/ledger/internal/app/query"
Expand All @@ -28,6 +30,7 @@ import (
"github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/portfolio"
"github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/product"
r "github.com/LerianStudio/midaz/components/ledger/internal/domain/rabbitmq"
rds "github.com/LerianStudio/midaz/components/ledger/internal/domain/redis"
portsGRPC "github.com/LerianStudio/midaz/components/ledger/internal/ports/grpc"
portsHTTP "github.com/LerianStudio/midaz/components/ledger/internal/ports/http"
"github.com/LerianStudio/midaz/components/ledger/internal/service"
Expand Down Expand Up @@ -110,6 +113,19 @@ func setupTelemetryProviders(cfg *service.Config) *mopentelemetry.Telemetry {
return t
}

func setupRedisConnection(cfg *service.Config, log mlog.Logger) *mredis.RedisConnection {
connStrSource := fmt.Sprintf("%s:%s", cfg.RedisHost, cfg.RedisPort)

return &mredis.RedisConnection{
Addr: connStrSource,
User: cfg.RedisUser,
Password: cfg.RedisPassword,
DB: 0,
Protocol: 3,
Logger: log,
}
}

var (
serviceSet = wire.NewSet(
common.InitLocalEnvConfig,
Expand All @@ -119,6 +135,7 @@ var (
setupMongoDBConnection,
setupCasdoorConnection,
setupRabbitMQConnection,
setupRedisConnection,
portsGRPC.NewRouterGRPC,
service.NewServerGRPC,
portsHTTP.NewRouter,
Expand All @@ -133,6 +150,7 @@ var (
mongodb.NewMetadataMongoDBRepository,
rabbitmq.NewProducerRabbitMQ,
rabbitmq.NewConsumerRabbitMQ,
redis.NewConsumerRedis,
wire.Struct(new(portsHTTP.OrganizationHandler), "*"),
wire.Struct(new(portsHTTP.LedgerHandler), "*"),
wire.Struct(new(portsHTTP.AssetHandler), "*"),
Expand All @@ -150,6 +168,7 @@ var (
wire.Bind(new(metadata.Repository), new(*mongodb.MetadataMongoDBRepository)),
wire.Bind(new(r.ConsumerRepository), new(*rabbitmq.ConsumerRabbitMQRepository)),
wire.Bind(new(r.ProducerRepository), new(*rabbitmq.ProducerRabbitMQRepository)),
wire.Bind(new(rds.RedisRepository), new(*redis.RedisConsumerRepository)),
)

svcSet = wire.NewSet(
Expand Down
83 changes: 83 additions & 0 deletions components/ledger/internal/gen/mock/redis/redis_repository_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9ad6105

Please sign in to comment.