-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotefile_input.go
76 lines (62 loc) · 1.26 KB
/
remotefile_input.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
package config
import (
"io"
"log"
"net/http"
"time"
)
type RemoteFileInput struct {
option InputOption
done chan struct{}
}
func NewRemoteFileInput(option InputOption) (Inputer, error) {
return &RemoteFileInput{option: option, done: make(chan struct{})}, nil
}
func (i *RemoteFileInput) Read() (Content, error) {
c := Content{}
resp, err := http.Get(i.option.Path)
if err != nil {
return c, err
}
defer resp.Body.Close()
c.ModTime = i.ParseModTime(resp)
if c.Body, err = io.ReadAll(resp.Body); err != nil {
return c, err
}
return c, nil
}
func (i *RemoteFileInput) ParseModTime(resp *http.Response) time.Time {
modTime := time.Now()
keys := map[string]string{
"date": http.TimeFormat,
"last-modified": http.TimeFormat,
}
for k, format := range keys {
vv := resp.Header.Get(k)
if vv == "" {
continue
}
mtime, err := time.Parse(format, vv)
if err != nil {
continue
}
return mtime
}
return modTime
}
func (i *RemoteFileInput) Watch(cb func(Inputer)) error {
ticker := time.NewTicker(i.option.PollInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
case <-i.done:
log.Println("Watcher Done,", i.option.Path)
return nil
}
cb(i)
}
}
func (i *RemoteFileInput) Close() {
close(i.done)
}