diff --git a/app.go b/app.go index a886e51..51e9c95 100644 --- a/app.go +++ b/app.go @@ -6,6 +6,7 @@ import ( "os/signal" "reflect" "runtime" + "strconv" "sync/atomic" "syscall" "time" @@ -35,12 +36,19 @@ func (s *Service) Name() string { } func (s *Service) Debug() bool { + if s.opts != nil { + return s.opts.debug + } return false } func (s *Service) Version() string { - return s.opts.version + if s.opts != nil { + return s.opts.version + } + return "" } + func (s *Service) Metadata() map[string]string { if s.service == nil { return nil @@ -255,6 +263,7 @@ func New(cbs ...Option) *Service { registrarTimeout: time.Second * 30, }, } + s.opts.debug, _ = strconv.ParseBool("AEUS_DEBUG") s.opts.metadata = make(map[string]string) for _, cb := range cbs { cb(s.opts) diff --git a/middleware/auth/jwt.go b/middleware/auth/jwt.go index b0137c7..fddf9b2 100644 --- a/middleware/auth/jwt.go +++ b/middleware/auth/jwt.go @@ -108,9 +108,7 @@ func JWT(keyFunc jwt.Keyfunc, cbs ...Option) middleware.Middleware { return err } } - if strings.HasPrefix(token, bearerWord) { - token = strings.TrimPrefix(token, bearerWord) - } + token, _ = strings.CutPrefix(token, bearerWord) var ( ti *jwt.Token ) diff --git a/options.go b/options.go index 10e437f..bbd1103 100644 --- a/options.go +++ b/options.go @@ -2,6 +2,7 @@ package aeus import ( "context" + "maps" "time" "git.nobla.cn/golang/aeus/pkg/logger" @@ -18,6 +19,7 @@ type options struct { servers []Server endpoints []string scope Scope + debug bool registrarTimeout time.Duration registry registry.Registry serviceLoader ServiceLoader @@ -41,9 +43,7 @@ func WithMetadata(metadata map[string]string) Option { if o.metadata == nil { o.metadata = make(map[string]string) } - for k, v := range metadata { - o.metadata[k] = v - } + maps.Copy(o.metadata, metadata) } } @@ -71,6 +71,12 @@ func WithScope(scope Scope) Option { } } +func WithDebug(debug bool) Option { + return func(o *options) { + o.debug = debug + } +} + func WithServiceLoader(loader ServiceLoader) Option { return func(o *options) { o.serviceLoader = loader diff --git a/pkg/logger/default.go b/pkg/logger/slog.go similarity index 100% rename from pkg/logger/default.go rename to pkg/logger/slog.go diff --git a/transport/http/context.go b/transport/http/context.go index 1617b68..3c1d76f 100644 --- a/transport/http/context.go +++ b/transport/http/context.go @@ -22,6 +22,10 @@ func (c *Context) Context() context.Context { return c.ctx.Request.Context() } +func (c *Context) Gin() *gin.Context { + return c.ctx +} + func (c *Context) Request() *http.Request { return c.ctx.Request }