You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package postman
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"test3k/authPostman/internal/config"
|
|
|
|
|
|
|
|
"github.com/segmentio/kafka-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthPostmanServer struct {
|
|
|
|
kafkaReader *kafka.Reader
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(ctx context.Context, config *config.Config, topic string) *AuthPostmanServer {
|
|
|
|
return &AuthPostmanServer{
|
|
|
|
kafkaReader: kafka.NewReader(kafka.ReaderConfig{
|
|
|
|
Topic: topic,
|
|
|
|
Brokers: []string{net.JoinHostPort(config.Kafka.Host, strconv.Itoa(config.Kafka.Port))},
|
|
|
|
GroupID: "consumer-group-id",
|
|
|
|
Partition: 0,
|
|
|
|
MinBytes: 10e3, // 10KB
|
|
|
|
MaxBytes: 10e6, // 10MB
|
|
|
|
}),
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AuthPostmanServer) GracefulStop() error {
|
|
|
|
return s.kafkaReader.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AuthPostmanServer) ReadMessage(offset int64) error {
|
|
|
|
// ...
|
|
|
|
s.kafkaReader.SetOffset(offset)
|
|
|
|
|
|
|
|
//
|
|
|
|
for {
|
|
|
|
m, err := s.kafkaReader.ReadMessage(s.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value))
|
|
|
|
}
|
|
|
|
}
|