diff --git a/api/resolver/subdomain/subdomain.go b/api/resolver/subdomain/subdomain.go index d8ece604..e4a1e0db 100644 --- a/api/resolver/subdomain/subdomain.go +++ b/api/resolver/subdomain/subdomain.go @@ -54,7 +54,9 @@ func (r *Resolver) Domain(req *http.Request) string { // extract the top level domain plus one (e.g. 'myapp.com') domain, err := publicsuffix.EffectiveTLDPlusOne(host) if err != nil { - logger.Debugf("Unable to extract domain from %v", host) + if logger.V(logger.DebugLevel) { + logger.Debugf("Unable to extract domain from %v", host) + } return "" } diff --git a/api/router/registry/registry.go b/api/router/registry/registry.go index e9cf51f0..2293317b 100644 --- a/api/router/registry/registry.go +++ b/api/router/registry/registry.go @@ -324,8 +324,8 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) { if !mMatch { continue } - if logger.V(logger.DebugLevel) { - logger.Debugf("api method match %s", req.Method) + if logger.V(logger.TraceLevel) { + logger.Tracef("api method match %s", req.Method) } // 2. try host @@ -347,21 +347,21 @@ func (r *registryRouter) Endpoint(req *http.Request) (*api.Service, error) { if !hMatch { continue } - if logger.V(logger.DebugLevel) { - 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 path via google.api path matching for _, pathreg := range cep.pathregs { matches, err := pathreg.Match(path, "") if err != nil { - if logger.V(logger.DebugLevel) { - 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.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() @@ -381,13 +381,13 @@ 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.Debugf("api pcre path not match %s != %v", path, pathreg) + if logger.V(logger.TraceLevel) { + logger.Tracef("api pcre path not match %s != %v", path, pathreg) } continue } - if logger.V(logger.DebugLevel) { - logger.Debugf("api pcre path match %s != %v", path, pathreg) + if logger.V(logger.TraceLevel) { + logger.Tracef("api pcre path match %s != %v", path, pathreg) } pMatch = true break diff --git a/api/server/acme/acme.go b/api/server/acme/acme.go index c962d038..3859976b 100644 --- a/api/server/acme/acme.go +++ b/api/server/acme/acme.go @@ -15,6 +15,7 @@ var ( // Provider is a ACME provider interface type Provider interface { + Init(...Option) error // Listen returns a new listener Listen(...string) (net.Listener, error) // TLSConfig returns a tls config diff --git a/api/server/acme/autocert/autocert.go b/api/server/acme/autocert/autocert.go index 8e7e1bf8..da3625a3 100644 --- a/api/server/acme/autocert/autocert.go +++ b/api/server/acme/autocert/autocert.go @@ -15,6 +15,10 @@ import ( // autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert type autocertProvider struct{} +func (a *autocertProvider) Init(opts ...acme.Option) error { + return nil +} + // Listen implements acme.Provider func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) { return autocert.NewListener(hosts...), nil diff --git a/api/server/acme/certmagic/certmagic.go b/api/server/acme/certmagic/certmagic.go index 85ad9b65..dd2b9039 100644 --- a/api/server/acme/certmagic/certmagic.go +++ b/api/server/acme/certmagic/certmagic.go @@ -3,13 +3,13 @@ package certmagic import ( "crypto/tls" + "fmt" "math/rand" "net" "time" "github.com/caddyserver/certmagic" "github.com/unistack-org/micro/v3/api/server/acme" - "github.com/unistack-org/micro/v3/logger" ) type certmagicProvider struct { @@ -48,6 +48,15 @@ func (c *certmagicProvider) TLSConfig(hosts ...string) (*tls.Config, error) { 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 func NewProvider(options ...acme.Option) acme.Provider { opts := acme.DefaultOptions() @@ -56,12 +65,6 @@ func NewProvider(options ...acme.Option) acme.Provider { 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{ opts: opts, } diff --git a/api/server/http/http.go b/api/server/http/http.go index dfaea8bc..e0ddde53 100644 --- a/api/server/http/http.go +++ b/api/server/http/http.go @@ -86,7 +86,10 @@ func (s *httpServer) Start() error { go func() { if err := http.Serve(l, s.mux); err != nil { // temporary fix - logger.Error(err) + if logger.V(logger.ErrorLevel) { + logger.Errorf("serve err: %v", err) + } + s.Stop() } }() diff --git a/service.go b/service.go index 95f756a6..bb3c5f89 100644 --- a/service.go +++ b/service.go @@ -10,7 +10,6 @@ import ( "github.com/unistack-org/micro/v3/client" "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/server" - "github.com/unistack-org/micro/v3/store" 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.Profile(&s.opts.Profile), ); err != nil { - logger.Fatal(err) + logger.Fatalf("[cmd] init failed: %v", err) } // execute the command // TODO: do this in service.Run() 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 { + var err error for _, fn := range s.opts.BeforeStart { - if err := fn(); err != nil { + if err = fn(); err != nil { return err } } - if err := s.opts.Server.Start(); err != nil { + if err = s.opts.Server.Start(); err != nil { return err } for _, fn := range s.opts.AfterStart { - if err := fn(); err != nil { + if err = fn(); err != nil { return err } } @@ -114,25 +110,24 @@ func (s *service) Start() error { } func (s *service) Stop() error { - var gerr error - + var err error for _, fn := range s.opts.BeforeStop { - if err := fn(); err != nil { - gerr = err + if err = fn(); err != nil { + return err } } - if err := s.opts.Server.Stop(); err != nil { + if err = s.opts.Server.Stop(); err != nil { return err } for _, fn := range s.opts.AfterStop { - if err := fn(); err != nil { - gerr = err + if err = fn(); err != nil { + return err } } - return gerr + return nil } func (s *service) Run() error { diff --git a/tunnel/broker/broker.go b/tunnel/broker/broker.go index e09a22ef..5790d0eb 100644 --- a/tunnel/broker/broker.go +++ b/tunnel/broker/broker.go @@ -117,10 +117,14 @@ func (t *tunSubscriber) run() { // receive message m := new(transport.Message) if err := c.Recv(m); err != nil { - logger.Error(err) - if err = c.Close(); err != nil { + if logger.V(logger.ErrorLevel) { logger.Error(err) } + if err = c.Close(); err != nil { + if logger.V(logger.ErrorLevel) { + logger.Error(err) + } + } continue }