-
Notifications
You must be signed in to change notification settings - Fork 10
/
namecheck.go
49 lines (41 loc) · 1.18 KB
/
namecheck.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
package namecheck
import (
"context"
"errors"
"net/http"
"strings"
"time"
"github.com/haritsfahreza/namecheck/util"
)
func check(ctx context.Context, name, URL string, ct ChannelType) (ChannelStatus, error) {
resp, err := util.HTTPClient.Get(strings.Replace(URL, "{name}", name, 1))
if err != nil {
if strings.Contains(err.Error(), "no such host") {
return StatusAvailable, nil
} else if strings.Contains(err.Error(), "Timeout") {
return StatusUnknown, errors.New("Timeout")
}
return StatusUnknown, err
}
if resp.StatusCode == http.StatusOK {
return StatusNotAvailable, nil
}
return StatusAvailable, nil
}
//Check name availability on all channel
func Check(ctx context.Context, name string, baseChannel []*Channel) (channels []*Channel, duration time.Duration) {
start := time.Now()
length := len(baseChannel)
resultCh := make(chan *Channel, length)
for _, channel := range baseChannel {
go func(ch *Channel) {
newChannel := ch
newChannel.Status, newChannel.Error = check(ctx, name, ch.URL, ch.Type)
resultCh <- newChannel
}(channel)
}
for i := 0; i < length; i++ {
channels = append(channels, <-resultCh)
}
return channels, time.Since(start)
}