-
Notifications
You must be signed in to change notification settings - Fork 112
/
service.go
183 lines (157 loc) · 5.39 KB
/
service.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
// 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 ocdav
import (
"net/http"
"strings"
"github.com/cs3org/reva/v2/internal/http/interceptors/appctx"
"github.com/cs3org/reva/v2/internal/http/interceptors/auth"
revaLogMiddleware "github.com/cs3org/reva/v2/internal/http/interceptors/log"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/rhttp/global"
"github.com/cs3org/reva/v2/pkg/storage/favorite/memory"
rtrace "github.com/cs3org/reva/v2/pkg/trace"
"github.com/go-chi/chi/v5"
httpServer "github.com/go-micro/plugins/v4/server/http"
"go-micro.dev/v4"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/server"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
const (
// ServerName to use when announcing the service to the registry
ServerName = "ocdav"
)
// Service initializes the ocdav service and underlying http server.
func Service(opts ...Option) (micro.Service, error) {
sopts := newOptions(opts...)
// set defaults
if err := setDefaults(&sopts); err != nil {
return nil, err
}
sopts.Logger = sopts.Logger.With().Str("name", sopts.Name).Logger()
srv := httpServer.NewServer(
server.TLSConfig(sopts.TLSConfig),
server.Name(sopts.Name),
server.Address(sopts.Address), // Address defaults to ":0" and will pick any free port
server.Version(sopts.config.VersionString),
)
tp := rtrace.GetTracerProvider(sopts.TracingEnabled, sopts.TracingCollector, sopts.TracingEndpoint, sopts.Name)
revaService, err := ocdav.NewWith(&sopts.config, sopts.FavoriteManager, sopts.lockSystem, &sopts.Logger, tp)
if err != nil {
return nil, err
}
// Comment back in after resolving the issue in go-chi.
// See comment in line 87.
// register additional webdav verbs
// chi.RegisterMethod(ocdav.MethodPropfind)
// chi.RegisterMethod(ocdav.MethodProppatch)
// chi.RegisterMethod(ocdav.MethodLock)
// chi.RegisterMethod(ocdav.MethodUnlock)
// chi.RegisterMethod(ocdav.MethodCopy)
// chi.RegisterMethod(ocdav.MethodMove)
// chi.RegisterMethod(ocdav.MethodMkcol)
// chi.RegisterMethod(ocdav.MethodReport)
r := chi.NewRouter()
if err := useMiddlewares(r, &sopts, revaService, tp); err != nil {
return nil, err
}
r.Handle("/*", revaService.Handler())
// This is a workaround for the go-chi concurrent map read write issue.
// After the issue has been solved upstream in go-chi we should switch
// back to using `chi.RegisterMethod`.
r.MethodNotAllowed(http.HandlerFunc(revaService.Handler().ServeHTTP))
hd := srv.NewHandler(r)
if err := srv.Handle(hd); err != nil {
return nil, err
}
service := micro.NewService(
micro.Server(srv),
micro.Registry(registry.NewRegistry()),
)
// Init the service? make that optional?
service.Init()
// finally, return the service so it can be Run() by the caller himself
return service, nil
}
func setDefaults(sopts *Options) error {
// set defaults
if sopts.Name == "" {
sopts.Name = ServerName
}
if sopts.lockSystem == nil {
client, err := pool.GetGatewayServiceClient(sopts.config.GatewaySvc)
if err != nil {
return err
}
sopts.lockSystem = ocdav.NewCS3LS(client)
}
if sopts.FavoriteManager == nil {
sopts.FavoriteManager, _ = memory.New(map[string]interface{}{})
}
if !strings.HasPrefix(sopts.config.Prefix, "/") {
sopts.config.Prefix = "/" + sopts.config.Prefix
}
if sopts.config.VersionString == "" {
sopts.config.VersionString = "0.0.0"
}
return nil
}
func useMiddlewares(r *chi.Mux, sopts *Options, svc global.Service, tp trace.TracerProvider) error {
// auth
for _, v := range svc.Unprotected() {
sopts.Logger.Info().Str("url", v).Msg("unprotected URL")
}
authMiddle, err := auth.New(map[string]interface{}{
"gatewaysvc": sopts.config.GatewaySvc,
"token_managers": map[string]interface{}{
"jwt": map[string]interface{}{
"secret": sopts.JWTSecret,
},
},
}, svc.Unprotected(), tp)
if err != nil {
return err
}
// log
lm := revaLogMiddleware.New()
// tracing
tm := func(h http.Handler) http.Handler { return h }
if sopts.TracingEnabled {
tm = traceHandler(tp, "ocdav")
}
// ctx
cm := appctx.New(sopts.Logger, tp)
// actually register
r.Use(tm, lm, authMiddle, cm)
return nil
}
func traceHandler(tp trace.TracerProvider, name string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := rtrace.Propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
t := tp.Tracer("reva")
ctx, span := t.Start(ctx, name)
defer span.End()
rtrace.Propagator.Inject(ctx, propagation.HeaderCarrier(r.Header))
h.ServeHTTP(w, r.WithContext(ctx))
})
}
}