Skip to content

Commit

Permalink
Fixed comments and nits
Browse files Browse the repository at this point in the history
  • Loading branch information
anuptalwalkar committed Jan 18, 2017
1 parent 7388b46 commit 6b1c13a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
16 changes: 6 additions & 10 deletions examples/simple/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package main

import (
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -49,13 +48,10 @@ func registerHTTPers(service service.Host) []uhttp.RouteHandler {
}
}

func simpleFilter() uhttp.FilterFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, next uhttp.Handler) {
fxctx, ok := ctx.(fx.Context)
if ok {
next.ServeHTTP(fxctx, w, r)
} else {
uhttp.Wrap(service.NopHost(), next).ServeHTTP(w, r)
}
}
type simpleFilter struct {
}

func (simpleFilter) Apply(ctx fx.Context, w http.ResponseWriter, r *http.Request, next uhttp.Handler) {
io.WriteString(w, "Going through simpleFilter")
next.ServeHTTP(ctx, w, r)
}
2 changes: 1 addition & 1 deletion examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func main() {
svc, err := service.WithModules(
uhttp.New(registerHTTPers, []uhttp.Filter{simpleFilter()}),
uhttp.New(registerHTTPers, []uhttp.Filter{simpleFilter{}}),
).Build()

if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions modules/uhttp/filterchain_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (fc filterChain) ServeHTTP(ctx fx.Context, w http.ResponseWriter, r *http.R
type FilterChainBuilder interface {
// AddFilter is used to add the next filter to the chain during construction time.
// The calls to AddFilter can be chained.
AddFilter(filter Filter) FilterChainBuilder
AddFilter(filter ...Filter) FilterChainBuilder

// Build creates an immutable FilterChain.
Build(finalHandler Handler) filterChain
Expand All @@ -71,8 +71,7 @@ func defaultFilterChainBuilder(host service.Host) FilterChainBuilder {
fcb := NewFilterChainBuilder(host)
return fcb.AddFilter(contextFilter(host)).
AddFilter(tracingServerFilter(host)).
AddFilter(authorizationFilter(host)).
AddFilter(panicFilter(host))
AddFilter(authorizationFilter(host))
}

// NewFilterChainBuilder creates an empty filterChainBuilder for setup
Expand All @@ -82,16 +81,17 @@ func NewFilterChainBuilder(host service.Host) FilterChainBuilder {
}
}

func (f filterChainBuilder) AddFilter(filter Filter) FilterChainBuilder {
f.filters = append(f.filters, filter)
func (f filterChainBuilder) AddFilter(filters ...Filter) FilterChainBuilder {
for _, filter := range filters {
f.filters = append(f.filters, filter)
}
f.filters = append(f.filters, panicFilter(f.Host))
return f
}

func (f filterChainBuilder) Build(finalHandler Handler) filterChain {
fc := filterChain{}
for _, filter := range f.filters {
fc.filters = append(fc.filters, filter)
return filterChain{
filters: f.filters,
finalHandler: finalHandler,
}
fc.finalHandler = finalHandler
return fc
}
1 change: 1 addition & 0 deletions modules/uhttp/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func authorizationFilter(host service.Host) FilterFunc {
}

// panicFilter handles any panics and return an error
// panic filter should be added at the end of filter chain to catch panics
func panicFilter(host service.Host) FilterFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, next Handler) {
fxctx := &fxcontext.Context{
Expand Down
15 changes: 15 additions & 0 deletions modules/uhttp/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func TestHTTPModule_WithFilter(t *testing.T) {
})
}

func TestHTTPModule_WithUserPanicFilter(t *testing.T) {
withModule(t, registerPanic, []Filter{userPanicFilter()}, nil, false, func(m *Module) {
assert.NotNil(t, m)
makeRequest(m, "GET", "/", nil, func(r *http.Response) {
assert.Equal(t, http.StatusInternalServerError, r.StatusCode, "Expected 500 with panic wrapper")
})
})
}

func TestHTTPModule_Panic_OK(t *testing.T) {
withModule(t, registerPanic, nil, nil, false, func(m *Module) {
assert.NotNil(t, m)
Expand Down Expand Up @@ -272,3 +281,9 @@ func fakeFilter() FilterFunc {
Wrap(service.NopHost(), next).ServeHTTP(w, r)
}
}

func userPanicFilter() FilterFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, next Handler) {
panic("Intentional panic for:" + r.URL.Path)
}
}

0 comments on commit 6b1c13a

Please sign in to comment.