-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add context logger middleware and fixed get request decoding
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Package ctxlogger provides an [htadaptor.Middleware] that injects | ||
a given logger into context so that it can be recovered | ||
later in the stack for use. | ||
*/ | ||
package ctxlogger | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/dkotik/htadaptor" | ||
) | ||
|
||
type contextKeyType struct{} | ||
|
||
var contextKey = contextKeyType{} | ||
|
||
func FromContext(ctx context.Context) *slog.Logger { | ||
logger, ok := ctx.Value(contextKey).(*slog.Logger) | ||
if ok { | ||
return logger | ||
} | ||
return slog.Default() | ||
} | ||
|
||
func NewContext(parent context.Context, l *slog.Logger) context.Context { | ||
if l == nil { | ||
l = slog.Default() | ||
} | ||
return context.WithValue(parent, contextKey, l) | ||
} | ||
|
||
// New creates an [htadaptor.Middleware] that injects [slog.Logger] | ||
// into request context so it can be recovered later using [FromContext]. | ||
func New(logger *slog.Logger) htadaptor.Middleware { | ||
return func(next http.Handler) http.Handler { | ||
if next == nil { | ||
panic("cannot use a <nil> next handler") | ||
} | ||
return http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
next.ServeHTTP(w, r.WithContext(NewContext(r.Context(), logger))) | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters