forked from 0sc/sarama-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
44 lines (34 loc) · 1.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"html"
"log"
"net/http"
"github.com/Shopify/sarama"
)
const topic = "sample-topic"
func main() {
producer, err := newProducer()
if err != nil {
fmt.Println("Could not create producer: ", err)
}
consumer, err := sarama.NewConsumer(brokers, nil)
if err != nil {
fmt.Println("Could not create consumer: ", err)
}
subscribe(topic, consumer)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello Sarama!") })
http.HandleFunc("/save", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
r.ParseForm()
msg := prepareMessage(topic, r.FormValue("q"))
partition, offset, err := producer.SendMessage(msg)
if err != nil {
fmt.Fprintf(w, "%s error occured.", err.Error())
} else {
fmt.Fprintf(w, "Message was saved to partion: %d.\nMessage offset is: %d.\n", partition, offset)
}
})
http.HandleFunc("/retrieve", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, html.EscapeString(getMessage())) })
log.Fatal(http.ListenAndServe(":8081", nil))
}