This repository has been archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* middleware: Split everything into files by responsibility Add Router.chain() for middleware chaining Add middleware Remove json files Update README with features
- Loading branch information
Showing
8 changed files
with
155 additions
and
78 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package goat | ||
|
||
import ( | ||
"net/http" | ||
"sort" | ||
) | ||
|
||
// IndexHandler writes the index of all GET methods to the ResponseWriter | ||
func (r *Router) IndexHandler(w http.ResponseWriter, _ *http.Request, _ Params) { | ||
WriteJSON(w, r.Index()) | ||
} | ||
|
||
// Index returns a string map with the titles and urls of all GET routes | ||
func (r *Router) Index() map[string]string { | ||
index := r.index | ||
|
||
// Recursion | ||
for _, sr := range r.children { | ||
si := sr.Index() | ||
|
||
for k, v := range si { | ||
index[k] = v | ||
} | ||
} | ||
|
||
// Sort | ||
sorted := make(map[string]string) | ||
var keys []string | ||
for k := range index { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
for _, k := range keys { | ||
sorted[k] = index[k] | ||
} | ||
|
||
return sorted | ||
} |
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,23 @@ | ||
package goat | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestIndex(t *testing.T) { | ||
r := New() | ||
|
||
r.Get("/foo/bar", "foo_bar_url", emptyHandler) | ||
r.Get("/bar", "bar_url", emptyHandler) | ||
r.Post("/foo", "foo_url", emptyHandler) | ||
|
||
out := r.Index() | ||
expected := map[string]string{ | ||
"bar_url": "/bar", | ||
"foo_bar_url": "/foo/bar", | ||
} | ||
if !reflect.DeepEqual(out, expected) { | ||
t.Errorf("Index should regurn %v, but did return %v", expected, out) | ||
} | ||
} |
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,26 @@ | ||
package goat | ||
|
||
import "net/http" | ||
|
||
// Middleware reprents a default middleware function | ||
type Middleware func(http.Handler) http.Handler | ||
|
||
// chain calls all middlewares and returns the final handler | ||
func (r *Router) chain() http.Handler { | ||
var final http.Handler | ||
final = r.router | ||
|
||
for i := len(r.middleware) - 1; i >= 0; i-- { | ||
final = r.middleware[i](final) | ||
} | ||
|
||
return final | ||
} | ||
|
||
// Use adds middleware to the router | ||
func (r *Router) Use(middleware ...Middleware) { | ||
if r.parent != nil { | ||
panic("subrouters can't use middleware!") | ||
} | ||
r.middleware = append(r.middleware, middleware...) | ||
} |
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,16 @@ | ||
package goat | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestUse(t *testing.T) { | ||
r := New() | ||
mw := func(h http.Handler) http.Handler { return nil } | ||
//exp := []Middleware{mw} | ||
|
||
r.Use(mw) | ||
|
||
// TODO: Test function equality | ||
} |
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