github.com/cs3org/reva/v2@v2.27.7/internal/http/services/sciencemesh/sciencemesh.go (about)

     1  // Copyright 2018-2023 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package sciencemesh
    20  
    21  import (
    22  	"net/http"
    23  
    24  	"github.com/go-chi/chi/v5"
    25  	"github.com/rs/zerolog"
    26  
    27  	"github.com/cs3org/reva/v2/pkg/appctx"
    28  	"github.com/cs3org/reva/v2/pkg/rhttp/global"
    29  	"github.com/cs3org/reva/v2/pkg/sharedconf"
    30  	"github.com/cs3org/reva/v2/pkg/utils/cfg"
    31  )
    32  
    33  func init() {
    34  	global.Register("sciencemesh", New)
    35  }
    36  
    37  // New returns a new sciencemesh service.
    38  func New(m map[string]interface{}, _ *zerolog.Logger) (global.Service, error) {
    39  	var c config
    40  	if err := cfg.Decode(m, &c); err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	r := chi.NewRouter()
    45  	s := &svc{
    46  		conf:   &c,
    47  		router: r,
    48  	}
    49  
    50  	if err := s.routerInit(); err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	return s, nil
    55  }
    56  
    57  // Close performs cleanup.
    58  func (s *svc) Close() error {
    59  	return nil
    60  }
    61  
    62  type config struct {
    63  	Prefix           string       `mapstructure:"prefix"`
    64  	GatewaySvc       string       `mapstructure:"gatewaysvc"         validate:"required"`
    65  	ProviderDomain   string       `mapstructure:"provider_domain"    validate:"required"`
    66  	MeshDirectoryURL string       `mapstructure:"mesh_directory_url"`
    67  	OCMMountPoint    string       `mapstructure:"ocm_mount_point"`
    68  	Events           EventOptions `mapstructure:"events"`
    69  }
    70  
    71  // EventOptions are the configurable options for events
    72  type EventOptions struct {
    73  	Endpoint             string `mapstructure:"natsaddress"`
    74  	Cluster              string `mapstructure:"natsclusterid"`
    75  	TLSInsecure          bool   `mapstructure:"tlsinsecure"`
    76  	TLSRootCACertificate string `mapstructure:"tlsrootcacertificate"`
    77  	EnableTLS            bool   `mapstructure:"enabletls"`
    78  	AuthUsername         string `mapstructure:"authusername"`
    79  	AuthPassword         string `mapstructure:"authpassword"`
    80  }
    81  
    82  func (c *config) ApplyDefaults() {
    83  	if c.Prefix == "" {
    84  		c.Prefix = "sciencemesh"
    85  	}
    86  	if c.OCMMountPoint == "" {
    87  		c.OCMMountPoint = "/ocm"
    88  	}
    89  
    90  	c.GatewaySvc = sharedconf.GetGatewaySVC(c.GatewaySvc)
    91  }
    92  
    93  type svc struct {
    94  	conf   *config
    95  	router chi.Router
    96  }
    97  
    98  func (s *svc) routerInit() error {
    99  	tokenHandler := new(tokenHandler)
   100  	if err := tokenHandler.init(s.conf); err != nil {
   101  		return err
   102  	}
   103  	providersHandler := new(providersHandler)
   104  	if err := providersHandler.init(s.conf); err != nil {
   105  		return err
   106  	}
   107  	sharesHandler := new(sharesHandler)
   108  	if err := sharesHandler.init(s.conf); err != nil {
   109  		return err
   110  	}
   111  
   112  	appsHandler := new(appsHandler)
   113  	if err := appsHandler.init(s.conf); err != nil {
   114  		return err
   115  	}
   116  
   117  	s.router.Post("/generate-invite", tokenHandler.Generate)
   118  	s.router.Get("/list-invite", tokenHandler.ListInvite)
   119  	s.router.Post("/accept-invite", tokenHandler.AcceptInvite)
   120  	s.router.Get("/find-accepted-users", tokenHandler.FindAccepted)
   121  	s.router.Delete("/delete-accepted-user", tokenHandler.DeleteAccepted)
   122  	s.router.Get("/list-providers", providersHandler.ListProviders)
   123  	s.router.Post("/create-share", sharesHandler.CreateShare)
   124  	s.router.Post("/open-in-app", appsHandler.OpenInApp)
   125  	return nil
   126  }
   127  
   128  func (s *svc) Prefix() string {
   129  	return s.conf.Prefix
   130  }
   131  
   132  func (s *svc) Unprotected() []string {
   133  	return nil
   134  }
   135  
   136  func (s *svc) Handler() http.Handler {
   137  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   138  		log := appctx.GetLogger(r.Context())
   139  		log.Debug().Str("path", r.URL.Path).Msg("sciencemesh routing")
   140  
   141  		// unset raw path, otherwise chi uses it to route and then fails to match percent encoded path segments
   142  		r.URL.RawPath = ""
   143  		s.router.ServeHTTP(w, r)
   144  	})
   145  }