/
service.go
129 lines (109 loc) · 2.8 KB
/
service.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
// Package sequence implements github.com/the-anna-project/clg.Service and
// provides functionality to read the information sequence stored under a
// provided information ID. The provided information ID in fact is an
// information peer ID. The associated information sequence is stored as peer
// value within the information peer.
package sequence
import (
"sync"
"github.com/the-anna-project/context"
"github.com/the-anna-project/id"
"github.com/the-anna-project/peer"
)
// ServiceConfig represents the configuration used to create a new CLG service.
type ServiceConfig struct {
// Dependencies.
IDService id.Service
PeerCollection *peer.Collection
}
// DefaultServiceConfig provides a default configuration to create a new CLG
// service by best effort.
func DefaultServiceConfig() ServiceConfig {
var err error
var idService id.Service
{
idConfig := id.DefaultServiceConfig()
idService, err = id.NewService(idConfig)
if err != nil {
panic(err)
}
}
var peerCollection *peer.Collection
{
peerConfig := peer.DefaultCollectionConfig()
peerCollection, err = peer.NewCollection(peerConfig)
if err != nil {
panic(err)
}
}
config := ServiceConfig{
// Dependencies.
IDService: idService,
PeerCollection: peerCollection,
}
return config
}
// NewService creates a new configured CLG service.
func NewService(config ServiceConfig) (*Service, error) {
// Dependencies.
if config.IDService == nil {
return nil, maskAnyf(invalidConfigError, "ID service must not be empty")
}
if config.PeerCollection == nil {
return nil, maskAnyf(invalidConfigError, "peer collection must not be empty")
}
ID, err := config.IDService.New()
if err != nil {
return nil, maskAny(err)
}
newService := &Service{
// Dependencies.
peer: config.PeerCollection,
// Internals.
bootOnce: sync.Once{},
closer: make(chan struct{}, 1),
metadata: map[string]string{
"id": ID,
"kind": "read/information/sequence",
"name": "clg",
"type": "service",
},
shutdownOnce: sync.Once{},
}
return newService, nil
}
type Service struct {
// Dependencies.
peer *peer.Collection
// Internals.
bootOnce sync.Once
closer chan struct{}
metadata map[string]string
shutdownOnce sync.Once
}
func (s *Service) Action() interface{} {
return func(ctx context.Context, informationID string) (string, error) {
informationPeer, err := s.peer.Information.SearchByID(informationID)
if err != nil {
return "", maskAny(err)
}
return informationPeer.Value(), nil
}
}
func (s *Service) Boot() {
s.bootOnce.Do(func() {
// Service specific boot logic goes here.
})
}
func (s *Service) Metadata() map[string]string {
m := map[string]string{}
for k, v := range s.metadata {
m[k] = v
}
return m
}
func (s *Service) Shutdown() {
s.shutdownOnce.Do(func() {
close(s.closer)
})
}