2021-03-30 16:05:40 +05:30
|
|
|
package lib
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
producer_module "com.navi.medici.janus/producer"
|
|
|
|
|
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
RequestChannel = make(chan *RequestObject)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RequestObject struct {
|
|
|
|
|
Body []byte
|
|
|
|
|
Header http.Header
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 16:32:05 +05:30
|
|
|
func ProcessRequestChannel(topic string) {
|
2021-03-30 16:05:40 +05:30
|
|
|
for {
|
|
|
|
|
request := <- RequestChannel
|
2021-03-30 16:32:05 +05:30
|
|
|
ClickstreamProtobufEventHandler(*request, topic)
|
2021-03-30 16:05:40 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ClickstreamProtobufEventHandler(request RequestObject, topic string) {
|
|
|
|
|
|
|
|
|
|
messageBytes := request.Body
|
|
|
|
|
|
2021-04-01 12:56:04 +05:30
|
|
|
// to be of the format [magicByte] + [schemaID] + [messageIndex] + [value]
|
2021-03-30 16:05:40 +05:30
|
|
|
recordValue := []byte{}
|
|
|
|
|
|
|
|
|
|
// add [magicByte]
|
|
|
|
|
recordValue = append(recordValue, byte(0))
|
|
|
|
|
|
|
|
|
|
// add schemaID]
|
|
|
|
|
schemaIDBytes := make([]byte, 4)
|
|
|
|
|
binary.BigEndian.PutUint32(schemaIDBytes, uint32(producer_module.SchemaVersionMap[topic]))
|
|
|
|
|
recordValue = append(recordValue, schemaIDBytes...)
|
|
|
|
|
|
|
|
|
|
// add [messageIndex]
|
|
|
|
|
messageIndexBytes := []byte{byte(2), byte(0)}
|
|
|
|
|
recordValue = append(recordValue, messageIndexBytes...)
|
|
|
|
|
|
2021-04-01 12:56:04 +05:30
|
|
|
// Now write the bytes from the actual message...
|
2021-03-30 16:05:40 +05:30
|
|
|
recordValue = append(recordValue, messageBytes...)
|
|
|
|
|
|
|
|
|
|
message := &sarama.ProducerMessage{
|
|
|
|
|
Topic: topic,
|
|
|
|
|
Value: sarama.ByteEncoder(recordValue),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
producer_module.WriteMessageToKafkaAsync(message)
|
|
|
|
|
|
|
|
|
|
}
|