-
Notifications
You must be signed in to change notification settings - Fork 113
/
mentix.go
194 lines (162 loc) · 5.22 KB
/
mentix.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package mentix
import (
"net/http"
"github.com/cs3org/reva/v2/pkg/mentix/meshdata"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/cs3org/reva/v2/pkg/mentix"
"github.com/cs3org/reva/v2/pkg/mentix/config"
"github.com/cs3org/reva/v2/pkg/mentix/exchangers"
"github.com/cs3org/reva/v2/pkg/rhttp/global"
)
func init() {
global.Register(serviceName, New)
}
type svc struct {
conf *config.Configuration
mntx *mentix.Mentix
log *zerolog.Logger
stopSignal chan struct{}
}
const (
serviceName = "mentix"
)
func (s *svc) Close() error {
// Trigger and close the stopSignal signal channel to stop Mentix
s.stopSignal <- struct{}{}
close(s.stopSignal)
return nil
}
func (s *svc) Prefix() string {
return s.conf.Prefix
}
func (s *svc) Unprotected() []string {
// Get all endpoints exposed by the RequestExchangers
importers := s.mntx.GetRequestImporters()
exporters := s.mntx.GetRequestExporters()
getEndpoints := func(exchangers []exchangers.RequestExchanger) []string {
endpoints := make([]string, 0, len(exchangers))
for _, exchanger := range exchangers {
if !exchanger.IsProtectedEndpoint() {
endpoints = append(endpoints, exchanger.Endpoint())
}
}
return endpoints
}
endpoints := make([]string, 0, len(importers)+len(exporters))
endpoints = append(endpoints, getEndpoints(importers)...)
endpoints = append(endpoints, getEndpoints(exporters)...)
return endpoints
}
func (s *svc) Handler() http.Handler {
// Forward requests to Mentix
return http.HandlerFunc(s.mntx.RequestHandler)
}
func (s *svc) startBackgroundService() {
// Just run Mentix in the background
go func() {
if err := s.mntx.Run(s.stopSignal); err != nil {
s.log.Err(err).Msg("error while running mentix")
}
}()
}
func parseConfig(m map[string]interface{}) (*config.Configuration, error) {
cfg := &config.Configuration{}
if err := mapstructure.Decode(m, &cfg); err != nil {
return nil, errors.Wrap(err, "mentix: error decoding configuration")
}
applyInternalConfig(m, cfg)
applyDefaultConfig(cfg)
return cfg, nil
}
func applyInternalConfig(m map[string]interface{}, conf *config.Configuration) {
getSubsections := func(section string) []string {
subsections := make([]string, 0, 5)
if list, ok := m[section].(map[string]interface{}); ok {
for name := range list {
subsections = append(subsections, name)
}
}
return subsections
}
conf.EnabledConnectors = getSubsections("connectors")
conf.EnabledImporters = getSubsections("importers")
conf.EnabledExporters = getSubsections("exporters")
}
func applyDefaultConfig(conf *config.Configuration) {
// General
if conf.Prefix == "" {
conf.Prefix = serviceName
}
if conf.UpdateInterval == "" {
conf.UpdateInterval = "1h" // Update once per hour
}
// Connectors
if conf.Connectors.GOCDB.Scope == "" {
conf.Connectors.GOCDB.Scope = "SM" // TODO(Daniel-WWU-IT): This might change in the future
}
// Exporters
addDefaultConnector := func(enabledList *[]string) {
if len(*enabledList) == 0 {
*enabledList = append(*enabledList, "*")
}
}
if conf.Exporters.WebAPI.Endpoint == "" {
conf.Exporters.WebAPI.Endpoint = "/sites"
}
addDefaultConnector(&conf.Exporters.WebAPI.EnabledConnectors)
if conf.Exporters.CS3API.Endpoint == "" {
conf.Exporters.CS3API.Endpoint = "/cs3"
}
addDefaultConnector(&conf.Exporters.CS3API.EnabledConnectors)
if len(conf.Exporters.CS3API.ElevatedServiceTypes) == 0 {
conf.Exporters.CS3API.ElevatedServiceTypes = append(conf.Exporters.CS3API.ElevatedServiceTypes, meshdata.EndpointGateway, meshdata.EndpointOCM, meshdata.EndpointWebdav)
}
if conf.Exporters.SiteLocations.Endpoint == "" {
conf.Exporters.SiteLocations.Endpoint = "/loc"
}
addDefaultConnector(&conf.Exporters.SiteLocations.EnabledConnectors)
addDefaultConnector(&conf.Exporters.PrometheusSD.EnabledConnectors)
addDefaultConnector(&conf.Exporters.Metrics.EnabledConnectors)
}
// New returns a new Mentix service.
func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
// Prepare the configuration
conf, err := parseConfig(m)
if err != nil {
return nil, err
}
conf.Init()
// Create the Mentix instance
mntx, err := mentix.New(conf, log)
if err != nil {
return nil, errors.Wrap(err, "mentix: error creating Mentix")
}
// Create the service and start its background activity
s := &svc{
conf: conf,
mntx: mntx,
log: log,
stopSignal: make(chan struct{}),
}
s.startBackgroundService()
return s, nil
}