Skip to content

Commit

Permalink
Add event hook for redirect function in ServeHTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
aharshit29 committed Oct 8, 2024
1 parent ffabb35 commit 47a67f1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ install-packages:
@echo "Mounting git pre-push hook"
cp .git-pre-push-hook .git/hooks/pre-push
@echo "Installing python packages..."
pip3 install --user yq
#pip3 install --user yq

.PHONY: install-tools
# set GO111MODULE to off to compile ancient tools within the vendor directory
Expand Down
7 changes: 7 additions & 0 deletions runtime/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package zanzibar

import "net/http"

// Context Variables
const (
// ToCapture set to true if events have to be captured
Expand All @@ -35,6 +37,7 @@ const (

type EventHandlerFn func([]Event) error
type EnableEventGenFn func(string, string) bool
type RedirectFn func(w http.ResponseWriter, r *http.Request) bool

type Event interface {
Name() string
Expand Down Expand Up @@ -127,3 +130,7 @@ func NoOpEventHandler(events []Event) error {
func NoOpEventGen(_, _ string) bool {
return false
}

func NoOpRedirectFn(_ http.ResponseWriter, _ *http.Request) bool {
return false
}
11 changes: 9 additions & 2 deletions runtime/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ import (
"time"

metricCollector "github.com/afex/hystrix-go/hystrix/metric_collector"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/uber-go/tally"
"github.com/uber-go/tally/m3"
jaegerConfig "github.com/uber/jaeger-client-go/config"
jaegerLibTally "github.com/uber/jaeger-lib/metrics/tally"
"github.com/uber/tchannel-go"
"github.com/uber/zanzibar/v2/runtime/jsonwrapper"
"github.com/uber/zanzibar/v2/runtime/plugins"
"go.uber.org/yarpc"
Expand Down Expand Up @@ -83,6 +81,7 @@ type Options struct {
NotFoundHandler func(*Gateway) http.HandlerFunc
TracerProvider func(*Gateway) (opentracing.Tracer, io.Closer, error)
EventProvider func(*Gateway) (EnableEventGenFn, EventHandlerFn)
RedirectProvider func(*Gateway) RedirectFn
// If present, request uuid is retrieved from the incoming request
// headers using the key, and put on the context. Otherwise, a new
// uuid is created for the incoming request.
Expand Down Expand Up @@ -114,6 +113,7 @@ type Gateway struct {
JSONWrapper jsonwrapper.JSONWrapper
EventHandler EventHandlerFn
EnableEventGen EnableEventGenFn
RedirectHandlerFn RedirectFn
// gRPC client dispatcher for gRPC client lifecycle management
GRPCClientDispatcher *yarpc.Dispatcher

Expand Down Expand Up @@ -274,6 +274,13 @@ func CreateGateway(
gateway.EventHandler = NoOpEventHandler
}

if opts.RedirectProvider != nil {
redirectFn := opts.RedirectProvider(gateway)
gateway.RedirectHandlerFn = redirectFn
} else {
gateway.RedirectHandlerFn = NoOpRedirectFn
}

if opts.NotFoundHandler != nil &&
config.ContainsKey("http.notFoundHandler.custom") &&
config.MustGetBoolean("http.notFoundHandler.custom") {
Expand Down
40 changes: 37 additions & 3 deletions runtime/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ import (
"sync"
"testing"

"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/uber-go/tally"
"github.com/uber/jaeger-client-go"
"github.com/uber/tchannel-go"
"github.com/uber/zanzibar/v2/runtime/jsonwrapper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -367,3 +364,40 @@ func TestGatewayWithEventHandler(t *testing.T) {

})
}

func TestGatewayWithRedirectHandler(t *testing.T) {

rawCfgMap := map[string]interface{}{}
var metricsBackend tally.CachedStatsReporter

opts := &Options{
GetContextScopeExtractors: nil,
GetContextFieldExtractors: nil,
JSONWrapper: jsonwrapper.NewDefaultJSONWrapper(),
MetricsBackend: metricsBackend,
NotFoundHandler: func(gateway *Gateway) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {}
},
}

t.Run("without redirect handler", func(t *testing.T) {
cfg := NewStaticConfigOrDie(nil, rawCfgMap)
g, err := CreateGateway(cfg, opts)
assert.Nil(t, err)
assert.NotEqual(t, NoOpRedirectFn, g.RedirectHandlerFn)
})

t.Run("with redirect handler", func(t *testing.T) {
redirectFn := func(_ http.ResponseWriter, _ *http.Request) bool {
return false
}
opts.RedirectProvider = func(gateway *Gateway) RedirectFn {
return redirectFn
}
cfg := NewStaticConfigOrDie(nil, rawCfgMap)
g, err := CreateGateway(cfg, opts)
assert.Nil(t, err)
assert.Equal(t, redirectFn, g.RedirectHandlerFn)

})
}
9 changes: 8 additions & 1 deletion runtime/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"net/http"
"net/url"

"github.com/opentracing/opentracing-go"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"github.com/uber-go/tally"
Expand Down Expand Up @@ -91,6 +90,7 @@ type RouterEndpoint struct {
config *StaticConfig
eventHandler EventHandlerFn
enableEventGen EnableEventGenFn
redirectFn RedirectFn
}

// NewRouterEndpoint creates an endpoint that can be registered to HTTPRouter
Expand All @@ -106,9 +106,11 @@ func NewRouterEndpoint(
// continue working as is.
eh := NoOpEventHandler
eg := NoOpEventGen
rh := NoOpRedirectFn
if deps.Gateway != nil {
eh = deps.Gateway.EventHandler
eg = deps.Gateway.EnableEventGen
rh = deps.Gateway.RedirectHandlerFn
}

return &RouterEndpoint{
Expand All @@ -123,6 +125,7 @@ func NewRouterEndpoint(
config: deps.Config,
eventHandler: eh,
enableEventGen: eg,
redirectFn: rh,
}
}

Expand All @@ -139,6 +142,10 @@ func (endpoint *RouterEndpoint) HandleRequest(
// defer cancel()
//}

if ok := endpoint.redirectFn(w, r); ok {
return
}

urlValues := ParamsFromContext(r.Context())
req := NewServerHTTPRequest(w, r, urlValues, endpoint)
ctx := req.Context()
Expand Down

0 comments on commit 47a67f1

Please sign in to comment.