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

     1  // Copyright 2018-2021 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 sysinfo
    20  
    21  import (
    22  	"net/http"
    23  
    24  	"github.com/mitchellh/mapstructure"
    25  	"github.com/pkg/errors"
    26  	"github.com/rs/zerolog"
    27  
    28  	"github.com/cs3org/reva/v2/pkg/appctx"
    29  	"github.com/cs3org/reva/v2/pkg/rhttp/global"
    30  	"github.com/cs3org/reva/v2/pkg/sysinfo"
    31  )
    32  
    33  func init() {
    34  	global.Register(serviceName, New)
    35  }
    36  
    37  type config struct {
    38  	Prefix string `mapstructure:"prefix"`
    39  }
    40  
    41  type svc struct {
    42  	conf *config
    43  }
    44  
    45  const (
    46  	serviceName = "sysinfo"
    47  )
    48  
    49  // Close is called when this service is being stopped.
    50  func (s *svc) Close() error {
    51  	return nil
    52  }
    53  
    54  // Prefix returns the main endpoint of this service.
    55  func (s *svc) Prefix() string {
    56  	return s.conf.Prefix
    57  }
    58  
    59  // Unprotected returns all endpoints that can be queried without prior authorization.
    60  func (s *svc) Unprotected() []string {
    61  	return []string{"/"}
    62  }
    63  
    64  // Handler serves all HTTP requests.
    65  func (s *svc) Handler() http.Handler {
    66  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    67  		log := appctx.GetLogger(r.Context())
    68  		if _, err := w.Write([]byte(s.getJSONData())); err != nil {
    69  			log.Err(err).Msg("error writing SysInfo response")
    70  		}
    71  	})
    72  }
    73  
    74  func (s *svc) getJSONData() string {
    75  	if data, err := sysinfo.SysInfo.ToJSON(); err == nil {
    76  		return data
    77  	}
    78  
    79  	return ""
    80  }
    81  
    82  func parseConfig(m map[string]interface{}) (*config, error) {
    83  	cfg := &config{}
    84  	if err := mapstructure.Decode(m, &cfg); err != nil {
    85  		return nil, errors.Wrap(err, "sysinfo: error decoding configuration")
    86  	}
    87  	applyDefaultConfig(cfg)
    88  	return cfg, nil
    89  }
    90  
    91  func applyDefaultConfig(conf *config) {
    92  	if conf.Prefix == "" {
    93  		conf.Prefix = serviceName
    94  	}
    95  }
    96  
    97  // New returns a new SysInfo service.
    98  func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) {
    99  	// Prepare the configuration
   100  	conf, err := parseConfig(m)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	// Create the service
   106  	s := &svc{
   107  		conf: conf,
   108  	}
   109  	return s, nil
   110  }