-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (98 loc) · 3 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"github.com/kakkoyun/kafkaques/consumer"
"github.com/kakkoyun/kafkaques/kafkaques"
"github.com/kakkoyun/kafkaques/producer"
"github.com/metalmatze/signal/internalserver"
"github.com/alecthomas/kong"
"github.com/common-nighthawk/go-figure"
"github.com/go-kit/log/level"
"github.com/metalmatze/signal/healthcheck"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
)
var (
version string
commit string
date string
builtBy string
)
type Flags struct {
LogLevel string `default:"info" enum:"error,warn,info,debug" help:"log level."`
Address string `default:":8080" help:"Address string for internal server"`
Produce struct {
Brokers []string `kong:"required,help='Brokers.'"`
Topic string `kong:"required,arg,name='topic',help='Topic push messages to.'"`
} `cmd:"" help:"Consumer messages"`
Consume struct {
Brokers []string `kong:"required,help='Brokers.'"`
Topic string `kong:"required,arg,name='topic',help='Topic to receive messages from.'"`
Group string `kong:"help='Group.'"`
} `cmd:"" help:"Produce messages"`
}
func main() {
flags := &Flags{}
kongCtx := kong.Parse(flags)
serverStr := figure.NewColorFigure("Kafkaques", "roman", "yellow", true)
serverStr.Print()
logger := kafkaques.NewLogger(flags.LogLevel, kafkaques.LogFormatLogfmt, "kafkaques")
level.Debug(logger).Log("msg", "kafkaques initialized",
"version", version,
"commit", commit,
"date", date,
"builtBy", builtBy,
"config", fmt.Sprint(flags),
)
registry := prometheus.NewRegistry()
healthchecks := healthcheck.NewMetricsHandler(healthcheck.NewHandler(), registry)
h := internalserver.NewHandler(
internalserver.WithHealthchecks(healthchecks),
internalserver.WithPrometheusRegistry(registry),
internalserver.WithPProf(),
)
s := http.Server{
Addr: flags.Address,
Handler: h,
}
var g run.Group
ctx, cancel := context.WithCancel(context.Background())
switch kongCtx.Command() {
case "produce <topic>":
g.Add(func() error {
return producer.Run(ctx, logger, flags.Produce.Brokers, flags.Produce.Topic)
}, func(error) {
cancel()
})
case "consume <topic>":
g.Add(func() error {
return consumer.Run(ctx, logger, flags.Consume.Brokers, flags.Consume.Group, flags.Consume.Topic)
}, func(error) {
cancel()
})
default:
level.Error(logger).Log("err", "unknown command", "cmd", kongCtx.Command())
os.Exit(1)
}
g.Add(func() error {
level.Info(logger).Log("msg", "starting internal HTTP server", "address", s.Addr)
return s.ListenAndServe()
}, func(err error) {
_ = s.Shutdown(context.Background())
})
g.Add(run.SignalHandler(ctx, os.Interrupt, os.Kill))
if err := g.Run(); err != nil {
var e run.SignalError
if errors.As(err, &e) {
level.Error(logger).Log("msg", "program exited with signal", "err", err, "signal", e.Signal)
} else {
level.Error(logger).Log("msg", "program exited with error", "err", err)
}
os.Exit(1)
}
level.Info(logger).Log("msg", "exited")
}