-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
77 lines (65 loc) · 1.48 KB
/
types.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
package katsuragi
import (
"container/list"
"net/http"
"sync"
"time"
"golang.org/x/net/html"
)
type FetcherProps struct {
UserAgent string
Timeout time.Duration //ms
CacheCap int
}
type Fetcher struct {
cache map[string]*list.Element
lruList *list.List
mu sync.RWMutex
props FetcherProps
}
var defaultFetcherProps = FetcherProps{
Timeout: 3000 * time.Millisecond,
CacheCap: 10,
}
func NewFetcher(props *FetcherProps) *Fetcher {
if props == nil {
props = &defaultFetcherProps
} else {
// Set default values for unspecified fields
if props.Timeout == 0 {
props.Timeout = defaultFetcherProps.Timeout
}
if props.CacheCap == 0 {
props.CacheCap = defaultFetcherProps.CacheCap
}
}
return &Fetcher{
cache: make(map[string]*list.Element),
lruList: list.New(),
props: *props,
}
}
type GetLinksProps struct {
Url string
Category string
}
type DomainParts struct {
Subdomain string
Root string
TLD string
}
type cacheEntry struct {
url string
response *html.Node
isError bool
err error
}
// HTTP Client
type UserAgentTransport struct {
UserAgent string
Transport http.RoundTripper
}
func (t *UserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", t.UserAgent)
return t.Transport.RoundTrip(req)
}