diff --git a/api/handler/rpc/rpc.go b/api/handler/rpc/rpc.go index ccab593a..c66f715c 100644 --- a/api/handler/rpc/rpc.go +++ b/api/handler/rpc/rpc.go @@ -444,7 +444,7 @@ func writeError(w http.ResponseWriter, r *http.Request, err error) { _, werr := w.Write([]byte(ce.Error())) if werr != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(werr) } } @@ -470,7 +470,7 @@ func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) { // write response _, err := w.Write(rsp) if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } } diff --git a/api/handler/rpc/stream.go b/api/handler/rpc/stream.go index b58823e8..541b8df6 100644 --- a/api/handler/rpc/stream.go +++ b/api/handler/rpc/stream.go @@ -49,7 +49,7 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, } payload, err := requestPayload(r) if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return @@ -72,7 +72,7 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, conn, rw, _, err := upgrader.Upgrade(r, w) if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return @@ -80,7 +80,7 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, defer func() { if err := conn.Close(); err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return @@ -116,7 +116,7 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, // create a new stream stream, err := c.Stream(ctx, req, callOpt) if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return @@ -124,7 +124,7 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, if request != nil { if err = stream.Send(request); err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return @@ -150,7 +150,7 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, if strings.Contains(err.Error(), "context canceled") { return } - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return @@ -158,13 +158,13 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, // write the response if err := wsutil.WriteServerMessage(rw, op, buf); err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return } if err = rw.Flush(); err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return @@ -195,7 +195,7 @@ func writeLoop(rw io.ReadWriter, stream client.Stream) { return } } - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return @@ -212,7 +212,7 @@ func writeLoop(rw io.ReadWriter, stream client.Stream) { // if the extracted payload isn't empty lets use it request := &raw.Frame{Data: buf} if err := stream.Send(request); err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Error(err) } return diff --git a/api/router/registry/registry.go b/api/router/registry/registry.go index 743f5c9c..e9cf51f0 100644 --- a/api/router/registry/registry.go +++ b/api/router/registry/registry.go @@ -53,7 +53,7 @@ func (r *registryRouter) refresh() { services, err := r.opts.Registry.ListServices() if err != nil { attempts++ - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Errorf("unable to list services: %v", err) } time.Sleep(time.Duration(attempts) * time.Second) @@ -66,7 +66,7 @@ func (r *registryRouter) refresh() { for _, s := range services { service, err := r.opts.Registry.GetService(s.Name) if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Errorf("unable to get service: %v", err) } continue @@ -94,7 +94,7 @@ func (r *registryRouter) process(res *registry.Result) { // get entry from cache service, err := r.opts.Registry.GetService(res.Service.Name) if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Errorf("unable to get %v service: %v", res.Service.Name, err) } return @@ -129,7 +129,7 @@ func (r *registryRouter) store(services []*registry.Service) { } // if we got nothing skip if err := api.Validate(end); err != nil { - if logger.V(logger.TraceLevel, logger.DefaultLogger) { + if logger.V(logger.TraceLevel) { logger.Tracef("endpoint validation failed: %v", err) } continue @@ -176,7 +176,7 @@ func (r *registryRouter) store(services []*registry.Service) { } hostreg, err := regexp.CompilePOSIX(h) if err != nil { - if logger.V(logger.TraceLevel, logger.DefaultLogger) { + if logger.V(logger.TraceLevel) { logger.Tracef("endpoint have invalid host regexp: %v", err) } continue @@ -197,7 +197,7 @@ func (r *registryRouter) store(services []*registry.Service) { rule, err := util.Parse(p) if err != nil && !pcreok { - if logger.V(logger.TraceLevel, logger.DefaultLogger) { + if logger.V(logger.TraceLevel) { logger.Tracef("endpoint have invalid path pattern: %v", err) } continue @@ -208,7 +208,7 @@ func (r *registryRouter) store(services []*registry.Service) { tpl := rule.Compile() pathreg, err := util.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, "") if err != nil { - if logger.V(logger.TraceLevel, logger.DefaultLogger) { + if logger.V(logger.TraceLevel) { logger.Tracef("endpoint have invalid path pattern: %v", err) } continue @@ -233,7 +233,7 @@ func (r *registryRouter) watch() { w, err := r.opts.Registry.Watch() if err != nil { attempts++ - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Errorf("error watching endpoints: %v", err) } time.Sleep(time.Duration(attempts) * time.Second) @@ -258,7 +258,7 @@ func (r *registryRouter) watch() { // process next event res, err := w.Next() if err != nil { - if logger.V(logger.ErrorLevel, logger.DefaultLogger) { + if logger.V(logger.ErrorLevel) { logger.Errorf("error getting next endpoint: %v", err) } close(ch) @@ -324,7 +324,7 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) { if !mMatch { continue } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("api method match %s", req.Method) } @@ -347,7 +347,7 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) { if !hMatch { continue } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("api host match %s", req.URL.Host) } @@ -355,12 +355,12 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) { for _, pathreg := range cep.pathregs { matches, err := pathreg.Match(path, "") if err != nil { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("api gpath not match %s != %v", path, pathreg) } continue } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("api gpath match %s = %v", path, pathreg) } pMatch = true @@ -381,12 +381,12 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) { // 4. try path via pcre path matching for _, pathreg := range cep.pcreregs { if !pathreg.MatchString(req.URL.Path) { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("api pcre path not match %s != %v", path, pathreg) } continue } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("api pcre path match %s != %v", path, pathreg) } pMatch = true diff --git a/api/router/static/static.go b/api/router/static/static.go index e2d60dc5..881157f9 100644 --- a/api/router/static/static.go +++ b/api/router/static/static.go @@ -249,8 +249,8 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) { if !mMatch { continue } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("api method match %s", req.Method) + if logger.V(logger.TraceLevel) { + logger.Tracef("api method match %s", req.Method) } // 2. try host @@ -272,21 +272,21 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) { if !hMatch { continue } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("api host match %s", req.URL.Host) + if logger.V(logger.TraceLevel) { + logger.Tracef("api host match %s", req.URL.Host) } // 3. try google.api path for _, pathreg := range ep.pathregs { matches, err := pathreg.Match(path, "") if err != nil { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("api gpath not match %s != %v", path, pathreg) + if logger.V(logger.TraceLevel) { + logger.Tracef("api gpath not match %s != %v", path, pathreg) } continue } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("api gpath match %s = %v", path, pathreg) + if logger.V(logger.TraceLevel) { + logger.Tracef("api gpath match %s = %v", path, pathreg) } pMatch = true ctx := req.Context() @@ -306,8 +306,8 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) { // 4. try path via pcre path matching for _, pathreg := range ep.pcreregs { if !pathreg.MatchString(req.URL.Path) { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("api pcre path not match %s != %v", req.URL.Path, pathreg) + if logger.V(logger.TraceLevel) { + logger.Tracef("api pcre path not match %s != %v", req.URL.Path, pathreg) } continue } diff --git a/api/server/acme/autocert/autocert.go b/api/server/acme/autocert/autocert.go index ac277a45..8e7e1bf8 100644 --- a/api/server/acme/autocert/autocert.go +++ b/api/server/acme/autocert/autocert.go @@ -31,7 +31,7 @@ func (a *autocertProvider) TLSConfig(hosts ...string) (*tls.Config, error) { } dir := cacheDir() if err := os.MkdirAll(dir, 0700); err != nil { - if logger.V(logger.InfoLevel, logger.DefaultLogger) { + if logger.V(logger.InfoLevel) { logger.Infof("warning: autocert not using a cache: %v", err) } } else { diff --git a/api/server/http/http.go b/api/server/http/http.go index 03b7c1e3..dfaea8bc 100644 --- a/api/server/http/http.go +++ b/api/server/http/http.go @@ -75,7 +75,7 @@ func (s *httpServer) Start() error { return err } - if logger.V(logger.InfoLevel, logger.DefaultLogger) { + if logger.V(logger.InfoLevel) { logger.Infof("HTTP API Listening on %s", l.Addr().String()) } diff --git a/broker/options.go b/broker/options.go index 4f87c56f..974b9d1e 100644 --- a/broker/options.go +++ b/broker/options.go @@ -27,10 +27,16 @@ type Options struct { Context context.Context } -func NewOptions() Options { - return Options{ - Context: context.Background(), +func NewOptions(opts ...Option) Options { + options := Options{ + Registry: registry.DefaultRegistry, + Logger: logger.DefaultLogger, + Context: context.Background(), } + for _, o := range opts { + o(&options) + } + return options } type PublishOptions struct { diff --git a/client/client.go b/client/client.go index 63a9b9bb..5364dc1b 100644 --- a/client/client.go +++ b/client/client.go @@ -9,7 +9,7 @@ import ( ) var ( - DefaultClient Client + DefaultClient Client = newClient() ) // Client is the interface used to make requests to services. diff --git a/client/noop.go b/client/noop.go new file mode 100644 index 00000000..deb43b55 --- /dev/null +++ b/client/noop.go @@ -0,0 +1,157 @@ +package client + +import ( + "context" + + "github.com/unistack-org/micro/v3/codec" +) + +type noopClient struct { + opts Options +} + +type noopMessage struct { + topic string + payload interface{} + contentType string +} + +type noopRequest struct { + service string + method string + endpoint string + contentType string + body interface{} + codec codec.Writer + stream bool +} + +func (n *noopRequest) Service() string { + return n.service +} + +func (n *noopRequest) Method() string { + return n.method +} + +func (n *noopRequest) Endpoint() string { + return n.endpoint +} + +func (n *noopRequest) ContentType() string { + return n.contentType +} + +func (n *noopRequest) Body() interface{} { + return n.body +} + +func (n *noopRequest) Codec() codec.Writer { + return n.codec +} + +func (n *noopRequest) Stream() bool { + return n.stream +} + +type noopResponse struct { + codec codec.Reader + header map[string]string +} + +func (n *noopResponse) Codec() codec.Reader { + return n.codec +} + +func (n *noopResponse) Header() map[string]string { + return n.header +} + +func (n *noopResponse) Read() ([]byte, error) { + return nil, nil +} + +type noopStream struct{} + +func (n *noopStream) Context() context.Context { + return context.Background() +} + +func (n *noopStream) Request() Request { + return &noopRequest{} +} + +func (n *noopStream) Response() Response { + return &noopResponse{} +} + +func (n *noopStream) Send(interface{}) error { + return nil +} + +func (n *noopStream) Recv(interface{}) error { + return nil +} + +func (n *noopStream) Error() error { + return nil +} + +func (n *noopStream) Close() error { + return nil +} + +func (n *noopMessage) Topic() string { + return n.topic +} + +func (n *noopMessage) Payload() interface{} { + return n.payload +} + +func (n *noopMessage) ContentType() string { + return n.contentType +} + +func (n *noopClient) Init(opts ...Option) error { + for _, o := range opts { + o(&n.opts) + } + return nil +} + +func (n *noopClient) Options() Options { + return n.opts +} + +func (n *noopClient) String() string { + return "noop" +} + +func (n *noopClient) Call(ctx context.Context, req Request, rsp interface{}, opts ...CallOption) error { + return nil +} + +func (n *noopClient) NewRequest(service, endpoint string, req interface{}, opts ...RequestOption) Request { + return &noopRequest{} +} + +func (n *noopClient) NewMessage(topic string, msg interface{}, opts ...MessageOption) Message { + return &noopMessage{} +} + +func (n *noopClient) Stream(ctx context.Context, req Request, opts ...CallOption) (Stream, error) { + return &noopStream{}, nil +} + +func (n *noopClient) Publish(ctx context.Context, msg Message, opts ...PublishOption) error { + return nil +} + +func newClient(opts ...Option) Client { + options := NewOptions() + for _, o := range opts { + o(&options) + } + return &noopClient{opts: options} +} diff --git a/client/options.go b/client/options.go index 59de1580..9a1243d7 100644 --- a/client/options.go +++ b/client/options.go @@ -105,8 +105,8 @@ type RequestOptions struct { Context context.Context } -func NewOptions(options ...Option) Options { - opts := Options{ +func NewOptions(opts ...Option) Options { + options := Options{ Cache: NewCache(), Context: context.Background(), ContentType: "application/protobuf", @@ -122,13 +122,15 @@ func NewOptions(options ...Option) Options { PoolSize: DefaultPoolSize, PoolTTL: DefaultPoolTTL, Selector: random.NewSelector(), + Logger: logger.DefaultLogger, + Broker: broker.DefaultBroker, } - for _, o := range options { - o(&opts) + for _, o := range opts { + o(&options) } - return opts + return options } // Broker to be used for pub/sub diff --git a/go.mod b/go.mod index 178ad010..cb74220b 100644 --- a/go.mod +++ b/go.mod @@ -8,26 +8,26 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/ef-ds/deque v1.0.4-0.20190904040645-54cb57c252a1 - github.com/evanphx/json-patch/v5 v5.0.0 + github.com/evanphx/json-patch/v5 v5.1.0 github.com/ghodss/yaml v1.0.0 github.com/go-acme/lego/v3 v3.4.0 github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee github.com/gobwas/ws v1.0.3 github.com/golang/protobuf v1.4.2 - github.com/google/uuid v1.1.1 + github.com/google/uuid v1.1.2 github.com/hashicorp/hcl v1.0.0 github.com/kr/text v0.2.0 // indirect github.com/micro/cli/v2 v2.1.2 - github.com/miekg/dns v1.1.27 + github.com/miekg/dns v1.1.31 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c github.com/patrickmn/go-cache v2.1.0+incompatible github.com/stretchr/testify v1.5.1 - github.com/unistack-org/micro-codec-bytes v0.0.0-20200827104921-3616a69473a6 + github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844 github.com/unistack-org/micro-config-cmd v0.0.0-20200828075439-d859b9d7265b - golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 - golang.org/x/net v0.0.0-20200707034311-ab3426394381 - google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 + golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a + golang.org/x/net v0.0.0-20200904194848-62affa334b73 + google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d google.golang.org/protobuf v1.25.0 gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect ) diff --git a/go.sum b/go.sum index e8986fb7..f3c35d55 100644 --- a/go.sum +++ b/go.sum @@ -75,6 +75,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch/v5 v5.0.0 h1:dKTrUeykyQwKb/kx7Z+4ukDs6l+4L41HqG1XHnhX7WE= github.com/evanphx/json-patch/v5 v5.0.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4= +github.com/evanphx/json-patch/v5 v5.1.0 h1:B0aXl1o/1cP8NbviYiBMkcHBtUjIJ1/Ccg6b+SwCLQg= +github.com/evanphx/json-patch/v5 v5.1.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4= github.com/exoscale/egoscale v0.18.1/go.mod h1:Z7OOdzzTOz1Q1PjQXumlz9Wn/CddH0zSYdCF3rnBKXE= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= @@ -139,6 +141,8 @@ github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OI github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/gophercloud/gophercloud v0.3.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8= @@ -197,6 +201,8 @@ github.com/micro/cli/v2 v2.1.2/go.mod h1:EguNh6DAoWKm9nmk+k/Rg0H3lQnDxqzu5x5srOt github.com/miekg/dns v1.1.15/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.27 h1:aEH/kqUzUxGJ/UHcEKdJY+ugH6WEzsEBBSPa8zuy1aM= github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= +github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo= +github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed/go.mod h1:3rdaFaCv4AyBgu5ALFM0+tSuHrBh6v692nyQe3ikrq0= @@ -275,8 +281,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/timewasted/linode v0.0.0-20160829202747-37e84520dcf7/go.mod h1:imsgLplxEC/etjIhdr3dNzV3JeT27LbVu5pYWm0JCBY= github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY= github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g= -github.com/unistack-org/micro-codec-bytes v0.0.0-20200827104921-3616a69473a6 h1:bBPX47ly/xhLhuCMIvybXKevCOCZCmxzvDwR61zu3cQ= -github.com/unistack-org/micro-codec-bytes v0.0.0-20200827104921-3616a69473a6/go.mod h1:g5sOI8TWgGZiVHe8zoUPdtz7+0oLnqTnfBoai6Qb7jE= +github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844 h1:5b1yuSllbsMm/9fUIlIXSr8DbsKT/sAKSCgOx6+SAfI= +github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844/go.mod h1:g5sOI8TWgGZiVHe8zoUPdtz7+0oLnqTnfBoai6Qb7jE= github.com/unistack-org/micro-config-cmd v0.0.0-20200828075439-d859b9d7265b h1:v5Ak+Sr780jZclFDnx82g5biF0N5HRVKphEpJhbnVUs= github.com/unistack-org/micro-config-cmd v0.0.0-20200828075439-d859b9d7265b/go.mod h1:6pm1cadbwsFcEW1ZbV5Fp0i3goR3TNfROMNSPih3I8k= github.com/unistack-org/micro/v3 v3.0.0-20200827083227-aa99378adc6e/go.mod h1:rPQbnry3nboAnMczj8B1Gzlcyv/HYoMZLgd3/3nttJ4= @@ -303,6 +309,8 @@ golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 h1:DZhuSZLsGlFL4CmhA8BcRA0mnthyA/nZ00AqCUo7vHg= golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM= +golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -345,6 +353,8 @@ golang.org/x/net v0.0.0-20190930134127-c5a3c61f89f3/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20191027093000-83d349e8ac1a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU= golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA= +golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= @@ -405,6 +415,7 @@ golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20191216173652-a0e659d51361 h1:RIIXAeV6GvDBuADKumTODatUqANFZ+5BPMnzsy4hulY= golang.org/x/tools v0.0.0-20191216173652-a0e659d51361/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -432,6 +443,8 @@ google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBr google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d h1:92D1fum1bJLKSdr11OJ+54YeCMCGYIygTA7R/YZxH5M= +google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.19.1/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= @@ -448,6 +461,7 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= diff --git a/logger/level.go b/logger/level.go index 915f0d47..4e27e204 100644 --- a/logger/level.go +++ b/logger/level.go @@ -117,10 +117,6 @@ func Fatalf(template string, args ...interface{}) { } // Returns true if the given level is at or lower the current logger level -func V(lvl Level, log Logger) bool { - l := DefaultLogger - if log != nil { - l = log - } - return l.Options().Level <= lvl +func V(lvl Level) bool { + return DefaultLogger.Options().Level <= lvl } diff --git a/registry/options.go b/registry/options.go index 598ecc9d..0b4702ed 100644 --- a/registry/options.go +++ b/registry/options.go @@ -19,11 +19,15 @@ type Options struct { Context context.Context } -func NewOptions() Options { - return Options{ +func NewOptions(opts ...Option) Options { + options := Options{ Logger: logger.DefaultLogger, Context: context.Background(), } + for _, o := range opts { + o(&options) + } + return options } type RegisterOptions struct { diff --git a/server/handler.go b/server/handler.go index 8236c355..9f7beb4e 100644 --- a/server/handler.go +++ b/server/handler.go @@ -9,6 +9,10 @@ type HandlerOptions struct { Metadata map[string]map[string]string } +func NewHandlerOptions() HandlerOptions { + return HandlerOptions{} +} + type SubscriberOption func(*SubscriberOptions) type SubscriberOptions struct { @@ -20,6 +24,19 @@ type SubscriberOptions struct { Context context.Context } +func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions { + opt := SubscriberOptions{ + AutoAck: true, + Context: context.Background(), + } + + for _, o := range opts { + o(&opt) + } + + return opt +} + // EndpointMetadata is a Handler option that allows metadata to be added to // individual endpoints. func EndpointMetadata(name string, md map[string]string) HandlerOption { @@ -44,18 +61,6 @@ func InternalSubscriber(b bool) SubscriberOption { o.Internal = b } } -func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions { - opt := SubscriberOptions{ - AutoAck: true, - Context: context.Background(), - } - - for _, o := range opts { - o(&opt) - } - - return opt -} // DisableAutoAck will disable auto acking of messages // after they have been handled. diff --git a/server/noop.go b/server/noop.go new file mode 100644 index 00000000..8fdd61eb --- /dev/null +++ b/server/noop.go @@ -0,0 +1,108 @@ +package server + +import "github.com/unistack-org/micro/v3/registry" + +type noopServer struct { + h Handler + opts Options +} + +type noopHandler struct { + opts HandlerOptions + h interface{} +} + +type noopSubscriber struct { + topic string + opts SubscriberOptions + h interface{} +} + +func (n *noopSubscriber) Topic() string { + return n.topic +} + +func (n *noopSubscriber) Subscriber() interface{} { + return n.h +} + +func (n *noopSubscriber) Endpoints() []*registry.Endpoint { + return nil +} + +func (n *noopSubscriber) Options() SubscriberOptions { + return n.opts +} + +func (n *noopHandler) Endpoints() []*registry.Endpoint { + return nil +} + +func (n *noopHandler) Handler() interface{} { + return nil +} + +func (n *noopHandler) Options() HandlerOptions { + return n.opts +} + +func (n *noopHandler) Name() string { + return "noop" +} + +func (n *noopServer) Handle(handler Handler) error { + n.h = handler + return nil +} + +func (n *noopServer) Subscribe(subscriber Subscriber) error { + // n.s = handler + return nil +} + +func (n *noopServer) NewHandler(h interface{}, opts ...HandlerOption) Handler { + options := NewHandlerOptions() + for _, o := range opts { + o(&options) + } + return &noopHandler{opts: options, h: h} +} + +func (n *noopServer) NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber { + options := NewSubscriberOptions() + for _, o := range opts { + o(&options) + } + return &noopSubscriber{topic: topic, opts: options, h: h} +} + +func (n *noopServer) Init(opts ...Option) error { + for _, o := range opts { + o(&n.opts) + } + return nil +} + +func (n *noopServer) Start() error { + return nil +} + +func (n *noopServer) Stop() error { + return nil +} + +func (n *noopServer) Options() Options { + return n.opts +} + +func (n *noopServer) String() string { + return "noop" +} + +func newServer(opts ...Option) Server { + options := NewOptions() + for _, o := range opts { + o(&options) + } + return &noopServer{opts: options} +} diff --git a/server/options.go b/server/options.go index 603dc415..2c2bf0ab 100644 --- a/server/options.go +++ b/server/options.go @@ -51,39 +51,19 @@ type Options struct { Context context.Context } -func newOptions(opt ...Option) Options { - opts := Options{ +func NewOptions() Options { + return Options{ Codecs: make(map[string]codec.NewCodec), + Context: context.Background(), Metadata: map[string]string{}, RegisterInterval: DefaultRegisterInterval, RegisterTTL: DefaultRegisterTTL, + RegisterCheck: DefaultRegisterCheck, + Address: DefaultAddress, + Name: DefaultName, + Version: DefaultVersion, + Id: DefaultId, } - - for _, o := range opt { - o(&opts) - } - - if opts.RegisterCheck == nil { - opts.RegisterCheck = DefaultRegisterCheck - } - - if len(opts.Address) == 0 { - opts.Address = DefaultAddress - } - - if len(opts.Name) == 0 { - opts.Name = DefaultName - } - - if len(opts.Id) == 0 { - opts.Id = DefaultId - } - - if len(opts.Version) == 0 { - opts.Version = DefaultVersion - } - - return opts } // Server name diff --git a/server/server.go b/server/server.go index 6300d67c..87387275 100644 --- a/server/server.go +++ b/server/server.go @@ -11,7 +11,7 @@ import ( ) var ( - DefaultServer Server + DefaultServer Server = newServer() ) // Server is a simple micro server abstraction @@ -138,7 +138,7 @@ type Option func(*Options) var ( DefaultAddress = ":0" - DefaultName = "go.micro.server" + DefaultName = "server" DefaultVersion = "latest" DefaultId = uuid.New().String() DefaultRegisterCheck = func(context.Context) error { return nil } diff --git a/service.go b/service.go index d2ea2503..95f756a6 100644 --- a/service.go +++ b/service.go @@ -149,7 +149,7 @@ func (s *service) Run() error { defer s.opts.Profile.Stop() } - if logger.V(logger.InfoLevel, logger.DefaultLogger) { + if logger.V(logger.InfoLevel) { logger.Infof("Starting [service] %s", s.Name()) } diff --git a/store/options.go b/store/options.go index 2a5685b4..b8dcf28d 100644 --- a/store/options.go +++ b/store/options.go @@ -23,11 +23,15 @@ type Options struct { Context context.Context } -func NewOptions() Options { - return Options{ +func NewOptions(opts ...Option) Options { + options := Options{ Logger: logger.DefaultLogger, Context: context.Background(), } + for _, o := range opts { + o(&options) + } + return options } // Option sets values in Options diff --git a/util/auth/auth.go b/util/auth/auth.go index b715a9d4..5cf52aa2 100644 --- a/util/auth/auth.go +++ b/util/auth/auth.go @@ -25,7 +25,7 @@ func Verify(a auth.Auth) error { if err != nil { return err } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("Auth [%v] Generated an auth account", a.String()) } @@ -67,7 +67,9 @@ func Verify(a auth.Auth) error { auth.WithExpiry(time.Minute*10), ) if err != nil { - logger.Warnf("[Auth] Error refreshing token: %v", err) + if logger.V(logger.WarnLevel) { + logger.Warnf("[Auth] Error refreshing token: %v", err) + } continue } diff --git a/util/kubernetes/client/client.go b/util/kubernetes/client/client.go index fb812969..e390e25a 100644 --- a/util/kubernetes/client/client.go +++ b/util/kubernetes/client/client.go @@ -227,7 +227,7 @@ func (c *client) Watch(r *Resource, opts ...WatchOption) (Watcher, error) { // NewService returns default micro kubernetes service definition func NewService(name, version, typ, namespace string) *Service { - if logger.V(logger.TraceLevel, logger.DefaultLogger) { + if logger.V(logger.TraceLevel) { logger.Tracef("kubernetes default service: name: %s, version: %s", name, version) } @@ -270,7 +270,7 @@ func NewService(name, version, typ, namespace string) *Service { // NewService returns default micro kubernetes deployment definition func NewDeployment(name, version, typ, namespace string) *Deployment { - if logger.V(logger.TraceLevel, logger.DefaultLogger) { + if logger.V(logger.TraceLevel) { logger.Tracef("kubernetes default deployment: name: %s, version: %s", name, version) } diff --git a/util/router/parse.go b/util/router/parse.go index 2ffcdd9f..9ba06101 100644 --- a/util/router/parse.go +++ b/util/router/parse.go @@ -102,21 +102,21 @@ type parser struct { // topLevelSegments is the target of this parser. func (p *parser) topLevelSegments() ([]segment, error) { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.TraceLevel) { logger.Debugf("Parsing %q", p.tokens) } segs, err := p.segments() if err != nil { return nil, err } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("accept segments: %q; %q", p.accepted, p.tokens) + if logger.V(logger.TraceLevel) { + logger.Tracef("accept segments: %q; %q", p.accepted, p.tokens) } if _, err := p.accept(typeEOF); err != nil { return nil, fmt.Errorf("unexpected token %q after segments %q", p.tokens[0], strings.Join(p.accepted, "")) } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("accept eof: %q; %q", p.accepted, p.tokens) + if logger.V(logger.TraceLevel) { + logger.Tracef("accept eof: %q; %q", p.accepted, p.tokens) } return segs, nil } @@ -127,8 +127,8 @@ func (p *parser) segments() ([]segment, error) { return nil, err } - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("accept segment: %q; %q", p.accepted, p.tokens) + if logger.V(logger.TraceLevel) { + logger.Tracef("accept segment: %q; %q", p.accepted, p.tokens) } segs := []segment{s} for { @@ -140,8 +140,8 @@ func (p *parser) segments() ([]segment, error) { return segs, err } segs = append(segs, s) - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("accept segment: %q; %q", p.accepted, p.tokens) + if logger.V(logger.TraceLevel) { + logger.Tracef("accept segment: %q; %q", p.accepted, p.tokens) } } } diff --git a/util/router/runtime.go b/util/router/runtime.go index eb497dee..bd17445e 100644 --- a/util/router/runtime.go +++ b/util/router/runtime.go @@ -62,7 +62,7 @@ func NewPattern(version int, ops []int, pool []string, verb string, opts ...Patt } if version != 1 { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("unsupported version: %d", version) } return Pattern{}, ErrInvalidPattern @@ -70,7 +70,7 @@ func NewPattern(version int, ops []int, pool []string, verb string, opts ...Patt l := len(ops) if l%2 != 0 { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.DebugLevel) { logger.Debugf("odd number of ops codes: %d", l) } return Pattern{}, ErrInvalidPattern @@ -95,8 +95,8 @@ func NewPattern(version int, ops []int, pool []string, verb string, opts ...Patt stack++ case OpPushM: if pushMSeen { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debug("pushM appears twice") + if logger.V(logger.TraceLevel) { + logger.Trace("pushM appears twice") } return Pattern{}, ErrInvalidPattern } @@ -104,8 +104,8 @@ func NewPattern(version int, ops []int, pool []string, verb string, opts ...Patt stack++ case OpLitPush: if op.operand < 0 || len(pool) <= op.operand { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("negative literal index: %d", op.operand) + if logger.V(logger.TraceLevel) { + logger.Tracef("negative literal index: %d", op.operand) } return Pattern{}, ErrInvalidPattern } @@ -115,23 +115,23 @@ func NewPattern(version int, ops []int, pool []string, verb string, opts ...Patt stack++ case OpConcatN: if op.operand <= 0 { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("negative concat size: %d", op.operand) + if logger.V(logger.TraceLevel) { + logger.Tracef("negative concat size: %d", op.operand) } return Pattern{}, ErrInvalidPattern } stack -= op.operand if stack < 0 { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debug("stack underflow") + if logger.V(logger.TraceLevel) { + logger.Trace("stack underflow") } return Pattern{}, ErrInvalidPattern } stack++ case OpCapture: if op.operand < 0 || len(pool) <= op.operand { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("variable name index out of bound: %d", op.operand) + if logger.V(logger.TraceLevel) { + logger.Tracef("variable name index out of bound: %d", op.operand) } return Pattern{}, ErrInvalidPattern } @@ -140,14 +140,14 @@ func NewPattern(version int, ops []int, pool []string, verb string, opts ...Patt vars = append(vars, v) stack-- if stack < 0 { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debug("stack underflow") + if logger.V(logger.DebugLevel) { + logger.Trace("stack underflow") } return Pattern{}, ErrInvalidPattern } default: - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("invalid opcode: %d", op.code) + if logger.V(logger.DebugLevel) { + logger.Tracef("invalid opcode: %d", op.code) } return Pattern{}, ErrInvalidPattern } @@ -171,7 +171,7 @@ func NewPattern(version int, ops []int, pool []string, verb string, opts ...Patt // MustPattern is a helper function which makes it easier to call NewPattern in variable initialization. func MustPattern(p Pattern, err error) Pattern { if err != nil { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { + if logger.V(logger.FatalLevel) { logger.Fatalf("Pattern initialization failed: %v", err) } }