github.com/annwntech/go-micro/v2@v2.9.5/config/source/service/service.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/annwntech/go-micro/v2/client"
     8  	"github.com/annwntech/go-micro/v2/config/source"
     9  	proto "github.com/annwntech/go-micro/v2/config/source/service/proto"
    10  	"github.com/annwntech/go-micro/v2/errors"
    11  	"github.com/annwntech/go-micro/v2/logger"
    12  )
    13  
    14  var (
    15  	DefaultName      = "go.micro.config"
    16  	DefaultNamespace = "global"
    17  	DefaultPath      = ""
    18  )
    19  
    20  type service struct {
    21  	serviceName string
    22  	namespace   string
    23  	path        string
    24  	opts        source.Options
    25  	client      proto.ConfigService
    26  }
    27  
    28  func (m *service) Read() (set *source.ChangeSet, err error) {
    29  	client := proto.NewConfigService(m.serviceName, m.opts.Client)
    30  	req, err := client.Read(context.Background(), &proto.ReadRequest{
    31  		Namespace: m.namespace,
    32  		Path:      m.path,
    33  	})
    34  	if verr, ok := err.(*errors.Error); ok && verr.Code == http.StatusNotFound {
    35  		return nil, nil
    36  	} else if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	return toChangeSet(req.Change.ChangeSet), nil
    41  }
    42  
    43  func (m *service) Watch() (w source.Watcher, err error) {
    44  	client := proto.NewConfigService(m.serviceName, m.opts.Client)
    45  	stream, err := client.Watch(context.Background(), &proto.WatchRequest{
    46  		Namespace: m.namespace,
    47  		Path:      m.path,
    48  	})
    49  	if err != nil {
    50  		if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
    51  			logger.Error("watch err: ", err)
    52  		}
    53  		return
    54  	}
    55  	return newWatcher(stream)
    56  }
    57  
    58  // Write is unsupported
    59  func (m *service) Write(cs *source.ChangeSet) error {
    60  	return nil
    61  }
    62  
    63  func (m *service) String() string {
    64  	return "service"
    65  }
    66  
    67  func NewSource(opts ...source.Option) source.Source {
    68  	var options source.Options
    69  	for _, o := range opts {
    70  		o(&options)
    71  	}
    72  
    73  	addr := DefaultName
    74  	namespace := DefaultNamespace
    75  	path := DefaultPath
    76  
    77  	if options.Context != nil {
    78  		a, ok := options.Context.Value(serviceNameKey{}).(string)
    79  		if ok {
    80  			addr = a
    81  		}
    82  
    83  		k, ok := options.Context.Value(namespaceKey{}).(string)
    84  		if ok {
    85  			namespace = k
    86  		}
    87  
    88  		p, ok := options.Context.Value(pathKey{}).(string)
    89  		if ok {
    90  			path = p
    91  		}
    92  	}
    93  
    94  	if options.Client == nil {
    95  		options.Client = client.DefaultClient
    96  	}
    97  
    98  	s := &service{
    99  		serviceName: addr,
   100  		opts:        options,
   101  		namespace:   namespace,
   102  		path:        path,
   103  	}
   104  
   105  	return s
   106  }