forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
56 lines (47 loc) · 1.06 KB
/
create.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
package service
import (
"fmt"
"net/http"
"os"
"os/exec"
"time"
"github.com/NYTimes/gizmo/server"
)
// CreateStream is a JSON endpoint for creating a new topic in Kafka.
func (s *StreamService) CreateStream(r *http.Request) (int, interface{}, error) {
id := time.Now().Unix()
topic := topicName(id)
err := createTopic(topic)
if err != nil {
return http.StatusInternalServerError, nil, jsonErr{err}
}
server.LogWithFields(r).WithField("topic", topic).Info("created new topic")
return http.StatusOK, struct {
Status string `json:"status"`
StreamID int64 `json:"stream_id"`
}{"success!", id}, nil
}
func topicName(id int64) string {
return fmt.Sprintf("stream-%d", id)
}
func createTopic(name string) error {
cmd := exec.Command("kafka-topics.sh",
"--create",
"--zookeeper",
"localhost:2181",
"--replication-factor",
"1",
"--partition",
"1",
"--topic",
name)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}
type jsonErr struct {
Err error `json:"error"`
}
func (e jsonErr) Error() string {
return e.Err.Error()
}