-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (106 loc) · 2.51 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
package main
import (
"github.com/adaptant-labs/geo/georeverse"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/oschwald/maxminddb-golang"
"github.com/urfave/cli"
"log"
"net/http"
"os"
"strconv"
)
var (
reverser *georeverse.CountryReverser
geodb *maxminddb.Reader
)
type ConsulConfig struct {
enabled bool
agentAddress string
}
type ServiceConfig struct {
consul *ConsulConfig
dataPath string
host string
port int
}
func defaultServiceConfig() *ServiceConfig {
return &ServiceConfig{
port: 4041,
host: "",
dataPath: "./data/polygons.properties",
consul: &ConsulConfig{
enabled: true,
},
}
}
func main() {
var err error
config := defaultServiceConfig()
app := cli.NewApp()
app.EnableBashCompletion = true
app.Name = "Reverse Geocoding Service"
app.Usage = "Microservice for providing ISO 3166-1 country code lookup from a lat/lng pair"
app.Email = "labs@adaptant.io"
app.Version = "0.0.2"
app.Author = "Adaptant Labs"
app.Copyright = "Adaptant Solutions AG"
app.Flags = []cli.Flag {
cli.IntFlag{
Name: "port",
Value: config.port,
Usage: "Port to bind to",
Destination: &config.port,
},
cli.StringFlag{
Name: "host",
Usage: "Host address to bind to",
Value: config.host,
Destination: &config.host,
},
cli.StringFlag{
Name: "data",
Usage: "Polygon definition file to use",
Value: config.dataPath,
Destination: &config.dataPath,
},
cli.BoolTFlag{
Name: "use-consul",
Usage: "Use Consul for Service Registration",
Destination: &config.consul.enabled,
},
cli.StringFlag{
Name: "consul-agent",
Usage: "Consul Agent to connect to",
Value: "localhost:8500",
Destination: &config.consul.agentAddress,
},
}
app.Action = func(c *cli.Context) error {
reverser, err = georeverse.NewCountryReverser(config.dataPath)
if err != nil {
return err
}
geodb, err = maxminddb.Open("data/GeoLite2-Country.mmdb")
if err != nil {
return err
}
if config.consul.enabled == true {
err = ConsulServiceRegister(config)
if err != nil {
return err
}
}
router := mux.NewRouter()
router.HandleFunc("/georeverse", georeverseHandler)
router.HandleFunc("/georeverse/{ipAddress}", georeverseIPHandler)
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
addr := config.host + ":" + strconv.Itoa(config.port)
log.Printf("Listening on %s/georeverse...", addr)
return http.ListenAndServe(addr, loggedRouter)
}
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}