-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (125 loc) · 3.91 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
package main
import (
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/everesio/buddy/consumers"
"github.com/everesio/buddy/controller"
"github.com/everesio/buddy/producers"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
)
var version = "Unknown"
var params struct {
httpAddr string
debugAddr string
producer string
consumer string
debug bool
syncInterval int
jsonLog bool
}
func init() {
kingpin.Flag("http-addr", "HTTP listen address").Default(":8080").StringVar(¶ms.httpAddr)
kingpin.Flag("debug-addr", "Debug listen address").Default(":8081").StringVar(¶ms.debugAddr)
kingpin.Flag("producer", "The endpoints producer to use.").Default("google").StringVar(¶ms.producer)
kingpin.Flag("consumer", "The endpoints consumer to use.").Default("google").StringVar(¶ms.consumer)
kingpin.Flag("debug", "Enable debug logging.").BoolVar(¶ms.debug)
kingpin.Flag("sync-interval", "Sync interval in seconds.").Default("15").IntVar(¶ms.syncInterval)
kingpin.Flag("json-log", "Enable json log formatter.").BoolVar(¶ms.jsonLog)
}
func main() {
kingpin.Version(version)
kingpin.Parse()
var formatter log.Formatter
if params.jsonLog {
formatter = &log.JSONFormatter{}
} else {
formatter = &log.TextFormatter{FullTimestamp: true}
}
log.SetFormatter(formatter)
if params.debug {
log.SetLevel(log.DebugLevel)
}
log.Info("Starting buddy")
producer, err := producers.New(params.producer)
if err != nil {
log.Fatalf("Error creating producer: %v", err)
}
consumer, err := consumers.NewSynced(params.consumer)
if err != nil {
log.Fatalf("Error creating consumer: %v", err)
}
errc := make(chan error)
// Interrupt handler.
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
opts := &controller.Options{
SyncInterval: time.Duration(params.syncInterval) * time.Second,
}
ctrl := controller.New(producer, consumer, opts, errc)
// Debug listener.
go func() {
m := http.NewServeMux()
m.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
m.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
m.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
m.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
m.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
log.Info("Debug addr ", params.debugAddr)
errc <- http.ListenAndServe(params.debugAddr, m)
}()
// HTTP transport.
go func() {
http.Handle("/metrics", promhttp.Handler())
http.Handle("/endpoints", endpointsHandler(producer))
http.Handle("/records", recordsHandler(producer, consumer))
http.Handle("/sync", syncHandler(ctrl))
log.Info("HTTP addr ", params.httpAddr)
errc <- http.ListenAndServe(params.httpAddr, nil)
}()
// Controller.
go func() {
ctrl.Run()
}()
// Run!
log.Info("exit", <-errc)
}
func endpointsHandler(producer producers.Producer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
endpoints, err := producer.Endpoints()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(endpoints)
})
}
func recordsHandler(producer producers.Producer, consumer consumers.Consumer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
records, err := consumer.Records(producer.ComputeZones())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(records)
})
}
func syncHandler(controller *controller.Controller) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
err := controller.Synchronize()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}