github.com/erda-project/erda-infra@v1.0.9/base/servicehub/README.md (about)

     1  # servicehub
     2  
     3  The *servicehub.Hub* is Service Manager, which manages the startup, initialization, dependency, and shutdown of services.
     4  
     5  Provider provide one or more services, and implement the *servicehub.Provider* interface to provide services.
     6  
     7  The *servicehub.Hub* manages all providers registered by function *servicehub.RegisterProvider* .
     8  
     9  ## Example
    10  The configuration file *examples.yaml*
    11  ```yaml
    12  hello-provider:
    13      message: "hello world"
    14  ```
    15  
    16  The code file *main.go*
    17  ```go
    18  package main
    19  
    20  import (
    21  	"context"
    22  	"os"
    23  	"time"
    24  
    25  	"github.com/erda-project/erda-infra/base/logs"
    26  	"github.com/erda-project/erda-infra/base/servicehub"
    27  )
    28  
    29  type config struct {
    30  	Message string `file:"message" flag:"msg" default:"hi" desc:"message to show" env:"HELLO_MESSAGE"`
    31  }
    32  
    33  type provider struct {
    34  	Cfg *config
    35  	Log logs.Logger
    36  }
    37  
    38  func (p *provider) Init(ctx servicehub.Context) error {
    39  	p.Log.Info("message: ", p.Cfg.Message)
    40  	return nil
    41  }
    42  
    43  func (p *provider) Run(ctx context.Context) error {
    44  	p.Log.Info("hello provider is running...")
    45  	tick := time.NewTicker(3 * time.Second)
    46  	defer tick.Stop()
    47  	for {
    48  		select {
    49  		case <-tick.C:
    50  			p.Log.Info("do something...")
    51  		case <-ctx.Done():
    52  			return nil
    53  		}
    54  	}
    55  }
    56  
    57  func init() {
    58  	servicehub.Register("hello-provider", &servicehub.Spec{
    59  		Services:    []string{"hello"},
    60  		Description: "hello for example",
    61  		ConfigFunc:  func() interface{} { return &config{} },
    62  		Creator: func() servicehub.Provider {
    63  			return &provider{}
    64  		},
    65  	})
    66  }
    67  
    68  func main() {
    69  	hub := servicehub.New()
    70  	hub.Run("examples", "", os.Args...)
    71  }
    72  ```
    73  
    74  Output:
    75  ```sh
    76  ➜ go run main.go
    77  INFO[2021-03-23 11:55:34.830] message: hello world                          module=hello-provider
    78  INFO[2021-03-23 11:55:34.830] provider hello-provider initialized          
    79  INFO[2021-03-23 11:55:34.830] signals to quit:[hangup interrupt terminated quit] 
    80  INFO[2021-03-23 11:55:34.831] hello provider is running...                  module=hello-provider
    81  INFO[2021-03-23 11:55:37.831] do something...                               module=hello-provider
    82  INFO[2021-03-23 11:55:40.835] do something...                               module=hello-provider
    83  ^C
    84  INFO[2021-03-23 11:55:41.624] provider hello-provider Run exit  
    85  ```
    86  
    87  [Example details](./examples/run/main.go)
    88  
    89  [More Examples](./examples/)
    90  
    91  ## Reading Config
    92  Support the following ways to read config, the priority from low to high is:
    93  * default Tag In Struct
    94  * System Environment Variable
    95  * .env File Environment Variable
    96  * Config File
    97  * Flag
    98  
    99  Supports file formats:
   100  * yaml、yml
   101  * json
   102  * hcl
   103  * toml
   104  * ...
   105