-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiHandler.go
65 lines (54 loc) · 1.5 KB
/
multiHandler.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
package main
import (
"fmt"
"net/http"
"regexp"
)
type MultiResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Results map[string]int `json:"results"`
Messages []string `json:"messages"`
Notice string `json:"notice"`
AsOf string `json:"as_of"`
}
var re = regexp.MustCompile("[ ,\t\n\r]+")
func multiHandler(w http.ResponseWriter, r *http.Request) {
retVal := MultiResponse{}
retVal.Notice = "Free for light, non-commericial use. For heavy or commercial use, please contact us."
retVal.Results = make(map[string]int)
input := r.PostFormValue("domains")
if input == "" {
input = r.URL.Query().Get("domains")
}
if input == "" {
retVal.Success = false
retVal.Message = "domains query parameter is required"
handleJson(w, r, retVal)
return
}
domains := re.Split(input, -1)
for _, rawDomain := range domains {
if rawDomain == "" {
continue
}
domain, pureErr := purifyDomain(rawDomain)
if pureErr != nil {
retVal.Messages = append(retVal.Messages, fmt.Sprintf("Error: %s does not appear to be valid (%s)", rawDomain, pureErr.Error()))
continue
}
if rawDomain != domain {
retVal.Messages = append(retVal.Messages, fmt.Sprintf("Warning: %s was purified to %s", rawDomain, domain))
}
rank, ok := rankings[domain]
if ok {
retVal.Results[domain] = rank
} else {
retVal.Results[domain] = -1
}
}
retVal.AsOf = rankDate
retVal.Success = true
retVal.Message = "OK"
handleJson(w, r, retVal)
}