diff --git a/cache/cache.go b/cache/cache.go index 2e7cbe7..fdc6eb2 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -4,27 +4,19 @@ import ( "time" "github.com/gofiber/storage/sqlite3/v2" + "github.com/ochom/gutils/helpers" ) var store *sqlite3.Storage -// Init creates a new cache instance -func Init(dbPath, tableName string) { - store = initCache(dbPath, tableName) +func init() { + database := helpers.GetEnv("CACHE_DB_PATH", "./fiber.sqlite3") + table := helpers.GetEnv("CACHE_TABLE_NAME", "fiber_storage") + store = initCache(database, table) } // initCache creates a new cache instance func initCache(dbPath, tableName string) *sqlite3.Storage { - path := dbPath - if path == "" { - path = "./fiber.sqlite3" - } - - name := tableName - if name == "" { - name = "fiber_storage" - } - return sqlite3.New(sqlite3.Config{ Database: dbPath, Table: tableName, diff --git a/cache/cache_test.go b/cache/cache_test.go index a7fbf6f..c83ea2f 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -9,7 +9,6 @@ import ( ) func TestSet(t *testing.T) { - cache.Init("", "") type args struct { key string value []byte @@ -32,7 +31,6 @@ func TestSet(t *testing.T) { } func TestGet(t *testing.T) { - cache.Init("", "") type args struct { key string } diff --git a/cache/fiber.sqlite3 b/cache/fiber.sqlite3 index d2d1434..1dd8e84 100644 Binary files a/cache/fiber.sqlite3 and b/cache/fiber.sqlite3 differ diff --git a/helpers/password.go b/helpers/password.go index efc14a3..0bb1891 100644 --- a/helpers/password.go +++ b/helpers/password.go @@ -64,17 +64,17 @@ func GenerateOTP(size int) string { } // HashPassword hashes a password -func HashPassword(password string) []byte { +func HashPassword(password string) string { bsp, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { - return []byte{} + return "" } - return bsp + return string(bsp) } // ComparePassword compares a password with a hash -func ComparePassword(hash []byte, password string) bool { - err := bcrypt.CompareHashAndPassword(hash, []byte(password)) +func ComparePassword(hashedString string, password string) bool { + err := bcrypt.CompareHashAndPassword([]byte(hashedString), []byte(password)) return err == nil } diff --git a/helpers/password_test.go b/helpers/password_test.go index cab6cc9..19c65a5 100644 --- a/helpers/password_test.go +++ b/helpers/password_test.go @@ -34,7 +34,7 @@ func TestGenerateOTP(t *testing.T) { func TestPasswordHash(t *testing.T) { hash := helpers.HashPassword("password") - if !helpers.ComparePassword([]byte(hash), "password") { + if !helpers.ComparePassword(hash, "password") { t.Errorf("PasswordHash() = %v, want %v", hash, "password") } } diff --git a/pubsub/consumer.go b/pubsub/consumer.go index b6cdfd2..2786be3 100644 --- a/pubsub/consumer.go +++ b/pubsub/consumer.go @@ -17,18 +17,17 @@ func newConsumer(rabbitURL, queueName string) *Consumer { } // Consume consume messages from the channels -func Consume(queueName string, delayed bool, workerFunc func([]byte)) error { +func Consume(queueName string, workerFunc func([]byte)) error { c := newConsumer(rabbitURL, queueName) conn, ch, err := initQ(c.url) if err != nil { return fmt.Errorf("failed to initialize a connection: %s", err.Error()) } - defer ch.Close() defer conn.Close() - if err := bind(ch, c.exchange, c.queue, delayed); err != nil { - return err + if err := initPubSub(ch, c.exchange, c.queue); err != nil { + return fmt.Errorf("failed to initialize a pubsub: %s", err.Error()) } deliveries, err := ch.Consume( diff --git a/pubsub/consumer_test.go b/pubsub/consumer_test.go index 72e351b..742bb3b 100644 --- a/pubsub/consumer_test.go +++ b/pubsub/consumer_test.go @@ -8,7 +8,6 @@ import ( func TestConsume(t *testing.T) { type args struct { queueName string - delayed bool workerFunc func([]byte) } tests := []struct { @@ -20,7 +19,6 @@ func TestConsume(t *testing.T) { name: "test 1", args: args{ queueName: "test", - delayed: true, workerFunc: func(body []byte) { fmt.Println(string(body)) }, @@ -32,7 +30,7 @@ func TestConsume(t *testing.T) { t.Skip("Skipping test in non-local environment") } t.Run(tt.name, func(t *testing.T) { - if err := Consume(tt.args.queueName, tt.args.delayed, tt.args.workerFunc); (err != nil) != tt.wantErr { + if err := Consume(tt.args.queueName, tt.args.workerFunc); (err != nil) != tt.wantErr { t.Errorf("Consume() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/pubsub/publisher.go b/pubsub/publisher.go index 2ce26fe..b20c9e5 100644 --- a/pubsub/publisher.go +++ b/pubsub/publisher.go @@ -21,7 +21,7 @@ func newPublisher(queueName string) *publisher { } // publish ... -func (p *publisher) publish(body []byte, delay time.Duration, delayed bool) error { +func (p *publisher) publish(body []byte, delay time.Duration) error { conn, ch, err := initQ(p.url) if err != nil { return err @@ -30,7 +30,7 @@ func (p *publisher) publish(body []byte, delay time.Duration, delayed bool) erro defer ch.Close() defer conn.Close() - if err := bind(ch, p.exchange, p.queue, delayed); err != nil { + if err := initPubSub(ch, p.exchange, p.queue); err != nil { return err } @@ -56,11 +56,11 @@ func (p *publisher) publish(body []byte, delay time.Duration, delayed bool) erro // PublishWithDelay ... func PublishWithDelay(queueName string, body []byte, delay time.Duration) error { p := newPublisher(fmt.Sprintf("%s-%s", queuePrefix, queueName)) - return p.publish(body, delay, true) + return p.publish(body, delay) } // Publish ... func Publish(queueName string, body []byte) error { p := newPublisher(fmt.Sprintf("%s-%s", queuePrefix, queueName)) - return p.publish(body, 0, false) + return p.publish(body, 0) } diff --git a/pubsub/pubsub.go b/pubsub/pubsub.go index 2cdb2d2..3ac9f2e 100644 --- a/pubsub/pubsub.go +++ b/pubsub/pubsub.go @@ -35,15 +35,17 @@ func initQ(url string) (*amqp.Connection, *amqp.Channel, error) { } // initPubSub ... -func initPubSub(ch *amqp.Channel, args amqp.Table, channelType, exchangeName, queueName string) error { +func initPubSub(ch *amqp.Channel, exchangeName, queueName string) error { err := ch.ExchangeDeclare( - exchangeName, // name - channelType, // type - true, // durable - false, // auto-deleted - false, // internal - false, // no-wait - args, // arguments + exchangeName, // name + "x-delayed-message", // type + true, // durable + false, // auto-deleted + false, // internal + false, // no-wait + amqp.Table{ + "x-delayed-type": "direct", + }, // arguments ) if err != nil { return fmt.Errorf("exchange Declare: %s", err.Error()) @@ -76,17 +78,3 @@ func initPubSub(ch *amqp.Channel, args amqp.Table, channelType, exchangeName, qu return nil } - -// bind ... -func bind(ch *amqp.Channel, exchangeName, queueName string, delayed bool) error { - args := make(amqp.Table) - if delayed { - args["x-delayed-type"] = "direct" - channelType := "x-delayed-message" - return initPubSub(ch, args, channelType, exchangeName, queueName) - } - - args["x-queue-mode"] = "lazy" - channelType := "direct" - return initPubSub(ch, args, channelType, exchangeName, queueName) -}