-
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.
Merge pull request #8 from creztfallen/refactor/dependencies-interchange
Refactor/dependencies interchange
- Loading branch information
Showing
9 changed files
with
234 additions
and
198 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 |
---|---|---|
@@ -1 +1 @@ | ||
API_KEY=7cc97052c8384d669d2eba12b32d3a36 | ||
API_URL="https://api.currencyfreaks.com/v2.0/rates/latest?apikey=7cc97052c8384d669d2eba12b32d3a36" |
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,43 @@ | ||
package message_broker | ||
|
||
import ( | ||
amqp "github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
type MessageQueue interface { | ||
SendMessage(message interface{}, queueName string) error | ||
ReceiveMessage(queueName string) (<- chan amqp.Delivery, error) | ||
Close() | ||
} | ||
|
||
type RabbitMQ struct { | ||
conn *amqp.Connection | ||
ch *amqp.Channel | ||
CleanUp func() | ||
} | ||
|
||
|
||
|
||
func NewRabbitMQ(url string) (*RabbitMQ, error) { | ||
conn, err := amqp.Dial(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ch, err := conn.Channel() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rabbitmqInstance := &RabbitMQ{ | ||
conn: conn, | ||
ch: ch, | ||
} | ||
|
||
rabbitmqInstance.CleanUp = func() { | ||
ch.Close() | ||
} | ||
|
||
return rabbitmqInstance, nil | ||
} | ||
|
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,77 @@ | ||
package message_broker | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"time" | ||
|
||
amqp "github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
func (r *RabbitMQ) SendMessage(message interface{}, queueName string) error { | ||
_, err := r.ch.QueueDeclare( | ||
queueName, // name | ||
true, // durable | ||
false, // delete when unused | ||
false, // exclusive | ||
false, // no-wait | ||
nil, // arguments | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
encryptedMessage, err := json.Marshal(message) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
err = r.ch.PublishWithContext(ctx, | ||
"", // exchange | ||
queueName, // routing key | ||
false, // mandatory | ||
false, // immediate | ||
amqp.Publishing{ | ||
ContentType: "application/json", | ||
Body: []byte(encryptedMessage), | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func (r *RabbitMQ) ReceiveMessage(queueName string) (<- chan amqp.Delivery, error) { | ||
_, err := r.ch.QueueDeclare( | ||
queueName, // name | ||
true, // durable | ||
false, // delete when unused | ||
false, // exclusive | ||
false, // no-wait | ||
nil, // arguments | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
msgs, err := r.ch.Consume( | ||
queueName, // queue | ||
"", // consumer | ||
true, // auto-ack | ||
false, // exclusive | ||
false, // no-local | ||
false, // no-wait | ||
nil, // args | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
|
||
return msgs, nil | ||
} | ||
|
||
func (r *RabbitMQ) Close() { | ||
r.CleanUp() | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.