-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
76 lines (65 loc) · 2.07 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
_ "net/http/pprof"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/siggy/gographs/pkg/cache"
"github.com/siggy/gographs/pkg/graph"
"github.com/siggy/gographs/pkg/web"
log "github.com/sirupsen/logrus"
)
const (
targetAll = "all"
targetWeb = "web"
targetGraph = "graph"
)
func main() {
target := flag.String("target", targetAll, fmt.Sprintf("program target, must be one of: %s, %s, %s", targetAll, targetWeb, targetGraph))
webAddr := flag.String("addr", "localhost:8888", "web address to listen on")
graphAddr := flag.String("graph-addr", graph.DefaultGraphAddr, "graph address to listen on")
logLevel := flag.String("log-level", log.DebugLevel.String(), "log level, must be one of: panic, fatal, error, warn, info, debug, trace")
metricsAddr := flag.String("metrics-addr", "localhost:8080", "address to listen on for metrics requests")
redisAddr := flag.String("redis-addr", "localhost:6379", "address to connect to redis")
flag.Parse()
level, err := log.ParseLevel(*logLevel)
if err != nil {
log.Fatalf("invalid log-level: %s", *logLevel)
}
log.SetLevel(level)
http.Handle("/metrics", promhttp.Handler())
go func() {
log.Infof("metrics server listening on %s", *metricsAddr)
err = http.ListenAndServe(*metricsAddr, nil)
if err != nil {
log.Fatalf("failed to listen on metrics address [%s]: %s", *metricsAddr, err)
}
}()
if *target == targetAll || *target == targetGraph {
go func() {
err := graph.Start(*graphAddr)
if err != nil {
log.Fatalf("failed to start graph server [%s]: %s", *webAddr, err)
}
}()
}
if *target == targetAll || *target == targetWeb {
go func() {
c, err := cache.New(*redisAddr)
if err != nil {
log.Fatalf("failed to initialize cache: %s", err)
}
graph := graph.NewClient(*graphAddr)
err = web.Start(c, *webAddr, graph)
if err != nil {
log.Fatalf("failed to start web server [%s]: %s", *webAddr, err)
}
}()
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
<-signalChan
}