Skip to content

Commit

Permalink
[server/kit] rearranging stack so logger is available within HTTPMidd…
Browse files Browse the repository at this point in the history
…leware (#232)

* rearanging stack so logger is available within HTTPMiddleware

* remove space
  • Loading branch information
jprobinson authored Sep 23, 2019
1 parent fcf6e85 commit f94ac06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
31 changes: 15 additions & 16 deletions server/kit/kitserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,33 +186,32 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.errs.Flush()
}
}()
s.handler.ServeHTTP(w, r)

// populate context with helpful keys
ctx := httptransport.PopulateRequestContext(r.Context(), r)

// add google trace header to use in tracing and logging
ctx = context.WithValue(ctx, ContextKeyCloudTraceContext,
r.Header.Get("X-Cloud-Trace-Context"))

// add a request scoped logger to the context
ctx = SetLogger(ctx, AddLogKeyVals(ctx, s.logger))

s.handler.ServeHTTP(w, r.WithContext(ctx))
}

func (s *Server) register(svc Service) {
s.svc = svc
s.handler = s.svc.HTTPMiddleware(s.mux)

opts := []httptransport.ServerOption{
// populate context with helpful keys
httptransport.ServerBefore(func(ctx context.Context, r *http.Request) context.Context {
ctx = httptransport.PopulateRequestContext(ctx, r)
// add google trace header to use in tracing and logging
return context.WithValue(ctx, ContextKeyCloudTraceContext,
r.Header.Get("X-Cloud-Trace-Context"))
}),
// inject the server logger into every request context
httptransport.ServerBefore(func(ctx context.Context, _ *http.Request) context.Context {
return SetLogger(ctx, AddLogKeyVals(ctx, s.logger))
}),
}
opts = append(opts, svc.HTTPOptions()...)

const warmupPath = "/_ah/warmup"
var (
healthzFound bool
warmupFound bool
)

opts := svc.HTTPOptions()

// register all endpoints with our wrappers & default decoders/encoders
for path, epMethods := range svc.HTTPEndpoints() {
for method, ep := range epMethods {
Expand Down
5 changes: 4 additions & 1 deletion server/kit/kitserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ func (s *server) Middleware(e endpoint.Endpoint) endpoint.Endpoint {
}

func (s *server) HTTPMiddleware(h http.Handler) http.Handler {
return gserver.CORSHandler(h, "")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
kit.LogDebug(r.Context(), "logging in the HTTP middleware!")
gserver.CORSHandler(h, "").ServeHTTP(w, r)
})
}

func (s *server) HTTPOptions() []httptransport.ServerOption {
Expand Down

0 comments on commit f94ac06

Please sign in to comment.