Externalize adapters

This commit is contained in:
Lena Fuhrimann 2017-04-03 14:12:36 +02:00
parent 4672fc2e1c
commit e96567846b
4 changed files with 10 additions and 43 deletions

View file

@ -1,15 +0,0 @@
package adapters
import "net/http"
// Adapter is an HTTP middleware
type Adapter func(http.Handler) http.Handler
// Adapt applies adapters to an HTTP handler function
func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
for i := len(adapters) - 1; i >= 0; i-- {
h = adapters[i](h)
}
return h
}

View file

@ -1,25 +0,0 @@
package adapters
import (
"log"
"net/http"
"time"
)
// Logging logs HTTP requests
func Logging(logger *log.Logger) Adapter {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func(start time.Time) {
logger.Printf(
"%s\t%s\t%s",
r.Method,
r.RequestURI,
time.Since(start),
)
}(time.Now())
next.ServeHTTP(w, r)
})
}
}

10
glide.lock generated
View file

@ -1,10 +1,14 @@
hash: 5af937afdf2e2b202f0394765f6fa07af14098dd77047df4705611751b70fc16
updated: 2017-04-02T17:09:45.925326395+02:00
hash: 9b88c8ce183463a407bdb87f79b6b857d8343a7bb5ce4723223c539c654d6d6d
updated: 2017-04-03T14:10:47.88086765+02:00
imports:
- name: github.com/gorilla/context
version: 08b5f424b9271eedf6f9f0ce86cb9396ed337a42
version: 1ea25387ff6f684839d82767c1733ff4d4d15d0a
- name: github.com/gorilla/mux
version: 392c28fe23e1c45ddba891b0320b3b5df220beea
- name: github.com/mastertinner/adapters
version: 4066d5a6918fa50f51471aea41771c61deca0680
subpackages:
- logging
- name: github.com/minio/minio-go
version: dcaae9ec4d0b0a81d17f22f6d7a186491f6a55ec
subpackages:

View file

@ -8,3 +8,6 @@ import:
version: ^1.1.4
subpackages:
- assert
- package: github.com/mastertinner/adapters
subpackages:
- logging