-
Notifications
You must be signed in to change notification settings - Fork 1
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 #27 from eduboard/leo/http-logging
http: logging middleware and Chain middlwares
- Loading branch information
Showing
4 changed files
with
89 additions
and
19 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
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,22 @@ | ||
package mock | ||
|
||
import "net/http" | ||
|
||
type Check struct { | ||
Passed bool | ||
} | ||
|
||
func GenerateCheckedMiddlewares(checks ...*Check) []func(http.Handler) http.Handler { | ||
var handlers = make([]func(http.Handler) http.Handler, len(checks)) | ||
for k, c := range checks { | ||
func(c *Check) { // Wrapping the function call like this creates a closure, making sure `c` does not change before evaluation. | ||
handlers[k] = func(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
c.Passed = true | ||
handler.ServeHTTP(w, r) | ||
}) | ||
} | ||
}(c) | ||
} | ||
return handlers | ||
} |