Skip to content

Instantly share code, notes, and snippets.

@chris001177
Created July 28, 2019 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris001177/9717aeec94d5502e2adc5cafe083a792 to your computer and use it in GitHub Desktop.
Save chris001177/9717aeec94d5502e2adc5cafe083a792 to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
sarama "gopkg.in/Shopify/sarama.v1"
)
// KafkaController allows us to attach a producer
// to our handlers
type KafkaController struct {
producer sarama.AsyncProducer
}
// Handler grabs a message from a GET parama and
// send it to the kafka queue asynchronously
func (c *KafkaController) Handler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
msg := r.FormValue("msg")
if msg == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("msg must be set"))
return
}
c.producer.Input() <- &sarama.ProducerMessage{Topic:
"example", Key: nil, Value:
sarama.StringEncoder(r.FormValue("msg"))}
w.WriteHeader(http.StatusOK)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment