forked from buffalo-news/geo.bntech.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.go
87 lines (68 loc) · 1.62 KB
/
geo.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"syscall"
geoip2 "github.com/oschwald/geoip2-golang"
)
// Defining the settings structure
type settings struct {
License string `json:license,omitempty`
}
// Settings for the application are global so we can access them from anywhere
var Settings settings
var sigchan = make(chan os.Signal, 1)
var run = true
var db *geoip2.Reader
var httpCloser io.Closer
func main() {
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
// Initialize anything the API needs
initialize()
// Update the db to the newest copy
updateDB()
var err error
db, err = geoip2.Open("maxmind/GeoLite2-City.mmdb")
if err != nil {
serverLog("error parsing db\n")
os.Exit(1)
}
defer db.Close()
// Start the API
go httpServer()
// Run until shutdown is signaled, then close
for run {
select {
case sig := <-sigchan:
serverLog("Caught signal %v: terminating\n", sig)
run = false
}
}
httpCloser.Close()
}
func initialize() {
// Open our settingFile
settingsFile, err := os.Open("./settings.json")
// If we os.Open returns an error then handle it
if err != nil {
serverLog(err.Error())
os.Exit(1)
}
// Read the settings into the global settings
settings, _ := ioutil.ReadAll(settingsFile)
err = json.Unmarshal(settings, &Settings)
// If there is an error in the json structure handle it
if err != nil {
serverLog(err.Error())
os.Exit(1)
}
// Defer the closing of our settingFile so that we can parse it later on
defer settingsFile.Close()
}
func serverLog(format string, a ...interface{}) {
fmt.Fprintf(os.Stdout, format, a...)
}