-
Notifications
You must be signed in to change notification settings - Fork 1
/
source_url.go
137 lines (118 loc) · 3.18 KB
/
source_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2019 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gconf
import (
"fmt"
"io"
"net/http"
neturl "net/url"
"strings"
"time"
)
var errNoContentType = fmt.Errorf("http response has no the header Content-Type")
// NewURLSource returns a url source to read the configuration data
// from the url by the stdlib http.Get(url).
//
// The header "Content-Type" indicates the data format, that's, it will split
// the value by "/" and use the last part, such as "application/json" represents
// the format "json". But you can set format to override it.
//
// The url source can watch the configuration data from the url each interval
// period. If interval is equal to 0, it is defaulted to time.Minute.
func NewURLSource(url string, interval time.Duration, format ...string) Source {
if url == "" {
panic("the url must not be nil")
} else if _, err := neturl.Parse(url); err != nil {
panic(err)
}
var _format string
if len(format) > 0 && format[0] != "" {
_format = format[0]
}
if interval <= 0 {
interval = time.Minute
}
return urlSource{
id: fmt.Sprintf("url:%s", url),
url: url,
format: _format,
period: interval,
}
}
type urlSource struct {
id string
url string
format string
period time.Duration
}
func (u urlSource) String() string { return u.id }
func (u urlSource) Read() (DataSet, error) {
resp, err := http.Get(u.url)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return DataSet{Source: u.id, Format: u.format}, err
}
format := u.format
if format == "" {
// Get the format from the header "Content-Type".
ct := strings.TrimSpace(resp.Header.Get("Content-Type"))
if index := strings.IndexByte(ct, ';'); index > 0 {
ct = strings.TrimSpace(ct[:index])
}
if index := strings.LastIndexByte(ct, '/'); index > 0 {
ct = ct[index+1:]
}
if ct == "" {
return DataSet{Source: u.id}, errNoContentType
}
format = ct
}
// Read the body of the response.
data, err := io.ReadAll(resp.Body)
if err != nil {
return DataSet{Source: u.id, Format: format}, err
}
ds := DataSet{
Data: data,
Format: format,
Source: u.id,
Timestamp: time.Now(),
}
ds.Checksum = "md5:" + ds.Md5()
return ds, nil
}
func (u urlSource) Watch(exit <-chan struct{}, load func(DataSet, error) bool) {
u.watch(exit, load)
}
func (u urlSource) watch(exit <-chan struct{}, load func(DataSet, error) bool) {
ticker := time.NewTicker(u.period)
defer ticker.Stop()
var last DataSet
for {
select {
case <-exit:
return
case <-ticker.C:
if ds, err := u.Read(); err != nil {
load(ds, err)
} else if len(ds.Data) > 0 && ds.Checksum != last.Checksum {
if load(ds, nil) {
last = ds
}
}
}
}
}