Skip to content

Commit

Permalink
Move addLoggerToContextFn to a middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbus committed Aug 30, 2018
1 parent caeeb34 commit 39a1a8e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
8 changes: 2 additions & 6 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,10 @@ func (t *HTTPTransport) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.httpmiddleware(t.mux).ServeHTTP(w, r)
}

// RegisterEndpoints registers all configured endpoints, wraps them with the m middleware and
// configures the logger.
func (t *HTTPTransport) RegisterEndpoints(m endpoint.Middleware, fn AddLoggerToContextFn) error {
// RegisterEndpoints registers all configured endpoints, wraps them with the m middleware.
func (t *HTTPTransport) RegisterEndpoints(m endpoint.Middleware) error {
opts := []kithttp.ServerOption{
kithttp.ServerBefore(kithttp.PopulateRequestContext),
kithttp.ServerBefore(func(ctx context.Context, _ *http.Request) context.Context {
return fn(ctx)
}),
}
opts = append(opts, t.opts...)

Expand Down
10 changes: 9 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kitty
import (
"context"

"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
kithttp "github.com/go-kit/kit/transport/http"
)
Expand Down Expand Up @@ -49,7 +50,14 @@ func (s *Server) addLoggerToContext(ctx context.Context) context.Context {
return context.WithValue(ctx, logKey, l)
}

type AddLoggerToContextFn func(ctx context.Context) context.Context
func (s *Server) addLoggerToContextMiddleware(m endpoint.Middleware) endpoint.Middleware {
return func(e endpoint.Endpoint) endpoint.Endpoint {
e = m(e)
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
return e(s.addLoggerToContext(ctx), request)
}
}
}

// Logger will return the logger that has been injected into the context by the kitty
// server. This function can only be called from an endpoint.
Expand Down
3 changes: 2 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func NewServer(t ...Transport) *Server {
// Run starts the server.
func (s *Server) Run(ctx context.Context) error {
ctx = s.addLoggerToContext(ctx)
m := s.addLoggerToContextMiddleware(s.middleware)
for _, t := range s.transports {
if err := t.RegisterEndpoints(s.middleware, s.addLoggerToContext); err != nil {
if err := t.RegisterEndpoints(m); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

// Transport is the interface all transports must implement.
type Transport interface {
// RegisterEndpoints registers all endpoints, and injects the AddLoggerToContextFn call to configure the logger.
RegisterEndpoints(m endpoint.Middleware, fn AddLoggerToContextFn) error
// RegisterEndpoints registers all endpoints. Endpoints needs to be wrapped with the specified middleware.
RegisterEndpoints(m endpoint.Middleware) error
// Start starts the transport.
Start(ctx context.Context) error
// Shutdown shutdowns the transport.
Expand Down

0 comments on commit 39a1a8e

Please sign in to comment.