-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update accesslog, remove IgnoreFaviconRoute and IgnoreFaviconHandler
- Loading branch information
Showing
6 changed files
with
53 additions
and
53 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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.
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
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