forked from itspage/go-platform
/
source.go
64 lines (53 loc) · 1.23 KB
/
source.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
package config
import (
"time"
proto "github.com/micro/config-srv/proto/config"
"github.com/micro/go-micro/client"
"golang.org/x/net/context"
)
type source struct {
opts SourceOptions
client proto.ConfigClient
}
func (s *source) Read() (*ChangeSet, error) {
rsp, err := s.client.Read(context.TODO(), &proto.ReadRequest{
Id: s.opts.Name,
})
if err != nil {
return nil, err
}
return &ChangeSet{
Timestamp: time.Unix(rsp.Change.ChangeSet.Timestamp, 0),
Data: []byte(rsp.Change.ChangeSet.Data),
Checksum: rsp.Change.ChangeSet.Checksum,
Source: rsp.Change.ChangeSet.Source,
}, nil
}
func (s *source) String() string {
return "os"
}
func (s *source) Watch() (SourceWatcher, error) {
stream, err := s.client.Watch(context.TODO(), &proto.WatchRequest{
Id: s.opts.Name,
})
if err != nil {
return nil, err
}
return &sourceWatcher{stream}, nil
}
func NewSource(opts ...SourceOption) Source {
var options SourceOptions
for _, o := range opts {
o(&options)
}
if len(options.Name) == 0 {
options.Name = DefaultSourceName
}
if options.Client == nil {
options.Client = client.DefaultClient
}
return &source{
opts: options,
client: proto.NewConfigClient("go.micro.srv.config", options.Client),
}
}