fixup logger usage (#33)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-09-05 02:43:16 +03:00 committed by GitHub
parent c576749b57
commit e7d418183b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 41 deletions

View File

@ -54,7 +54,9 @@ func (r *Resolver) Domain(req *http.Request) string {
// extract the top level domain plus one (e.g. 'myapp.com') // extract the top level domain plus one (e.g. 'myapp.com')
domain, err := publicsuffix.EffectiveTLDPlusOne(host) domain, err := publicsuffix.EffectiveTLDPlusOne(host)
if err != nil { if err != nil {
if logger.V(logger.DebugLevel) {
logger.Debugf("Unable to extract domain from %v", host) logger.Debugf("Unable to extract domain from %v", host)
}
return "" return ""
} }

View File

@ -324,8 +324,8 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
if !mMatch { if !mMatch {
continue continue
} }
if logger.V(logger.DebugLevel) { if logger.V(logger.TraceLevel) {
logger.Debugf("api method match %s", req.Method) logger.Tracef("api method match %s", req.Method)
} }
// 2. try host // 2. try host
@ -347,21 +347,21 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
if !hMatch { if !hMatch {
continue continue
} }
if logger.V(logger.DebugLevel) { if logger.V(logger.TraceLevel) {
logger.Debugf("api host match %s", req.URL.Host) logger.Tracef("api host match %s", req.URL.Host)
} }
// 3. try path via google.api path matching // 3. try path via google.api path matching
for _, pathreg := range cep.pathregs { for _, pathreg := range cep.pathregs {
matches, err := pathreg.Match(path, "") matches, err := pathreg.Match(path, "")
if err != nil { if err != nil {
if logger.V(logger.DebugLevel) { if logger.V(logger.TraceLevel) {
logger.Debugf("api gpath not match %s != %v", path, pathreg) logger.Tracef("api gpath not match %s != %v", path, pathreg)
} }
continue continue
} }
if logger.V(logger.DebugLevel) { if logger.V(logger.TraceLevel) {
logger.Debugf("api gpath match %s = %v", path, pathreg) logger.Tracef("api gpath match %s = %v", path, pathreg)
} }
pMatch = true pMatch = true
ctx := req.Context() ctx := req.Context()
@ -381,13 +381,13 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) {
// 4. try path via pcre path matching // 4. try path via pcre path matching
for _, pathreg := range cep.pcreregs { for _, pathreg := range cep.pcreregs {
if !pathreg.MatchString(req.URL.Path) { if !pathreg.MatchString(req.URL.Path) {
if logger.V(logger.DebugLevel) { if logger.V(logger.TraceLevel) {
logger.Debugf("api pcre path not match %s != %v", path, pathreg) logger.Tracef("api pcre path not match %s != %v", path, pathreg)
} }
continue continue
} }
if logger.V(logger.DebugLevel) { if logger.V(logger.TraceLevel) {
logger.Debugf("api pcre path match %s != %v", path, pathreg) logger.Tracef("api pcre path match %s != %v", path, pathreg)
} }
pMatch = true pMatch = true
break break

View File

@ -15,6 +15,7 @@ var (
// Provider is a ACME provider interface // Provider is a ACME provider interface
type Provider interface { type Provider interface {
Init(...Option) error
// Listen returns a new listener // Listen returns a new listener
Listen(...string) (net.Listener, error) Listen(...string) (net.Listener, error)
// TLSConfig returns a tls config // TLSConfig returns a tls config

View File

@ -15,6 +15,10 @@ import (
// autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert // autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert
type autocertProvider struct{} type autocertProvider struct{}
func (a *autocertProvider) Init(opts ...acme.Option) error {
return nil
}
// Listen implements acme.Provider // Listen implements acme.Provider
func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) { func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) {
return autocert.NewListener(hosts...), nil return autocert.NewListener(hosts...), nil

View File

@ -3,13 +3,13 @@ package certmagic
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"math/rand" "math/rand"
"net" "net"
"time" "time"
"github.com/caddyserver/certmagic" "github.com/caddyserver/certmagic"
"github.com/unistack-org/micro/v3/api/server/acme" "github.com/unistack-org/micro/v3/api/server/acme"
"github.com/unistack-org/micro/v3/logger"
) )
type certmagicProvider struct { type certmagicProvider struct {
@ -48,6 +48,15 @@ func (c *certmagicProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
return certmagic.TLS(hosts) return certmagic.TLS(hosts)
} }
func (p *certmagicProvider) Init(opts ...acme.Option) error {
if p.opts.Cache != nil {
if _, ok := p.opts.Cache.(certmagic.Storage); !ok {
return fmt.Errorf("ACME: cache provided doesn't implement certmagic's Storage interface")
}
}
return nil
}
// NewProvider returns a certmagic provider // NewProvider returns a certmagic provider
func NewProvider(options ...acme.Option) acme.Provider { func NewProvider(options ...acme.Option) acme.Provider {
opts := acme.DefaultOptions() opts := acme.DefaultOptions()
@ -56,12 +65,6 @@ func NewProvider(options ...acme.Option) acme.Provider {
o(&opts) o(&opts)
} }
if opts.Cache != nil {
if _, ok := opts.Cache.(certmagic.Storage); !ok {
logger.Fatal("ACME: cache provided doesn't implement certmagic's Storage interface")
}
}
return &certmagicProvider{ return &certmagicProvider{
opts: opts, opts: opts,
} }

View File

@ -86,7 +86,10 @@ func (s *httpServer) Start() error {
go func() { go func() {
if err := http.Serve(l, s.mux); err != nil { if err := http.Serve(l, s.mux); err != nil {
// temporary fix // temporary fix
logger.Error(err) if logger.V(logger.ErrorLevel) {
logger.Errorf("serve err: %v", err)
}
s.Stop()
} }
}() }()

View File

@ -10,7 +10,6 @@ import (
"github.com/unistack-org/micro/v3/client" "github.com/unistack-org/micro/v3/client"
"github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/server" "github.com/unistack-org/micro/v3/server"
"github.com/unistack-org/micro/v3/store"
signalutil "github.com/unistack-org/micro/v3/util/signal" signalutil "github.com/unistack-org/micro/v3/util/signal"
) )
@ -62,18 +61,14 @@ func (s *service) Init(opts ...Option) {
cmd.Store(&s.opts.Store), cmd.Store(&s.opts.Store),
cmd.Profile(&s.opts.Profile), cmd.Profile(&s.opts.Profile),
); err != nil { ); err != nil {
logger.Fatal(err) logger.Fatalf("[cmd] init failed: %v", err)
} }
// execute the command // execute the command
// TODO: do this in service.Run() // TODO: do this in service.Run()
if err := s.opts.Cmd.Run(); err != nil { if err := s.opts.Cmd.Run(); err != nil {
logger.Fatal(err) logger.Fatalf("[cmd] run failed: %v", err)
} }
// Explicitly set the table name to the service name
name := s.opts.Cmd.App().Name
s.opts.Store.Init(store.Table(name))
}) })
} }
@ -94,18 +89,19 @@ func (s *service) String() string {
} }
func (s *service) Start() error { func (s *service) Start() error {
var err error
for _, fn := range s.opts.BeforeStart { for _, fn := range s.opts.BeforeStart {
if err := fn(); err != nil { if err = fn(); err != nil {
return err return err
} }
} }
if err := s.opts.Server.Start(); err != nil { if err = s.opts.Server.Start(); err != nil {
return err return err
} }
for _, fn := range s.opts.AfterStart { for _, fn := range s.opts.AfterStart {
if err := fn(); err != nil { if err = fn(); err != nil {
return err return err
} }
} }
@ -114,25 +110,24 @@ func (s *service) Start() error {
} }
func (s *service) Stop() error { func (s *service) Stop() error {
var gerr error var err error
for _, fn := range s.opts.BeforeStop { for _, fn := range s.opts.BeforeStop {
if err := fn(); err != nil { if err = fn(); err != nil {
gerr = err return err
} }
} }
if err := s.opts.Server.Stop(); err != nil { if err = s.opts.Server.Stop(); err != nil {
return err return err
} }
for _, fn := range s.opts.AfterStop { for _, fn := range s.opts.AfterStop {
if err := fn(); err != nil { if err = fn(); err != nil {
gerr = err return err
} }
} }
return gerr return nil
} }
func (s *service) Run() error { func (s *service) Run() error {

View File

@ -117,10 +117,14 @@ func (t *tunSubscriber) run() {
// receive message // receive message
m := new(transport.Message) m := new(transport.Message)
if err := c.Recv(m); err != nil { if err := c.Recv(m); err != nil {
if logger.V(logger.ErrorLevel) {
logger.Error(err) logger.Error(err)
}
if err = c.Close(); err != nil { if err = c.Close(); err != nil {
if logger.V(logger.ErrorLevel) {
logger.Error(err) logger.Error(err)
} }
}
continue continue
} }