Skip to content

Commit

Permalink
Update accesslog, remove IgnoreFaviconRoute and IgnoreFaviconHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed May 3, 2024
1 parent be364b7 commit 5d2efca
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 53 deletions.
7 changes: 6 additions & 1 deletion _examples/restart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func main() {
_, _ = w.Write([]byte(strconv.FormatUint(restartCount.Load(), 10)))
}),
})
mux.HandleRoute(accesslog.IgnoreFaviconRoute())
mux.HandleRoute(serv.Route{
Name: "favicon",
Method: http.MethodGet,
Pattern: "/favicon.ico",
Handler: accesslog.IgnoreHandler(serv.NoContentHandler()),
})
mux.HandleRoute(serv.Route{
Name: "restart",
Pattern: "/restart",
Expand Down
37 changes: 0 additions & 37 deletions accesslog/context.go

This file was deleted.

43 changes: 31 additions & 12 deletions accesslog/ignore.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
// Copyright (c) 2024, Roel Schut. All rights reserved.
// Copyright (c) 2022, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package accesslog

import (
"github.com/go-pogo/serv"
"context"
"net/http"
)

func IgnoreFaviconHandler() http.HandlerFunc {
return func(wri http.ResponseWriter, req *http.Request) {
SetShouldIgnore(req.Context(), true)
wri.WriteHeader(http.StatusNoContent)
// ShouldIgnore returns if the [http.Request]'s context contains a
// "should ignore" value set using [SetShouldIgnore].
func ShouldIgnore(ctx context.Context) bool {
if v := ctx.Value(ctxSettingsKey{}); v != nil {
return v.(*ctxSettings).shouldIgnore
}
return false
}

func IgnoreFaviconRoute() serv.Route {
return serv.Route{
Name: "favicon",
Method: http.MethodGet,
Pattern: "/favicon.ico",
Handler: IgnoreFaviconHandler(),
// SetShouldIgnore marks the context from a [http.Request] as "should ignore".
// It return true if the context was successfully updated. If the context
// does not already contain a reference to
func SetShouldIgnore(ctx context.Context, ignore bool) bool {
if _, settings, exists := withSettings(ctx); exists {
settings.shouldIgnore = ignore
return true
}
return false
}

// IgnoreHandler returns a [http.Handler] that marks the [http.Request] as
// "should ignore".
func IgnoreHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(wri http.ResponseWriter, req *http.Request) {
ctx, settings, existing := withSettings(req.Context())
settings.shouldIgnore = true
if !existing {
req = req.WithContext(ctx)
}

next.ServeHTTP(wri, req)
wri.WriteHeader(http.StatusNoContent)
})
}
File renamed without changes.
4 changes: 1 addition & 3 deletions accesslog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func DefaultLogger(l *log.Logger) Logger {

var _ Logger = (*defaultLogger)(nil)

type defaultLogger struct {
*log.Logger
}
type defaultLogger struct{ *log.Logger }

func (l *defaultLogger) Log(_ context.Context, det Details, req *http.Request) {
handlerName := det.HandlerName
Expand Down
15 changes: 15 additions & 0 deletions accesslog/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ func (c *handler) ServeHTTP(wri http.ResponseWriter, req *http.Request) {
c.log.Log(ctx, det, req.Clone(&noopCtx{ctx}))
}

type ctxSettingsKey struct{}

type ctxSettings struct {
shouldIgnore bool
}

func withSettings(ctx context.Context) (context.Context, *ctxSettings, bool) {
if v := ctx.Value(ctxSettingsKey{}); v != nil {
return ctx, v.(*ctxSettings), true
}

v := new(ctxSettings)
return context.WithValue(ctx, ctxSettingsKey{}, v), v, false
}

var _ context.Context = (*noopCtx)(nil)

// A noopCtx is similar to context.Background as it is never canceled and has
Expand Down

0 comments on commit 5d2efca

Please sign in to comment.