forked from lmas/ss13.se
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler_helpers.go
47 lines (38 loc) · 866 Bytes
/
handler_helpers.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
package ss13_se
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type HttpError struct {
Status int
Err error
}
func (s HttpError) Error() string {
return fmt.Sprintf("%d %s", s.Status, s.Err.Error())
}
type handlerVars map[string]string
type handler func(http.ResponseWriter, *http.Request, handlerVars) error
func (h handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
//start := time.Now()
err := h(rw, req, mux.Vars(req))
//dur := time.Since(start)
if err != nil {
switch e := err.(type) {
case HttpError:
http.Error(rw, e.Error(), e.Status)
default:
http.Error(rw, http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
}
}
//log.Printf("%s %s \t%s \terr: %v\n",
//req.RemoteAddr,
//req.Method,
//req.URL.String(),
//req.UserAgent(),
//dur,
//resp.Status,
//err,
//)
}