feat: 添加rabbitMQ
This commit is contained in:
106
backend/internal/middleware/rabbitmq/rabbitMQ.go
Normal file
106
backend/internal/middleware/rabbitmq/rabbitMQ.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package rabbitmq
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"feedsystem_video_go/internal/config"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
type RabbitMQ struct {
|
||||
conn *amqp.Connection
|
||||
ch *amqp.Channel
|
||||
}
|
||||
|
||||
func NewRabbitMQ(cfg *config.RabbitMQConfig) (*RabbitMQ, error) {
|
||||
if cfg == nil {
|
||||
return nil, errors.New("rabbitmq config is nil")
|
||||
}
|
||||
url := "amqp://" + cfg.Username + ":" + cfg.Password + "@" + cfg.Host + ":" + strconv.Itoa(cfg.Port) + "/"
|
||||
conn, err := amqp.Dial(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &RabbitMQ{conn: conn, ch: ch}, nil
|
||||
}
|
||||
|
||||
func (r *RabbitMQ) Close() error {
|
||||
if r == nil || r.ch == nil || r.conn == nil {
|
||||
return nil
|
||||
}
|
||||
if err := r.ch.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.conn.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RabbitMQ) DeclareTopic(exchange string, queue string, bindingKey string) error {
|
||||
if r == nil || r.ch == nil {
|
||||
return errors.New("rabbitmq is not initialized")
|
||||
}
|
||||
if exchange == "" || queue == "" || bindingKey == "" {
|
||||
return errors.New("exchange/queue/bindingKey is required")
|
||||
}
|
||||
|
||||
if err := r.ch.ExchangeDeclare(
|
||||
exchange,
|
||||
"topic",
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
nil,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
q, err := r.ch.QueueDeclare(
|
||||
queue,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.ch.QueueBind(
|
||||
q.Name,
|
||||
bindingKey,
|
||||
exchange,
|
||||
false,
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *RabbitMQ) PublishJSON(ctx context.Context, exchange string, routingKey string, payload any) error {
|
||||
if r == nil || r.ch == nil {
|
||||
return errors.New("rabbitmq is not initialized")
|
||||
}
|
||||
if exchange == "" || routingKey == "" {
|
||||
return errors.New("exchange and routingKey are required")
|
||||
}
|
||||
b, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.ch.PublishWithContext(ctx, exchange, routingKey, false, false, amqp.Publishing{
|
||||
ContentType: "application/json",
|
||||
DeliveryMode: amqp.Persistent,
|
||||
Timestamp: time.Now(),
|
||||
Body: b,
|
||||
})
|
||||
}
|
||||
77
backend/internal/middleware/rabbitmq/socialMQ.go
Normal file
77
backend/internal/middleware/rabbitmq/socialMQ.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package rabbitmq
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SocialMQ struct {
|
||||
*RabbitMQ
|
||||
}
|
||||
|
||||
const (
|
||||
socialExchange = "social.events"
|
||||
socialQueue = "social.events"
|
||||
socialBindingKey = "social.*"
|
||||
|
||||
socialFollowRK = "social.follow"
|
||||
socialUnfollowRK = "social.unfollow"
|
||||
)
|
||||
|
||||
type SocialEvent struct {
|
||||
EventID string `json:"event_id"`
|
||||
Action string `json:"action"`
|
||||
FollowerID uint `json:"follower_id"`
|
||||
VloggerID uint `json:"vlogger_id"`
|
||||
OccurredAt time.Time `json:"occurred_at"`
|
||||
}
|
||||
|
||||
func NewSocialMQ(base *RabbitMQ) (*SocialMQ, error) {
|
||||
if base == nil {
|
||||
return nil, errors.New("rabbitmq base is nil")
|
||||
}
|
||||
if err := base.DeclareTopic(socialExchange, socialQueue, socialBindingKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SocialMQ{RabbitMQ: base}, nil
|
||||
}
|
||||
|
||||
func (s *SocialMQ) Follow(ctx context.Context, followerID, vloggerID uint) error {
|
||||
return s.publish(ctx, "follow", socialFollowRK, followerID, vloggerID)
|
||||
}
|
||||
|
||||
func (s *SocialMQ) UnFollow(ctx context.Context, followerID, vloggerID uint) error {
|
||||
return s.publish(ctx, "unfollow", socialUnfollowRK, followerID, vloggerID)
|
||||
}
|
||||
|
||||
func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, followerID, vloggerID uint) error {
|
||||
if s == nil || s.RabbitMQ == nil {
|
||||
return errors.New("social mq is not initialized")
|
||||
}
|
||||
if followerID == 0 || vloggerID == 0 {
|
||||
return errors.New("followerID and vloggerID are required")
|
||||
}
|
||||
id, err := newEventID(16)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
evt := SocialEvent{
|
||||
EventID: id,
|
||||
Action: action,
|
||||
FollowerID: followerID,
|
||||
VloggerID: vloggerID,
|
||||
OccurredAt: time.Now().UTC(),
|
||||
}
|
||||
return s.PublishJSON(ctx, socialExchange, routingKey, evt)
|
||||
}
|
||||
|
||||
func newEventID(n int) (string, error) {
|
||||
b := make([]byte, n)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
Reference in New Issue
Block a user