-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from foomo/feature/referrer
feat: http referrer
- Loading branch information
Showing
11 changed files
with
349 additions
and
23 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,22 @@ | ||
package keelerrors | ||
|
||
import "errors" | ||
|
||
type wrappedError struct { | ||
err error | ||
cause error | ||
} | ||
|
||
// NewWrappedError returns a new wrapped error | ||
func NewWrappedError(err, cause error) error { | ||
return &wrappedError{ | ||
err: err, | ||
cause: cause, | ||
} | ||
} | ||
|
||
func (e *wrappedError) As(target interface{}) bool { | ||
return errors.As(e.err, target) || errors.As(e.cause, target) | ||
} | ||
|
||
func (e *wrappedError) Is(target error) bool { | ||
return errors.Is(e.err, target) || errors.Is(e.cause, target) | ||
} | ||
|
||
func (e *wrappedError) Cause() error { | ||
return e.cause | ||
} | ||
|
||
func (e *wrappedError) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
func (e *wrappedError) Error() string { | ||
return e.err.Error() + ": " + e.cause.Error() | ||
} | ||
|
||
func (e *wrappedError) Unwrap() []error { | ||
return []error{e.err, e.cause} | ||
} |
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,75 @@ | ||
package keelerrors_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
keelerrors "github.com/foomo/keel/errors" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func ExampleNewWrappedError() { | ||
parentErr := errors.New("parent") | ||
childErr := errors.New("child") | ||
wrappedErr := keelerrors.NewWrappedError(parentErr, childErr) | ||
fmt.Println(parentErr) | ||
fmt.Println(childErr) | ||
fmt.Println(wrappedErr) | ||
// Output: | ||
// parent | ||
// child | ||
// parent: child | ||
} | ||
|
||
func TestNewWrappedError(t *testing.T) { | ||
parentErr := errors.New("parent") | ||
childErr := errors.New("child") | ||
assert.Error(t, keelerrors.NewWrappedError(parentErr, childErr)) | ||
} | ||
|
||
func TestWrapped(t *testing.T) { | ||
parentErr := errors.New("parent") | ||
childErr := errors.New("child") | ||
assert.Error(t, keelerrors.NewWrappedError(parentErr, childErr)) | ||
} | ||
|
||
func Test_wrappedError_As(t *testing.T) { | ||
type ( | ||
Parent struct { | ||
error | ||
} | ||
Child struct { | ||
error | ||
} | ||
) | ||
parentErr := &Parent{error: errors.New("parent")} | ||
childErr := &Child{error: errors.New("parent")} | ||
wrappedErr := keelerrors.NewWrappedError(parentErr, childErr) | ||
|
||
var ( | ||
p *Parent | ||
c *Child | ||
) | ||
if assert.ErrorAs(t, wrappedErr, &p) { | ||
assert.EqualError(t, p, parentErr.Error()) | ||
} | ||
if assert.ErrorAs(t, wrappedErr, &c) { | ||
assert.EqualError(t, c, childErr.Error()) | ||
} | ||
} | ||
|
||
func Test_wrappedError_Error(t *testing.T) { | ||
parentErr := errors.New("parent") | ||
childErr := errors.New("child") | ||
wrappedErr := keelerrors.NewWrappedError(parentErr, childErr) | ||
assert.Equal(t, wrappedErr.Error(), "parent: child") | ||
} | ||
|
||
func Test_wrappedError_Is(t *testing.T) { | ||
parentErr := errors.New("parent") | ||
childErr := errors.New("child") | ||
wrappedErr := keelerrors.NewWrappedError(parentErr, childErr) | ||
assert.ErrorIs(t, wrappedErr, parentErr) | ||
assert.ErrorIs(t, wrappedErr, childErr) | ||
} |
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,6 +1,6 @@ | ||
module github.com/foomo/keel/example | ||
|
||
go 1.19 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 | ||
|
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,6 +1,6 @@ | ||
module github.com/foomo/keel | ||
|
||
go 1.19 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/avast/retry-go v3.0.0+incompatible | ||
|
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,19 @@ | ||
package context | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
const ContextKeyReferrer contextKey = "referrer" | ||
|
||
func GetReferrer(ctx context.Context) (string, bool) { | ||
if value, ok := ctx.Value(ContextKeyReferrer).(string); ok { | ||
return value, true | ||
} else { | ||
return "", false | ||
} | ||
} | ||
|
||
func SetReferrer(ctx context.Context, referer string) context.Context { | ||
return context.WithValue(ctx, ContextKeyReferrer, referer) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/foomo/keel/net/http/context" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ( | ||
RefererOptions struct { | ||
RequestHeader []string | ||
SetContext bool | ||
} | ||
RefererOption func(*RefererOptions) | ||
) | ||
|
||
// GetDefaultRefererOptions returns the default options | ||
func GetDefaultRefererOptions() RefererOptions { | ||
return RefererOptions{ | ||
RequestHeader: []string{"X-Referer", "X-Referrer", "Referer", "Referrer"}, | ||
SetContext: true, | ||
} | ||
} | ||
|
||
// RefererWithRequestHeader middleware option | ||
func RefererWithRequestHeader(v ...string) RefererOption { | ||
return func(o *RefererOptions) { | ||
o.RequestHeader = append(o.RequestHeader, v...) | ||
} | ||
} | ||
|
||
// RefererWithSetContext middleware option | ||
func RefererWithSetContext(v bool) RefererOption { | ||
return func(o *RefererOptions) { | ||
o.SetContext = v | ||
} | ||
} | ||
|
||
// Referer middleware | ||
func Referer(opts ...RefererOption) Middleware { | ||
options := GetDefaultRefererOptions() | ||
for _, opt := range opts { | ||
if opt != nil { | ||
opt(&options) | ||
} | ||
} | ||
return RefererWithOptions(options) | ||
} | ||
|
||
// RefererWithOptions middleware | ||
func RefererWithOptions(opts RefererOptions) Middleware { | ||
return func(l *zap.Logger, name string, next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
var referer string | ||
for _, value := range opts.RequestHeader { | ||
if referer = r.Header.Get(value); referer != "" { | ||
break | ||
} | ||
} | ||
if referer != "" && opts.SetContext { | ||
r = r.WithContext(context.SetReferrer(r.Context(), referer)) | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
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,51 @@ | ||
package roundtripware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"go.uber.org/zap" | ||
|
||
keelhttpcontext "github.com/foomo/keel/net/http/context" | ||
) | ||
|
||
type ( | ||
ReferrerOptions struct { | ||
Header string | ||
} | ||
ReferrerOption func(*ReferrerOptions) | ||
ReferrerGenerator func() string | ||
) | ||
|
||
// GetDefaultReferrerOptions returns the default options | ||
func GetDefaultReferrerOptions() ReferrerOptions { | ||
return ReferrerOptions{ | ||
Header: "X-Referrer", | ||
} | ||
} | ||
|
||
// ReferrerWithHeader middleware option | ||
func ReferrerWithHeader(v string) ReferrerOption { | ||
return func(o *ReferrerOptions) { | ||
o.Header = v | ||
} | ||
} | ||
|
||
// Referrer returns a RoundTripper which prints out the request & response object | ||
func Referrer(opts ...ReferrerOption) RoundTripware { | ||
o := GetDefaultReferrerOptions() | ||
for _, opt := range opts { | ||
if opt != nil { | ||
opt(&o) | ||
} | ||
} | ||
return func(l *zap.Logger, next Handler) Handler { | ||
return func(r *http.Request) (*http.Response, error) { | ||
if value := r.Header.Get(o.Header); value == "" { | ||
if value, ok := keelhttpcontext.GetReferrer(r.Context()); ok && value != "" { | ||
r.Header.Set(o.Header, value) | ||
} | ||
} | ||
return next(r) | ||
} | ||
} | ||
} |
Oops, something went wrong.