Skip to content

Commit

Permalink
Added comments and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
apourchet committed Jun 24, 2019
1 parent 5875f73 commit eade9bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# httpwrap
----------

`httpwrap` is a thin wrapper around the default http library that lets you compose handlers
and automatically inject outputs into the inputs of the next handler.
1 change: 0 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/gorilla/mux"
)

// To test.
type hand struct {
i int
}
Expand Down
15 changes: 14 additions & 1 deletion wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,32 @@ import (
)

// Wrapper implements the http.Handler interface, wrapping the handlers
// that are passed in
// that are passed in.
type Wrapper struct {
befores []beforeFn
after *afterFn
construct Constructor
}

// Constructor is the function signature for unmarshalling an http request into
// an object.
type Constructor func(http.ResponseWriter, *http.Request, interface{}) error

// New creates a new Wrapper object.
func New() Wrapper {
return Wrapper{
construct: func(http.ResponseWriter, *http.Request, interface{}) error { return nil },
}
}

// WithConstruct returns a new wrapper with the given Constructor function.
func (w Wrapper) WithConstruct(cons Constructor) Wrapper {
w.construct = cons
return w
}

// Before adds a new function that will execute before the main handler. The chain
// of befores will end if a before returns a non-nil error value.
func (w Wrapper) Before(fns ...interface{}) Wrapper {
befores := make([]beforeFn, len(w.befores)+len(fns))
copy(befores, w.befores)
Expand All @@ -40,6 +46,7 @@ func (w Wrapper) Before(fns ...interface{}) Wrapper {
return w
}

// Finally sets the last function that will execute during a request.
func (w Wrapper) Finally(fn interface{}) Wrapper {
after, err := newAfter(fn)
if err != nil {
Expand All @@ -49,6 +56,8 @@ func (w Wrapper) Finally(fn interface{}) Wrapper {
return w
}

// Wrap sets the main handling function to process requests. This Wrap function must
// be called to get an `http.Handler` type.
func (w Wrapper) Wrap(fn interface{}) Handler {
main, err := newMain(fn)
if err != nil {
Expand All @@ -60,21 +69,25 @@ func (w Wrapper) Wrap(fn interface{}) Handler {
}
}

// Handler is a Wrapper that implements `http.Handler`.
type Handler struct {
Wrapper
main mainFn
}

// Before adds the before functions to the underlying Wrapper.
func (h Handler) Before(fns ...interface{}) Handler {
h.Wrapper = h.Wrapper.Before(fns...)
return h
}

// Finally sets the `finally` function of the underlying Wrapper.
func (h Handler) Finally(fn interface{}) Handler {
h.Wrapper = h.Wrapper.Finally(fn)
return h
}

// ServeHTTP implements `http.Handler`.
func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ctx := newRunCtx(rw, req, h.construct)
err := h.serveBefores(ctx)
Expand Down

0 comments on commit eade9bd

Please sign in to comment.