github.com/Goboolean/common@v0.0.0-20231130153141-cb54596b217d/deprecated/kafka/consumer.go (about)

     1  package kafka
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Goboolean/common/pkg/resolver"
     7  	"github.com/IBM/sarama"
     8  )
     9  
    10  type Consumer struct {
    11  	consumer sarama.Consumer
    12  
    13  	data map[string]chan interface{}
    14  }
    15  
    16  func NewConsumer(c *resolver.ConfigMap) (*Consumer, error) {
    17  
    18  	host, err := c.GetStringKey("HOST")
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	port, err := c.GetStringKey("PORT")
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	address := fmt.Sprintf("%s:%s", host, port)
    29  
    30  	config := sarama.NewConfig()
    31  	config.Producer.Return.Errors = true
    32  
    33  	consumer, err := sarama.NewConsumer([]string{address}, config)
    34  
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return &Consumer{
    40  		consumer: consumer,
    41  	}, nil
    42  }
    43  
    44  func (c *Consumer) Close() error {
    45  	return c.consumer.Close()
    46  }