-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers_http.go
48 lines (35 loc) · 991 Bytes
/
handlers_http.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
package geoipfix
import (
"net"
"net/http"
"go.uber.org/zap"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/pkg/errors"
)
type httpHandler struct {
options
}
// GetLocation retrieves the IP from request.
func (h *httpHandler) GetLocation(w http.ResponseWriter, r *http.Request) error {
rawIP := chi.URLParam(r, "ipAddress")
if rawIP == "" {
rawIP = r.RemoteAddr
}
log := h.Logger.With(zap.String("ip_address", rawIP))
log.Info("Retrieve IP Address from request", zap.String("ip_address", rawIP))
ip := net.ParseIP(rawIP)
if ip == nil {
log.Error("IP Address cannot be parsed", zap.String("ip_address", rawIP))
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return nil
}
q := geoipQuery{}
err := h.DB.Lookup(ip, &q)
if err != nil {
return errors.Wrapf(err, "Cannot retrieve geoip information for %s", rawIP)
}
resp := q.Record(ip, r.Header.Get("Accept-Language"))
render.JSON(w, r, resp)
return nil
}