-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
66 lines (52 loc) · 1.08 KB
/
url.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
// Package url loads changesets from a url
package url
import (
"io/ioutil"
"net/http"
"time"
"github.com/balachandar-git/jet-config/source"
)
type urlSource struct {
url string
opts source.Options
}
var (
DefaultURL = "http://localhost:8080/config"
)
func (u *urlSource) Read() (*source.ChangeSet, error) {
rsp, err := http.Get(u.url)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
ft := format(rsp.Header.Get("Content-Type"))
if len(ft) == 0 {
ft = u.opts.Encoder.String()
}
cs := &source.ChangeSet{
Data: b,
Format: ft,
Timestamp: time.Now(),
Source: u.String(),
}
cs.Checksum = cs.Sum()
return cs, nil
}
func (u *urlSource) Watch() (source.Watcher, error) {
return newWatcher(u)
}
func (u *urlSource) String() string {
return "url"
}
func NewSource(opts ...source.Option) source.Source {
options := source.NewOptions(opts...)
url, ok := options.Context.Value(urlKey{}).(string)
if !ok {
url = DefaultURL
}
return &urlSource{url: url, opts: options}
}