/
storage.go
60 lines (50 loc) · 1.24 KB
/
storage.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
package storages
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Store
import (
"fmt"
"net/url"
"time"
"github.com/orange-cloudfoundry/statusetat/models"
)
type Store interface {
Creator() func(u *url.URL) (Store, error)
Detect(u *url.URL) bool
Create(incident models.Incident) (models.Incident, error)
Update(guid string, incident models.Incident) (models.Incident, error)
Delete(guid string) error
Read(guid string) (models.Incident, error)
ByDate(from, to time.Time) ([]models.Incident, error)
Persistents() ([]models.Incident, error)
Subscribe(email string) error
Unsubscribe(email string) error
Subscribers() ([]string, error)
Ping() error
}
var initStores = []Store{
NewRetry(&DB{}, 3),
NewRetry(&S3{}, 3),
NewRetry(&Local{}, 3),
}
func Factory(urls []*url.URL) (Store, error) {
if len(urls) == 0 {
return nil, fmt.Errorf("Url store must be set")
}
if len(urls) == 1 {
u := urls[0]
for _, store := range initStores {
if !store.Detect(u) {
continue
}
return store.Creator()(u)
}
return nil, fmt.Errorf("No valid store can be found")
}
repl := NewReplicate(initStores)
for _, u := range urls {
_, err := repl.Creator()(u)
if err != nil {
return nil, err
}
}
return repl, nil
}