File tree 7 files changed +145
-0
lines changed
7 files changed +145
-0
lines changed Original file line number Diff line number Diff line change
1
+ FROM golang:latest as stack
2
+
3
+ RUN set -ex; \
4
+ mkdir /opt/kafka-gateway; \
5
+ go get -u gopkg.in/confluentinc/confluent-kafka-go.v1/kafka;
6
+
7
+ FROM stack
8
+
9
+ WORKDIR /opt/kafka-gateway
10
+ COPY go.mod go.mod
11
+ COPY go.sum go.sum
12
+ COPY internal internal
13
+ COPY cmd cmd
14
+ COPY main.go main.go
15
+
16
+ RUN set -ex; \
17
+ ls -ls; \
18
+ go version; \
19
+ go build -o ./bin/gateway;
Original file line number Diff line number Diff line change
1
+ # Kafka Http Gateway
2
+
3
+
4
+
5
+
6
+ ### Dependenices
7
+
8
+ [ kafka] [ "github.com/confluentinc/confluent-kafka-go/kafka" ]
9
+
10
+ ### Tips
11
+
12
+ Setup the env ` CGO_ENABLED=0 ` in order to build app with kafka lib.
Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+
3
+ main () {
4
+
5
+ case $1 in
6
+ run)
7
+ dockerRun
8
+ ;;
9
+ build)
10
+ dockerBuild
11
+ ;;
12
+ * )
13
+ badOpt $@
14
+ esac
15
+
16
+ }
17
+
18
+ badOpt () {
19
+ echo bad options: $@
20
+ }
21
+
22
+ dockerRun () {
23
+ docker run --name kafka-gateway -it --rm s7i/kafka-gateway
24
+ }
25
+
26
+ dockerBuild () {
27
+ docker build -t s7i/kafka-gateway .
28
+ }
29
+
30
+ main $@
Original file line number Diff line number Diff line change
1
+ module s7i.io/kafka-gateway
2
+
3
+ go 1.17
4
+
5
+ require github.com/confluentinc/confluent-kafka-go v1.7.0
Original file line number Diff line number Diff line change
1
+ github.com/confluentinc/confluent-kafka-go v1.7.0 h1:tXh3LWb2Ne0WiU3ng4h5qiGA9XV61rz46w60O+cq8bM =
2
+ github.com/confluentinc/confluent-kafka-go v1.7.0 /go.mod h1:u2zNLny2xq+5rWeTQjFHbDzzNuba4P1vo31r9r4uAdg =
Original file line number Diff line number Diff line change
1
+ package kafka
2
+
3
+ import (
4
+ "fmt"
5
+
6
+ "github.com/confluentinc/confluent-kafka-go/kafka"
7
+ )
8
+
9
+ type Properties struct {
10
+ Server string
11
+ }
12
+
13
+ type KafkaProducer struct {
14
+ prod * kafka.Producer
15
+ }
16
+
17
+ func (prod * KafkaProducer ) Init (props * Properties ) {
18
+ fmt .Println ("making kafka producer" )
19
+
20
+ p , err := kafka .NewProducer (& kafka.ConfigMap {"bootstrap.servers" : props .Server })
21
+
22
+ if err != nil {
23
+
24
+ panic (err )
25
+ }
26
+ prod .prod = p
27
+
28
+ go func () {
29
+ for e := range p .Events () {
30
+ switch ev := e .(type ) {
31
+ case * kafka.Message :
32
+ m := ev
33
+ if m .TopicPartition .Error != nil {
34
+ fmt .Printf ("Delivery failed: %v\n " , m .TopicPartition .Error )
35
+ } else {
36
+ fmt .Printf ("Delivered message to topic %s [%d] at offset %v\n " ,
37
+ * m .TopicPartition .Topic , m .TopicPartition .Partition , m .TopicPartition .Offset )
38
+ }
39
+ return
40
+
41
+ default :
42
+ fmt .Printf ("Ignored event: %s\n " , ev )
43
+ }
44
+ }
45
+ }()
46
+
47
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "io"
6
+ "net/http"
7
+ "time"
8
+
9
+ "s7i.io/kafka-gateway/internal/kafka"
10
+ )
11
+
12
+ func main () {
13
+
14
+ prod := kafka.KafkaProducer {}
15
+ prod .Init (& kafka.Properties {Server : "localhost:9092" })
16
+
17
+ fmt .Println ("running server" )
18
+
19
+ http .HandleFunc ("/hello" , hndHello )
20
+ if err := http .ListenAndServe (":8080" , nil ); err != nil {
21
+ panic (err )
22
+ }
23
+
24
+ }
25
+
26
+ func hndHello (w http.ResponseWriter , req * http.Request ) {
27
+ io .WriteString (w , "hello world @" + time .Now ().String ())
28
+
29
+ fmt .Println (req )
30
+ }
You can’t perform that action at this time.
0 commit comments