-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiHandler.go
52 lines (43 loc) · 1.09 KB
/
apiHandler.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
package main
import "net/http"
type ApiResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Domain string `json:"domain"`
Input string `json:"input"`
Rank int `json:"rank"`
Notice string `json:"notice"`
AsOf string `json:"as_of"`
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
retVal := ApiResponse{}
retVal.Notice = "Free for light, non-commericial use. For heavy or commercial use, please contact us."
input := r.URL.Query().Get("domain")
if input == "" {
retVal.Success = false
retVal.Message = "domain query parameter is required"
handleJson(w, r, retVal)
return
}
retVal.Input = input
domain, pureErr := purifyDomain(input)
if pureErr != nil {
retVal.Success = false
retVal.Message = pureErr.Error()
handleJson(w, r, retVal)
return
}
retVal.Domain = domain
rank, ok := rankings[domain]
if !ok {
retVal.Success = false
retVal.Message = "Domain not ranked"
handleJson(w, r, retVal)
return
}
retVal.AsOf = rankDate
retVal.Success = true
retVal.Message = "OK"
retVal.Rank = rank
handleJson(w, r, retVal)
}