-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
123 lines (100 loc) · 2.65 KB
/
server.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
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"log"
"net"
"net/http"
"strconv"
"github.com/searchinform/cache"
"github.com/searchinform/provider"
)
var (
configPath string
)
func init() {
flag.StringVar(&configPath, "c", "conf.json", "config filepath")
}
// Controller - main struct with all dependences
type Controller struct {
cache cache.Cache
providers provider.Iterator
client HTTPClient
logger log.Logger
}
// Init run all background jobs
func (ctrl *Controller) Init() {
go cache.Cleaner(context.Background(), &ctrl.cache)
}
func (ctrl *Controller) error(w http.ResponseWriter, msg string, code int) {
ctrl.logger.Println(msg)
http.Error(w, msg, code)
}
func lookup(host string) (addr string, err error) {
addrs, err := net.LookupHost(host)
if err != nil {
return "", err
}
if len(addrs) == 0 {
return "", errors.New("empty addrs list")
}
return addrs[0], nil
}
// resolve returns country of this host
func (ctrl *Controller) resolve(host string) (string, error) {
addr, err := lookup(host)
if err != nil {
return "", errors.New("host lookup err : " + err.Error())
}
if country, ok := ctrl.cache.Get(addr); ok {
ctrl.logger.Printf("Resolve [%v]: addr [%v]: cache hit: country is `%v`", host, addr, country)
return country, nil
}
provider, err := ctrl.providers.Next()
if err != nil {
return "", errors.New("providers iter err : " + err.Error())
}
country, err := ctrl.client.Resolve(provider, addr)
if err != nil {
return "", errors.New("http client err : " + err.Error())
}
ctrl.cache.Insert(addr, country)
ctrl.logger.Printf("Resolve [%v]: addr [%v]: provider [%v]: country `%v`", host, addr, provider.Name, country)
return country, nil
}
// CountryByIP ..
func (ctrl *Controller) CountryByIP(w http.ResponseWriter, r *http.Request) {
host := r.FormValue("host")
if host == "" {
host = r.Host
}
country, err := ctrl.resolve(host)
if err != nil {
ctrl.error(w, "Resolve err: "+err.Error(), http.StatusInternalServerError)
return
}
body := &struct {
Host string `json:"host"`
Country string `json:"country"`
}{Host: host, Country: country}
json.NewEncoder(w).Encode(body)
}
func main() {
flag.Parse()
log.Println("Parsing config file", configPath)
conf, err := ParseConfig(configPath)
if err != nil {
log.Fatalln("Parse err:", err)
}
ctrl := NewFactory(conf).NewController()
ctrl.Init()
router := http.NewServeMux()
router.HandleFunc("/api/country", ctrl.CountryByIP)
port := conf.HTTP.Port
log.Printf("Server start on %v port...\n", port)
if err := http.ListenAndServe(":"+strconv.Itoa(port), router); err != nil {
log.Fatalln("ListenAndServe err:", err)
}
}