forked from cs3org/reva
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdataprovider.go
More file actions
217 lines (184 loc) · 6.9 KB
/
Copy pathdataprovider.go
File metadata and controls
217 lines (184 loc) · 6.9 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// 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 dataprovider
import (
"fmt"
"net/http"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog"
"github.com/owncloud/reva/v2/pkg/appctx"
"github.com/owncloud/reva/v2/pkg/events"
"github.com/owncloud/reva/v2/pkg/events/stream"
datatxregistry "github.com/owncloud/reva/v2/pkg/rhttp/datatx/manager/registry"
"github.com/owncloud/reva/v2/pkg/rhttp/global"
"github.com/owncloud/reva/v2/pkg/rhttp/router"
"github.com/owncloud/reva/v2/pkg/storage"
"github.com/owncloud/reva/v2/pkg/storage/fs/registry"
"github.com/owncloud/reva/v2/pkg/upload"
)
func init() {
global.Register("dataprovider", New)
}
type config struct {
Prefix string `mapstructure:"prefix" docs:"data;The prefix to be used for this HTTP service"`
Driver string `mapstructure:"driver" docs:"localhome;The storage driver to be used."`
Drivers map[string]map[string]interface{} `mapstructure:"drivers" docs:"url:pkg/storage/fs/localhome/localhome.go;The configuration for the storage driver"`
DataTXs map[string]map[string]interface{} `mapstructure:"data_txs" docs:"url:pkg/rhttp/datatx/manager/simple/simple.go;The configuration for the data tx protocols"`
NatsAddress string `mapstructure:"nats_address"`
NatsClusterID string `mapstructure:"nats_clusterID"`
NatsTLSInsecure bool `mapstructure:"nats_tls_insecure"`
NatsRootCACertPath string `mapstructure:"nats_root_ca_cert_path"`
NatsEnableTLS bool `mapstructure:"nats_enable_tls"`
NatsUsername string `mapstructure:"nats_username"`
NatsPassword string `mapstructure:"nats_password"`
UploadDirectory string `mapstructure:"upload_directory" docs:";Local directory for staging upload sessions. Overrides the driver's root. Required for drivers that have no local filesystem root."`
}
func (c *config) init() {
if c.Prefix == "" {
c.Prefix = "data"
}
if c.Driver == "" {
c.Driver = "localhome"
}
}
type svc struct {
conf *config
handler http.Handler
storage storage.FS
dataTXs map[string]http.Handler
}
// New returns a new datasvc
func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
conf := &config{}
if err := mapstructure.Decode(m, conf); err != nil {
return nil, err
}
conf.init()
var evstream events.Stream
if conf.NatsAddress == "" || conf.NatsClusterID == "" {
log.Warn().Msg("missing or incomplete nats configuration. Events will not be published.")
} else {
s, err := stream.NatsFromConfig("dataprovider", false, stream.NatsConfig{
Endpoint: conf.NatsAddress,
Cluster: conf.NatsClusterID,
EnableTLS: conf.NatsEnableTLS,
TLSInsecure: conf.NatsTLSInsecure,
TLSRootCACertificate: conf.NatsRootCACertPath,
AuthUsername: conf.NatsUsername,
AuthPassword: conf.NatsPassword,
})
if err != nil {
return nil, err
}
evstream = s
}
fs, err := getFS(conf, evstream, log)
if err != nil {
return nil, err
}
// the data path assembles chunks, so enable chunking
coord, err := upload.NewCoordinatorFromConfig(conf.UploadDirectory, conf.Drivers[conf.Driver], fs, evstream, log, true)
if err != nil {
return nil, fmt.Errorf("dataprovider: %w", err)
}
// only the data path consumes postprocessing results: one consumer group gets
// one copy of each event, so a second subscriber would take half of them
if ac := upload.AsyncConfFromDriverConf(conf.Drivers[conf.Driver]); ac.Enabled {
if err := coord.StartPostprocessing(evstream, ac.ConsumerGroup, ac.MountID, ac.NumConsumers); err != nil {
return nil, fmt.Errorf("dataprovider: could not start postprocessing: %w", err)
}
}
dataTXs, err := getDataTXs(conf, coord, fs, evstream, log)
if err != nil {
return nil, err
}
s := &svc{
storage: fs,
conf: conf,
dataTXs: dataTXs,
}
err = s.setHandler()
return s, err
}
func getFS(c *config, stream events.Stream, log *zerolog.Logger) (storage.FS, error) {
if f, ok := registry.NewFuncs[c.Driver]; ok {
return f(c.Drivers[c.Driver], stream, log)
}
return nil, fmt.Errorf("driver not found: %s", c.Driver)
}
func getDataTXs(c *config, coord upload.Coordinator, fs storage.FS, publisher events.Publisher, log *zerolog.Logger) (map[string]http.Handler, error) {
if c.DataTXs == nil {
c.DataTXs = make(map[string]map[string]interface{})
}
if len(c.DataTXs) == 0 {
c.DataTXs["simple"] = make(map[string]interface{})
c.DataTXs["simple"]["cache_store"] = "noop"
c.DataTXs["simple"]["cache_database"] = "reva"
c.DataTXs["spaces"] = make(map[string]interface{})
c.DataTXs["spaces"]["cache_store"] = "noop"
c.DataTXs["spaces"]["cache_database"] = "reva"
c.DataTXs["tus"] = make(map[string]interface{})
c.DataTXs["tus"]["cache_store"] = "noop"
c.DataTXs["tus"]["cache_database"] = "reva"
}
txs := make(map[string]http.Handler)
for t := range c.DataTXs {
if f, ok := datatxregistry.NewFuncs[t]; ok {
if tx, err := f(c.DataTXs[t], publisher, log); err == nil {
if handler, err := tx.Handler(coord, fs); err == nil {
txs[t] = handler
}
}
}
}
return txs, nil
}
func (s *svc) Close() error {
return nil
}
func (s *svc) Unprotected() []string {
return []string{
"/tus",
}
}
func (s *svc) Prefix() string {
return s.conf.Prefix
}
func (s *svc) Handler() http.Handler {
return s.handler
}
func (s *svc) setHandler() error {
s.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := appctx.GetLogger(r.Context())
log.Debug().Msgf("dataprovider routing: path=%s", r.URL.Path)
head, tail := router.ShiftPath(r.URL.Path)
if handler, ok := s.dataTXs[head]; ok {
r.URL.Path = tail
handler.ServeHTTP(w, r)
return
}
// If we don't find a prefix match for any of the protocols, upload the resource
// through the direct HTTP protocol
if handler, ok := s.dataTXs["simple"]; ok {
handler.ServeHTTP(w, r)
return
}
w.WriteHeader(http.StatusInternalServerError)
})
return nil
}