-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathocmd.go
134 lines (115 loc) · 3.81 KB
/
ocmd.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
// Copyright 2018-2022 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 ocmd
import (
"net/http"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/rhttp/global"
"github.com/cs3org/reva/pkg/rhttp/router"
"github.com/cs3org/reva/pkg/sharedconf"
"github.com/cs3org/reva/pkg/smtpclient"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog"
)
func init() {
global.Register("ocmd", New)
}
// Config holds the config options that need to be passed down to all ocdav handlers.
type Config struct {
SMTPCredentials *smtpclient.SMTPCredentials `mapstructure:"smtp_credentials"`
Prefix string `mapstructure:"prefix"`
Host string `mapstructure:"host"`
GatewaySvc string `mapstructure:"gatewaysvc"`
MeshDirectoryURL string `mapstructure:"mesh_directory_url"`
Config configData `mapstructure:"config"`
}
func (c *Config) init() {
c.GatewaySvc = sharedconf.GetGatewaySVC(c.GatewaySvc)
if c.Prefix == "" {
c.Prefix = "ocm"
}
}
type svc struct {
Conf *Config
SharesHandler *sharesHandler
NotificationsHandler *notificationsHandler
ConfigHandler *configHandler
InvitesHandler *invitesHandler
SendHandler *sendHandler
}
// New returns a new ocmd object.
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()
s := &svc{
Conf: conf,
}
s.SharesHandler = new(sharesHandler)
s.NotificationsHandler = new(notificationsHandler)
s.ConfigHandler = new(configHandler)
s.InvitesHandler = new(invitesHandler)
s.SendHandler = new(sendHandler)
s.SharesHandler.init(s.Conf)
s.NotificationsHandler.init(s.Conf)
log.Debug().Str("initializing ConfigHandler Host", s.Conf.Host)
s.ConfigHandler.init(s.Conf)
s.InvitesHandler.init(s.Conf)
s.SendHandler.init(s.Conf)
return s, nil
}
// Close performs cleanup.
func (s *svc) Close() error {
return nil
}
func (s *svc) Prefix() string {
return s.Conf.Prefix
}
func (s *svc) Unprotected() []string {
return []string{"/invites/accept", "/shares", "/ocm-provider", "/notifications"}
}
func (s *svc) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := appctx.GetLogger(ctx)
var head string
head, r.URL.Path = router.ShiftPath(r.URL.Path)
log.Debug().Str("head", head).Str("tail", r.URL.Path).Msg("http routing")
switch head {
case "ocm-provider":
s.ConfigHandler.Handler().ServeHTTP(w, r)
return
case "shares":
s.SharesHandler.Handler().ServeHTTP(w, r)
return
case "notifications":
s.NotificationsHandler.Handler().ServeHTTP(w, r)
return
case "invites":
s.InvitesHandler.Handler().ServeHTTP(w, r)
return
case "send":
s.SendHandler.Handler().ServeHTTP(w, r)
return
}
log.Warn().Msgf("request not handled. Try e.g. 'ocm-provider', 'shares', 'notifications', 'invites', or 'send' instead of '%s'", head)
w.WriteHeader(http.StatusNotFound)
})
}