github.com/Jeffail/benthos/v3@v3.65.0/lib/stream/example_base64_test.go (about) 1 package stream 2 3 import ( 4 "bytes" 5 "encoding/base64" 6 "log" 7 "os" 8 "os/signal" 9 "syscall" 10 "time" 11 12 "github.com/Jeffail/benthos/v3/lib/input" 13 "github.com/Jeffail/benthos/v3/lib/output" 14 "github.com/Jeffail/benthos/v3/lib/types" 15 ) 16 17 // Base64Encoder is a types.Processor implementation that base64 encodes 18 // all messages travelling through a Benthos stream. 19 type Base64Encoder struct{} 20 21 // ProcessMessage base64 encodes all messages. 22 func (p Base64Encoder) ProcessMessage(m types.Message) ([]types.Message, types.Response) { 23 // Create a copy of the original message 24 result := m.Copy() 25 26 // For each message part replace its contents with the base64 encoded 27 // version. 28 result.Iter(func(i int, part types.Part) error { 29 var buf bytes.Buffer 30 31 e := base64.NewEncoder(base64.StdEncoding, &buf) 32 e.Write(part.Get()) 33 e.Close() 34 35 part.Set(buf.Bytes()) 36 return nil 37 }) 38 39 return []types.Message{result}, nil 40 } 41 42 // CloseAsync shuts down the processor and stops processing requests. 43 func (p Base64Encoder) CloseAsync() { 44 // Do nothing as our processor doesn't require resource cleanup. 45 } 46 47 // WaitForClose blocks until the processor has closed down. 48 func (p Base64Encoder) WaitForClose(timeout time.Duration) error { 49 // Do nothing as our processor doesn't require resource cleanup. 50 return nil 51 } 52 53 // ExampleBase64Encoder demonstrates running a Kafka to Kafka stream where each 54 // incoming message is encoded with base64. 55 func Example_base64Encoder() { 56 conf := NewConfig() 57 58 conf.Input.Type = input.TypeKafka 59 conf.Input.Kafka.Addresses = []string{ 60 "localhost:9092", 61 } 62 conf.Input.Kafka.Topic = "example_topic_one" 63 64 conf.Output.Type = output.TypeKafka 65 conf.Output.Kafka.Addresses = []string{ 66 "localhost:9092", 67 } 68 conf.Output.Kafka.Topic = "example_topic_two" 69 70 s, err := New(conf, OptAddProcessors(func() (types.Processor, error) { 71 return Base64Encoder{}, nil 72 })) 73 if err != nil { 74 panic(err) 75 } 76 77 defer s.Stop(time.Second) 78 79 sigChan := make(chan os.Signal, 1) 80 signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) 81 82 // Wait for termination signal 83 <-sigChan 84 85 log.Println("Received SIGTERM, the service is closing.") 86 }