forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
391 lines (328 loc) · 10.7 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/Shopify/sarama"
"github.com/kataras/iris/v12"
)
/*
First of all, read about Apache Kafka, install and run it, if you didn't already: https://kafka.apache.org/quickstart
Secondly, install your favourite Go library for Apache Kafka communication.
I have chosen the shopify's one although I really loved the `segmentio/kafka-go` as well but it needs more to be done there
and you will be bored to read all the necessary code required to get started with it, so:
$ go get -u github.com/Shopify/sarama
The minimum Apache Kafka broker(s) version required is 0.10.0.0 but 0.11.x+ is recommended (tested with 2.5.0).
Resources:
- https://github.com/apache/kafka
- https://github.com/Shopify/sarama/blob/master/examples/http_server/http_server.go
- DIY
*/
// package-level variables for the sake of the example
// but you can define them inside your main func
// and pass around this config whenever you need to create a client or a producer or a consumer or use a cluster.
var (
// The Kafka brokers to connect to, as a comma separated list.
brokers = []string{getenv("KAFKA_1", "localhost:9092")}
// The config which makes our live easier when passing around, it pre-mades a lot of things for us.
config *sarama.Config
)
func getenv(key string, def string) string {
if value := os.Getenv(key); value != "" {
return value
}
return def
}
func init() {
config = sarama.NewConfig()
config.ClientID = "iris-example-client"
config.Version = sarama.V0_11_0_2
// config.Producer.RequiredAcks = sarama.WaitForAll // Wait for all in-sync replicas to ack the message.
config.Producer.Compression = sarama.CompressionSnappy
config.Producer.Flush.Frequency = 500 * time.Millisecond
config.Producer.Retry.Max = 10 // Retry up to 10 times to produce the message.
config.Producer.Return.Successes = true
// for SASL/basic plain text authentication: config.Net.SASL.
// config.Net.SASL.Enable = true
// config.Net.SASL.Handshake = false
// config.Net.SASL.User = "myuser"
// config.Net.SASL.Password = "mypass"
config.Consumer.Return.Errors = true
}
func main() {
app := iris.New()
app.OnErrorCode(iris.StatusNotFound, handleNotFound)
v1 := app.Party("/api/v1")
{
topicsAPI := v1.Party("/topics")
{
topicsAPI.Post("/", postTopicsHandler) // create a topic.
topicsAPI.Get("/", getTopicsHandler) // list all topics.
topicsAPI.Post("/{topic}/produce", postTopicProduceHandler) // store to a topic.
topicsAPI.Get("/{topic}/consume", getTopicConsumeSSEHandler) // retrieve all messages from a topic.
}
}
app.Get("/", docsHandler)
app.Logger().Infof("Brokers: %s", strings.Join(brokers, ", "))
// GET : http://localhost:8080
// POST, GET: http://localhost:8080/api/v1/topics
// POST : http://localhost:8080/api/v1/topics/{topic}/produce?key=my-key
// GET : http://localhost:8080/api/v1/topics/{topic}/consume?partition=0&offset=0
app.Listen(":8080")
}
// simple use-case, you can use templates and views obviously, see the "_examples/views" examples.
func docsHandler(ctx iris.Context) {
ctx.ContentType("text/html") // or ctx.HTML(fmt.Sprintf(...))
ctx.Writef(`<!DOCTYPE html>
<html>
<head>
<style>
th, td {
border: 1px solid black;
padding: 15px;
text-align: left;
}
</style>
</head>`)
defer ctx.Writef("</html>")
ctx.Writef("<body>")
defer ctx.Writef("</body>")
ctx.Writef(`
<table>
<tr>
<th>Method</th>
<th>Path</th>
<th>Handler</th>
</tr>
`)
defer ctx.Writef(`</table>`)
registeredRoutes := ctx.Application().GetRoutesReadOnly()
for _, r := range registeredRoutes {
if r.Path() == "/" { // don't list the root, current one.
continue
}
ctx.Writef(`
<tr>
<td>%s</td>
<td>%s%s</td>
<td>%s</td>
</tr>
`, r.Method(), ctx.Host(), r.Path(), r.MainHandlerName())
}
}
type httpError struct {
Code int `json:"code"`
Reason string `json:"reason,omitempty"`
}
func (h httpError) Error() string {
return fmt.Sprintf("Status Code: %d\nReason: %s", h.Code, h.Reason)
}
func fail(ctx iris.Context, statusCode int, format string, a ...interface{}) {
reason := "unspecified"
if format != "" {
reason = fmt.Sprintf(format, a...)
}
err := httpError{
Code: statusCode,
Reason: reason,
}
ctx.StopWithJSON(statusCode, err)
}
func handleNotFound(ctx iris.Context) {
suggestPaths := ctx.FindClosest(3)
if len(suggestPaths) == 0 {
ctx.WriteString("not found")
return
}
ctx.HTML("Did you mean?<ul>")
for _, s := range suggestPaths {
ctx.HTML(`<li><a href="%s">%s</a></li>`, s, s)
}
ctx.HTML("</ul>")
}
// Topic the payload for a kafka topic creation.
type Topic struct {
Topic string `json:"topic"`
Partitions int32 `json:"partitions"`
ReplicationFactor int16 `json:"replication"`
Configs []kv `json:"configs,omitempty"`
}
type kv struct {
Key string `json:"key"`
Value string `json:"value"`
}
func createKafkaTopic(t Topic) error {
cluster, err := sarama.NewClusterAdmin(brokers, config)
if err != nil {
return err
}
defer cluster.Close()
topicName := t.Topic
topicDetail := sarama.TopicDetail{
NumPartitions: t.Partitions,
ReplicationFactor: t.ReplicationFactor,
}
if len(t.Configs) > 0 {
topicDetail.ConfigEntries = make(map[string]*string, len(t.Configs))
for _, c := range t.Configs {
topicDetail.ConfigEntries[c.Key] = &c.Value // generate a ptr, or fill a new(string) with it and use that.
}
}
return cluster.CreateTopic(topicName, &topicDetail, false)
}
func postTopicsHandler(ctx iris.Context) {
var t Topic
err := ctx.ReadJSON(&t)
if err != nil {
fail(ctx, iris.StatusBadRequest,
"received invalid topic payload: %v", err)
return
}
// try to create the topic inside kafka.
err = createKafkaTopic(t)
if err != nil {
fail(ctx, iris.StatusInternalServerError,
"unable to create topic: %v", err)
return
}
ctx.StatusCode(iris.StatusCreated)
ctx.Writef("Topic %q created", t.Topic)
}
func getKafkaTopics() ([]string, error) {
client, err := sarama.NewClient(brokers, config)
if err != nil {
return nil, err
}
defer client.Close()
return client.Topics()
}
func getTopicsHandler(ctx iris.Context) {
topics, err := getKafkaTopics()
if err != nil {
fail(ctx, iris.StatusInternalServerError,
"unable to retrieve topics: %v", err)
return
}
ctx.JSON(topics)
}
func produceKafkaMessage(toTopic string, key string, value []byte) (partition int32, offset int64, err error) {
// On the broker side, you may want to change the following settings to get
// stronger consistency guarantees:
// - For your broker, set `unclean.leader.election.enable` to false
// - For the topic, you could increase `min.insync.replicas`.
producer, err := sarama.NewSyncProducer(brokers, config)
if err != nil {
return -1, -1, err
}
defer producer.Close()
// We are not setting a message key, which means that all messages will
// be distributed randomly over the different partitions.
return producer.SendMessage(&sarama.ProducerMessage{
Topic: toTopic,
Key: sarama.StringEncoder(key),
Value: sarama.ByteEncoder(value),
})
}
func postTopicProduceHandler(ctx iris.Context) {
topicName := ctx.Params().Get("topic")
key := ctx.URLParamDefault("key", "default")
// read the request data and store them as they are (not recommended in production ofcourse, do your own checks here).
body, err := ctx.GetBody()
if err != nil {
fail(ctx, iris.StatusUnprocessableEntity, "unable to read your data: %v", err)
return
}
partition, offset, err := produceKafkaMessage(topicName, key, body)
if err != nil {
fail(ctx, iris.StatusInternalServerError, "failed to store your data: %v", err)
return
}
// The tuple (topic, partition, offset) can be used as a unique identifier
// for a message in a Kafka cluster.
ctx.Writef("Your data is stored with unique identifier: %s/%d/%d", topicName, partition, offset)
}
type message struct {
Time time.Time `json:"time"`
Key string `json:"key"`
// Value []byte/json.RawMessage(if you are sure that you are sending only JSON) `json:"value"`
// or:
Value string `json:"value"` // for simple key-value storage.
}
func getTopicConsumeSSEHandler(ctx iris.Context) {
flusher, ok := ctx.ResponseWriter().Flusher()
if !ok {
ctx.StopWithText(iris.StatusHTTPVersionNotSupported, "streaming unsupported")
return
}
ctx.ContentType("application/json, text/event-stream")
ctx.Header("Cache-Control", "no-cache")
ctx.Header("Connection", "keep-alive")
master, err := sarama.NewConsumer(brokers, config)
if err != nil {
fail(ctx, iris.StatusInternalServerError, "unable to start master consumer: %v", err)
return
}
fromTopic := ctx.Params().Get("topic")
// take the partition, defaults to the first found if not url query parameter "partition" passed.
var partition int32
partitions, err := master.Partitions(fromTopic)
if err != nil {
master.Close()
fail(ctx, iris.StatusInternalServerError, "unable to get partitions for topic: '%s': %v", fromTopic, err)
return
}
if len(partitions) > 0 {
partition = partitions[0]
}
partition = ctx.URLParamInt32Default("partition", partition)
offset := ctx.URLParamInt64Default("offset", sarama.OffsetOldest)
consumer, err := master.ConsumePartition(fromTopic, partition, offset)
if err != nil {
ctx.Application().Logger().Error(err)
master.Close() // close the master here to avoid any leaks, we will exit.
fail(ctx, iris.StatusInternalServerError, "unable to start partition consumer: %v", err)
return
}
// `OnClose` fires when the request is finally done (all data read and handler exits) or interrupted by the user.
ctx.OnClose(func(_ iris.Context) {
ctx.Application().Logger().Warnf("a client left")
// Close shuts down the consumer. It must be called after all child
// PartitionConsumers have already been closed. <-- That is what
// godocs says but it doesn't work like this.
// if err = consumer.Close(); err != nil {
// ctx.Application().Logger().Errorf("[%s] unable to close partition consumer: %v", ctx.RemoteAddr(), err)
// }
// so close the master only and omit the first ^ consumer.Close:
if err = master.Close(); err != nil {
ctx.Application().Logger().Errorf("[%s] unable to close master consumer: %v", ctx.RemoteAddr(), err)
}
})
for {
select {
case consumerErr, ok := <-consumer.Errors():
if !ok {
return
}
ctx.Writef("data: error: {\"reason\": \"%s\"}\n\n", consumerErr.Error())
flusher.Flush()
case incoming, ok := <-consumer.Messages():
if !ok {
return
}
msg := message{
Time: incoming.Timestamp,
Key: string(incoming.Key),
Value: string(incoming.Value),
}
b, err := json.Marshal(msg)
if err != nil {
ctx.Application().Logger().Error(err)
continue
}
ctx.Writef("data: %s\n\n", b)
flusher.Flush()
}
}
}