forked from AfterShip/email-verifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
69 lines (57 loc) · 1.41 KB
/
handler.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
package emailverifier
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// updateDisposableDomains gets domains data from source's URL
func updateDisposableDomains(source string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequest("GET", source, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("get disposable domains from %s with status_code: %d", source, resp.StatusCode)
}
var domains []string
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if len(content) == 0 {
return nil
}
if err = json.Unmarshal(content, &domains); err != nil {
return err
}
newDomains := make(map[string]struct{})
for _, v := range domains {
newDomains[v] = struct{}{}
}
// clear up invalid disposable domains
disposableSyncDomains.Range(func(key, value interface{}) bool {
if _, exists := newDomains[key.(string)]; !exists {
disposableSyncDomains.Delete(key)
}
return true
})
// update new domain data
for _, d := range domains {
disposableSyncDomains.Store(d, struct{}{})
}
// add additionalDisposableDomains again
for d := range additionalDisposableDomains {
disposableSyncDomains.Store(d, struct{}{})
}
return nil
}