forked from kyma-project/kyma
/
content.go
173 lines (141 loc) · 4.89 KB
/
content.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package content
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"time"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/domain/shared"
"github.com/pkg/errors"
"github.com/allegro/bigcache"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/domain/content/disabled"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/domain/content/storage"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/gqlschema"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/module"
minio "github.com/minio/minio-go"
)
type contentRetriever struct {
ContentGetter shared.ContentGetter
ApiSpecGetter shared.ApiSpecGetter
AsyncApiSpecGetter shared.AsyncApiSpecGetter
}
func (r *contentRetriever) Content() shared.ContentGetter {
return r.ContentGetter
}
func (r *contentRetriever) ApiSpec() shared.ApiSpecGetter {
return r.ApiSpecGetter
}
func (r *contentRetriever) AsyncApiSpec() shared.AsyncApiSpecGetter {
return r.AsyncApiSpecGetter
}
type Config struct {
Address string `envconfig:"default=minio.kyma.local"`
Port int `envconfig:"default=443"`
AccessKey string
SecretKey string
Bucket string `envconfig:"default=content"`
Secure bool `envconfig:"default=true"`
ExternalAddress string `envconfig:"optional"`
AssetsFolder string `envconfig:"default=assets"`
VerifySSL bool `envconfig:"default=true"`
}
type PluggableContainer struct {
*module.Pluggable
cfg *resolverConfig
Resolver resolver
ContentRetriever *contentRetriever
storageSvc storage.Service
}
func New(cfg Config) (*PluggableContainer, error) {
minioClient, err := minio.New(fmt.Sprintf("%s:%d", cfg.Address, cfg.Port), cfg.AccessKey, cfg.SecretKey, cfg.Secure)
if err != nil {
return nil, errors.Wrap(err, "while initializing Minio client")
}
if !cfg.VerifySSL {
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // ignore invalid SSL certificates
}
minioClient.SetCustomTransport(transCfg)
}
cacheConfig := bigcache.DefaultConfig(24 * time.Hour)
cacheConfig.Shards = 2
cacheConfig.MaxEntriesInWindow = 60
cacheConfig.HardMaxCacheSize = 10
if cfg.ExternalAddress == "" {
protocol := "http"
if cfg.Secure {
protocol = protocol + "s"
}
cfg.ExternalAddress = fmt.Sprintf("%s://%s", protocol, cfg.Address)
}
container := &PluggableContainer{
cfg: &resolverConfig{
cfg: cfg,
cache: cacheConfig,
minioClient: minioClient,
},
Pluggable: module.NewPluggable("content"),
ContentRetriever: &contentRetriever{},
}
err = container.Disable()
return container, err
}
func (r *PluggableContainer) Enable() error {
cfg := r.cfg.cfg
minioClient := r.cfg.minioClient
cache, err := bigcache.NewBigCache(r.cfg.cache)
if err != nil {
return err
}
r.storageSvc = storage.New(minioClient, cache, cfg.Bucket, cfg.ExternalAddress, cfg.AssetsFolder)
asyncApiSpecSvc := newAsyncApiSpecService(r.storageSvc)
apiSpecSvc := newApiSpecService(r.storageSvc)
contentSvc := newContentService(r.storageSvc)
r.Pluggable.EnableAndSyncCache(func(stopCh chan struct{}) {
r.storageSvc.Initialize(stopCh)
r.Resolver = &domainResolver{
contentResolver: newContentResolver(contentSvc),
topicsResolver: newTopicsResolver(contentSvc),
}
r.ContentRetriever.ApiSpecGetter = apiSpecSvc
r.ContentRetriever.AsyncApiSpecGetter = asyncApiSpecSvc
r.ContentRetriever.ContentGetter = contentSvc
})
return nil
}
func (r *PluggableContainer) Disable() error {
r.Pluggable.Disable(func(disabledErr error) {
r.Resolver = disabled.NewResolver(disabledErr)
r.ContentRetriever.AsyncApiSpecGetter = disabled.NewAsyncApiSpecGetter(disabledErr)
r.ContentRetriever.ApiSpecGetter = disabled.NewApiSpecGetter(disabledErr)
r.ContentRetriever.ContentGetter = disabled.NewContentGetter(disabledErr)
r.storageSvc = nil
})
return nil
}
type resolverConfig struct {
minioClient *minio.Client
cfg Config
cache bigcache.Config
}
//go:generate failery -name=resolver -case=underscore -output disabled -outpkg disabled
type resolver interface {
ContentQuery(ctx context.Context, contentType, id string) (*gqlschema.JSON, error)
TopicsQuery(ctx context.Context, topics []gqlschema.InputTopic, internal *bool) ([]gqlschema.TopicEntry, error)
}
//go:generate failery -name=asyncApiSpecGetter -case=underscore -output disabled -outpkg disabled
type asyncApiSpecGetter interface {
Find(kind, id string) (*storage.AsyncApiSpec, error)
}
//go:generate failery -name=apiSpecGetter -case=underscore -output disabled -outpkg disabled
type apiSpecGetter interface {
Find(kind, id string) (*storage.ApiSpec, error)
}
//go:generate failery -name=contentGetter -case=underscore -output disabled -outpkg disabled
type contentGetter interface {
Find(kind, id string) (*storage.Content, error)
}
type domainResolver struct {
*contentResolver
*topicsResolver
}