-
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.
publish new blog posts into a message queue
- Loading branch information
1 parent
6f5dabe
commit 4b717a6
Showing
10 changed files
with
103 additions
and
35 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
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
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,5 @@ | ||
package blog | ||
|
||
type MessageQueueService interface { | ||
Publish(topic string, message []byte) error | ||
} |
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,60 @@ | ||
package rabbitmq | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
amqp "github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
type MessageQueueService struct { | ||
ch *amqp.Channel | ||
} | ||
|
||
func NewMessageQueueService(url string) (*MessageQueueService, error) { | ||
conn, err := amqp.Dial(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ch, err := conn.Channel() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &MessageQueueService{ | ||
ch: ch, | ||
}, nil | ||
} | ||
|
||
func (s *MessageQueueService) Publish(topic string, message []byte) error { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
q, err := s.ch.QueueDeclare( | ||
topic, | ||
false, | ||
false, | ||
false, | ||
false, | ||
nil, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := s.ch.PublishWithContext(ctx, | ||
"", | ||
q.Name, | ||
false, | ||
false, | ||
amqp.Publishing{ | ||
ContentType: "text/plain", | ||
Body: message, | ||
}, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |