github.com/cs3org/reva/v2@v2.27.7/internal/http/services/helloworld/helloworld.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 helloworld 20 21 import ( 22 "net/http" 23 24 "github.com/cs3org/reva/v2/pkg/appctx" 25 "github.com/cs3org/reva/v2/pkg/rhttp/global" 26 "github.com/mitchellh/mapstructure" 27 "github.com/rs/zerolog" 28 ) 29 30 func init() { 31 global.Register("helloworld", New) 32 } 33 34 // New returns a new helloworld service 35 func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error) { 36 conf := &config{} 37 if err := mapstructure.Decode(m, conf); err != nil { 38 return nil, err 39 } 40 41 conf.init() 42 43 return &svc{conf: conf}, nil 44 } 45 46 // Close performs cleanup. 47 func (s *svc) Close() error { 48 return nil 49 } 50 51 type config struct { 52 Prefix string `mapstructure:"prefix"` 53 HelloMessage string `mapstructure:"message"` 54 } 55 56 func (c *config) init() { 57 if c.HelloMessage == "" { 58 c.HelloMessage = "Hello World!" 59 } 60 61 if c.Prefix == "" { 62 c.Prefix = "helloworld" 63 } 64 } 65 66 type svc struct { 67 conf *config 68 } 69 70 func (s *svc) Prefix() string { 71 return s.conf.Prefix 72 } 73 74 func (s *svc) Unprotected() []string { 75 return []string{"/"} 76 } 77 78 func (s *svc) Handler() http.Handler { 79 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 80 log := appctx.GetLogger(r.Context()) 81 if _, err := w.Write([]byte(s.conf.HelloMessage)); err != nil { 82 log.Err(err).Msg("error writing response") 83 } 84 }) 85 }