-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
136 lines (108 loc) · 3.08 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
package main
import (
"fmt"
"github.com/InVisionApp/go-health"
"github.com/micro/go-micro"
"github.com/oschwald/geoip2-golang"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/InVisionApp/go-health/handlers"
geoip "github.com/ProtocolONE/geoip-service/pkg"
"github.com/ProtocolONE/geoip-service/pkg/proto"
prometheus_plugin "github.com/ProtocolONE/go-micro-plugins/wrapper/monitoring/prometheus"
"github.com/kelseyhightower/envconfig"
"github.com/micro/go-plugins/client/selector/static"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/Clever/pathio.v3"
)
type Config struct {
GeoIpDbPath string `envconfig:"MAXMIND_GEOIP_DB_PATH" required:"true"`
MetricsPort int `envconfig:"METRICS_PORT" required:"false" default:"8080"`
MicroSelector string `envconfig:"MICRO_SELECTOR" required:"false"`
}
type customHealthCheck struct{}
func main() {
cfg := &Config{}
if err := envconfig.Process("", cfg); err != nil {
log.Fatalf("Config init failed with error: %s\n", err)
}
pathioReader, err := pathio.Reader(cfg.GeoIpDbPath)
if err != nil {
log.Fatal(err)
}
geoipBuf, err := ioutil.ReadAll(pathioReader)
if err != nil {
log.Fatal(err)
}
db, err := geoip2.FromBytes(geoipBuf)
if err != nil {
log.Fatal(err)
}
defer func() {
err := db.Close()
if err != nil {
log.Fatal(err)
}
}()
dbMeta := db.Metadata()
dbBuildDate := time.Unix(int64(dbMeta.BuildEpoch), 0).UTC()
dbInfo := "Loaded database info:\n" +
"\tFilename: %s\n" +
"\tVersion: %d.%d\n" +
"\tType: %s\n" +
"\tBuild date: %s\n"
log.Printf(dbInfo, cfg.GeoIpDbPath, dbMeta.BinaryFormatMajorVersion, dbMeta.BinaryFormatMinorVersion, dbMeta.DatabaseType, dbBuildDate)
log.Println("Initialize micro service")
options := []micro.Option{
micro.Name(geoip.ServiceName),
micro.Version(geoip.Version),
micro.WrapHandler(prometheus_plugin.NewHandlerWrapper()),
}
if cfg.MicroSelector == "static" {
log.Println(`Use micro selector "static"`)
options = append(options, micro.Selector(static.NewSelector()))
}
service := micro.NewService(options...)
service.Init()
err = proto.RegisterGeoIpServiceHandler(service.Server(), &geoip.Service{GeoReader: db})
if err != nil {
log.Fatal(err)
}
initHealth(cfg)
initPrometheus()
go func() {
if err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.MetricsPort), nil); err != nil {
log.Fatal("Metrics listen failed")
}
}()
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
func initHealth(cfg *Config) {
h := health.New()
err := h.AddChecks([]*health.Config{
{
Name: "health-check",
Checker: &customHealthCheck{},
Interval: time.Duration(1) * time.Second,
Fatal: true,
},
})
if err != nil {
log.Fatal("Health check register failed")
}
log.Printf("Health check listening on :%d", cfg.MetricsPort)
if err = h.Start(); err != nil {
log.Fatal("Health check start failed")
}
http.HandleFunc("/health", handlers.NewJSONHandlerFunc(h, nil))
}
func initPrometheus() {
http.Handle("/metrics", promhttp.Handler())
}
func (c *customHealthCheck) Status() (interface{}, error) {
return "ok", nil
}