forked from tsuru/gandalf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
51 lines (42 loc) · 1.18 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2015 gandalf authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package api
import (
"log"
"net/http"
"os"
"time"
"github.com/codegangsta/negroni"
)
type loggerMiddleware struct {
logger *log.Logger
}
func NewLoggerMiddleware() *loggerMiddleware {
return &loggerMiddleware{
logger: log.New(os.Stdout, "", 0),
}
}
func (l *loggerMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
next(rw, r)
duration := time.Since(start)
res := rw.(negroni.ResponseWriter)
nowFormatted := time.Now().Format(time.RFC3339Nano)
l.logger.Printf("%s %s %s %d in %0.6fms",
nowFormatted, r.Method,
r.URL.Path, res.Status(),
float64(duration)/float64(time.Millisecond),
)
}
type responseHeaderMiddleware struct {
name string
value string
}
func NewResponseHeaderMiddleware(name string, value string) *responseHeaderMiddleware {
return &responseHeaderMiddleware{name: name, value: value}
}
func (m *responseHeaderMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
rw.Header().Set(m.name, m.value)
next(rw, r)
}