github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/examples/example/helloworld/provider.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 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 package example 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "github.com/erda-project/erda-infra/base/logs" 23 "github.com/erda-project/erda-infra/base/servicehub" 24 ) 25 26 // Interface . 27 type Interface interface { 28 Hello(name string) string 29 } 30 31 type config struct { 32 // some fields of config for this provider 33 Message string `file:"message" flag:"msg" default:"hi" desc:"message to print"` 34 } 35 36 // +provider 37 type provider struct { 38 Cfg *config 39 Log logs.Logger 40 } 41 42 // Init this is optional 43 func (p *provider) Init(ctx servicehub.Context) error { 44 p.Log.Info("message: ", p.Cfg.Message) 45 return nil 46 } 47 48 // Run this is optional 49 func (p *provider) Run(ctx context.Context) error { 50 tick := time.NewTicker(3 * time.Second) 51 defer tick.Stop() 52 for { 53 select { 54 case <-tick.C: 55 p.Log.Info("do something...") 56 case <-ctx.Done(): 57 return nil 58 } 59 } 60 } 61 62 // Hello . 63 func (p *provider) Hello(name string) string { 64 return fmt.Sprintf("hello %s", p.Cfg.Message) 65 } 66 67 func init() { 68 servicehub.Register("helloworld", &servicehub.Spec{ 69 Services: []string{"helloworld-service"}, 70 Description: "here is description of helloworld", 71 ConfigFunc: func() interface{} { return &config{} }, 72 Creator: func() servicehub.Provider { 73 return &provider{} 74 }, 75 }) 76 }