-
Notifications
You must be signed in to change notification settings - Fork 113
/
sciencemesh.go
136 lines (116 loc) · 3.88 KB
/
sciencemesh.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
// Copyright 2018-2024 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 sciencemesh
import (
"context"
"net/http"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/rhttp/global"
"github.com/cs3org/reva/pkg/sharedconf"
"github.com/cs3org/reva/pkg/smtpclient"
"github.com/cs3org/reva/pkg/utils/cfg"
"github.com/go-chi/chi/v5"
)
func init() {
global.Register("sciencemesh", New)
}
// New returns a new sciencemesh service.
func New(ctx context.Context, m map[string]interface{}) (global.Service, error) {
var c config
if err := cfg.Decode(m, &c); err != nil {
return nil, err
}
r := chi.NewRouter()
s := &svc{
conf: &c,
router: r,
}
if err := s.routerInit(); err != nil {
return nil, err
}
return s, nil
}
// Close performs cleanup.
func (s *svc) Close() error {
return nil
}
type config struct {
Prefix string `mapstructure:"prefix"`
SMTPCredentials *smtpclient.SMTPCredentials `mapstructure:"smtp_credentials"`
GatewaySvc string `mapstructure:"gatewaysvc" validate:"required"`
MeshDirectoryURL string `mapstructure:"mesh_directory_url" validate:"required"`
ProviderDomain string `mapstructure:"provider_domain" validate:"required"`
SubjectTemplate string `mapstructure:"subject_template"`
BodyTemplatePath string `mapstructure:"body_template_path"`
OCMMountPoint string `mapstructure:"ocm_mount_point"`
}
func (c *config) ApplyDefaults() {
if c.Prefix == "" {
c.Prefix = "sciencemesh"
}
if c.OCMMountPoint == "" {
c.OCMMountPoint = "/ocm"
}
c.GatewaySvc = sharedconf.GetGatewaySVC(c.GatewaySvc)
}
type svc struct {
conf *config
router chi.Router
}
func (s *svc) routerInit() error {
tokenHandler := new(tokenHandler)
if err := tokenHandler.init(s.conf); err != nil {
return err
}
providersHandler := new(providersHandler)
if err := providersHandler.init(s.conf); err != nil {
return err
}
sharesHandler := new(sharesHandler)
if err := sharesHandler.init(s.conf); err != nil {
return err
}
appsHandler := new(appsHandler)
if err := appsHandler.init(s.conf); err != nil {
return err
}
s.router.Get("/generate-invite", tokenHandler.Generate)
s.router.Get("/list-invite", tokenHandler.ListInvite)
s.router.Post("/accept-invite", tokenHandler.AcceptInvite)
s.router.Get("/find-accepted-users", tokenHandler.FindAccepted)
s.router.Delete("/delete-accepted-user", tokenHandler.DeleteAccepted)
s.router.Get("/list-providers", providersHandler.ListProviders)
s.router.Post("/create-share", sharesHandler.CreateShare)
s.router.Post("/open-in-app", appsHandler.OpenInApp)
return nil
}
func (s *svc) Prefix() string {
return s.conf.Prefix
}
func (s *svc) Unprotected() []string {
return nil
}
func (s *svc) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := appctx.GetLogger(r.Context())
log.Debug().Str("path", r.URL.Path).Msg("sciencemesh routing")
// unset raw path, otherwise chi uses it to route and then fails to match percent encoded path segments
r.URL.RawPath = ""
s.router.ServeHTTP(w, r)
})
}