forked from influxdata/influxdb
/
dependencies.go
70 lines (59 loc) · 1.46 KB
/
dependencies.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
package influxdb
import (
"context"
"errors"
"github.com/influxdata/flux"
)
type key int
const dependenciesKey key = iota
type StorageDependencies struct {
Reader Reader
MetaClient MetaClient
Authorizer Authorizer
AuthEnabled bool
}
func (d StorageDependencies) Inject(ctx context.Context) context.Context {
return context.WithValue(ctx, dependenciesKey, d)
}
func (d StorageDependencies) Validate() error {
if d.Reader == nil {
return errors.New("missing reader dependency")
}
if d.MetaClient == nil {
return errors.New("missing meta client dependency")
}
if d.AuthEnabled && d.Authorizer == nil {
return errors.New("missing authorizer dependency")
}
return nil
}
func GetStorageDependencies(ctx context.Context) StorageDependencies {
return ctx.Value(dependenciesKey).(StorageDependencies)
}
type Dependencies struct {
StorageDeps StorageDependencies
FluxDeps flux.Dependencies
}
func (d Dependencies) Inject(ctx context.Context) context.Context {
ctx = d.FluxDeps.Inject(ctx)
return d.StorageDeps.Inject(ctx)
}
func NewDependencies(
mc MetaClient,
reader Reader,
auth Authorizer,
authEnabled bool,
) (Dependencies, error) {
fdeps := flux.NewDefaultDependencies()
deps := Dependencies{FluxDeps: fdeps}
deps.StorageDeps = StorageDependencies{
Reader: reader,
MetaClient: mc,
Authorizer: auth,
AuthEnabled: authEnabled,
}
if err := deps.StorageDeps.Validate(); err != nil {
return Dependencies{}, err
}
return deps, nil
}