-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
95 lines (83 loc) · 2.65 KB
/
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
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
package tinygeoip
import (
"encoding/json"
"fmt"
"net"
"net/http"
"strings"
"time"
)
// DefaultOriginPolicy is the default for `Access-Control-Allow-Origin` header.
const DefaultOriginPolicy = "*"
// HTTPHandler implements a standard http.Handler interface for accessing
// a LookupDB.
type HTTPHandler struct {
// Handle to the LookupDB used for queries.
DB *LookupDB
// Value for `Access-Control-Allow-Origin` header.
//
// Header will be omitted if set to zero value.
OriginPolicy string
}
// NewHTTPHandler creates a HTTPHandler for requests against the given LookupDB.
//
// By default caching is enabled, and DefaultOriginPolicy is applied.
func NewHTTPHandler(db *LookupDB) *HTTPHandler {
return &HTTPHandler{
DB: db,
OriginPolicy: DefaultOriginPolicy,
}
}
// SetOriginPolicy sets value for `Access-Control-Allow-Origin` header.
//
// Returns pointer to the HTTPHandler to enable chaining in builder pattern.
func (hh *HTTPHandler) SetOriginPolicy(origins string) *HTTPHandler {
hh.OriginPolicy = origins
return hh
}
// ServeHTTP implements the standard http.Handler interface.
func (hh *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Set headers
if hh.OriginPolicy != "" {
w.Header().Set("Access-Control-Allow-Origin", hh.OriginPolicy)
}
w.Header().Set("Content-Type", "application/json")
// attempt to parse IP from query
ipText := strings.TrimPrefix(r.URL.Path, "/")
// nice error message when missing data
if ipText == "" {
w.WriteHeader(http.StatusBadRequest)
const parseIPError = `{"error": "missing IP query in path, try /192.168.1.1"}`
w.Write([]byte(parseIPError))
return
}
// attempt to parse the provided IP address
ip := net.ParseIP(ipText)
if ip == nil {
w.WriteHeader(http.StatusBadRequest)
const parseIPError = `{"error": "could not parse invalid IP address"}`
w.Write([]byte(parseIPError))
return
}
// do a DB lookup on the IP address
loc, err := hh.DB.Lookup(ip)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf(`{"error": "%v"}`, err.Error())))
return
}
// return results as JSON
//
// (yes, we're swallowing a potential marshal error here, but we already
// know loc should not be nil since we checked for err on the previous case)
b, _ := json.Marshal(loc)
w.Header().Set("Last-Modified", serverStartTime)
w.Write(b)
}
// for the last-modified time to hint to HTTP caching of results, we just use
// program launch time, as the values will never change outside of that. (we
// don't use the underlying database build time because the program itself may
// be modified.)
var (
serverStartTime = time.Now().Format(http.TimeFormat)
)