-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory.go
152 lines (124 loc) · 4.4 KB
/
factory.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package httpcache
import (
"errors"
"fmt"
"flamingo.me/dingo"
"flamingo.me/flamingo/v3/core/healthcheck/domain/healthcheck"
"flamingo.me/flamingo/v3/framework/config"
)
var ErrRedisConfig = errors.New("redis config not complete")
var ErrMemoryConfig = errors.New("memory config not complete")
var ErrTwoLevelConfig = errors.New("twolevel config not complete")
var ErrInvalidBackend = errors.New("invalid backend supplied")
type (
// FrontendFactory that can be used to build caches
FrontendFactory struct {
provider FrontendProvider
redisBackendFactory *RedisBackendFactory
inMemoryBackendFactory *InMemoryBackendFactory
twoLevelBackendFactory *TwoLevelBackendFactory
cacheConfig FactoryConfig
}
// FactoryConfig typed configuration used to build Caches by the factory
FactoryConfig map[string]BackendConfig
// BackendConfig typed configuration used to build BackendCaches by the factory
BackendConfig struct {
BackendType string
Memory *MemoryBackendConfig
Redis *RedisBackendConfig
Twolevel *struct {
First *BackendConfig
Second *BackendConfig
}
}
// FrontendProvider - Dingo Provider func
FrontendProvider func() *Frontend
)
// Inject for dependencies
func (f *FrontendFactory) Inject(
provider FrontendProvider,
redisBackendFactory *RedisBackendFactory,
inMemoryBackendFactory *InMemoryBackendFactory,
twoLevelBackendFactory *TwoLevelBackendFactory,
cfg *struct {
CacheConfig config.Map `inject:"config:httpcache.frontendFactory,optional"`
},
) *FrontendFactory {
f.provider = provider
f.inMemoryBackendFactory = inMemoryBackendFactory
f.redisBackendFactory = redisBackendFactory
f.twoLevelBackendFactory = twoLevelBackendFactory
if cfg != nil {
var cacheConfig FactoryConfig
err := cfg.CacheConfig.MapInto(&cacheConfig)
if err != nil {
panic(err)
}
f.cacheConfig = cacheConfig
}
return f
}
// BindConfiguredCaches creates annotated bindings from the cache configuration
func (f *FrontendFactory) BindConfiguredCaches(injector *dingo.Injector) error {
for cacheName, cfg := range f.cacheConfig {
backend, err := f.BuildBackend(cfg, cacheName)
if err != nil {
return err
}
frontend := f.BuildWithBackend(backend)
injector.Bind((*Frontend)(nil)).AnnotatedWith(cacheName).ToInstance(frontend)
if health, ok := backend.(healthcheck.Status); ok {
injector.BindMap(new(healthcheck.Status), "httpcache_"+cacheName).ToInstance(health)
}
}
return nil
}
// BuildWithBackend returns new HTTPFrontend cache with given backend
func (f *FrontendFactory) BuildWithBackend(backend Backend) *Frontend {
frontend := f.provider()
frontend.backend = backend
return frontend
}
// BuildBackend by given BackendConfig and frontendName
//
//nolint:cyclop // it is what it is
func (f *FrontendFactory) BuildBackend(backendConfig BackendConfig, frontendName string) (Backend, error) {
switch backendConfig.BackendType {
case "redis":
if backendConfig.Redis == nil {
return nil, ErrRedisConfig
}
return f.NewRedisBackend(*backendConfig.Redis, frontendName)
case "memory":
if backendConfig.Memory == nil {
return nil, ErrMemoryConfig
}
return f.NewMemoryBackend(*backendConfig.Memory, frontendName)
case "twolevel":
if backendConfig.Twolevel == nil || backendConfig.Twolevel.First == nil || backendConfig.Twolevel.Second == nil {
return nil, ErrTwoLevelConfig
}
first, err := f.BuildBackend(*backendConfig.Twolevel.First, frontendName)
if err != nil {
return nil, err
}
second, err := f.BuildBackend(*backendConfig.Twolevel.Second, frontendName)
if err != nil {
return nil, err
}
return f.NewTwoLevel(TwoLevelBackendConfig{first, second})
}
return nil, fmt.Errorf("backend type %q error: %w", backendConfig.BackendType, ErrInvalidBackend)
}
// NewMemoryBackend with given config and name
func (f *FrontendFactory) NewMemoryBackend(config MemoryBackendConfig, frontendName string) (Backend, error) {
return f.inMemoryBackendFactory.SetConfig(config).SetFrontendName(frontendName).Build()
}
// NewRedisBackend with given config and name
func (f *FrontendFactory) NewRedisBackend(config RedisBackendConfig, frontendName string) (Backend, error) {
return f.redisBackendFactory.SetConfig(config).SetFrontendName(frontendName).Build()
}
// NewTwoLevel with given config
func (f *FrontendFactory) NewTwoLevel(config TwoLevelBackendConfig) (Backend, error) {
return f.twoLevelBackendFactory.SetConfig(config).Build()
}