-
Notifications
You must be signed in to change notification settings - Fork 8
/
http_server.go
34 lines (31 loc) · 947 Bytes
/
http_server.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
package httputilmore
import (
"log"
"net/http"
"time"
)
// Log is a custom Http handler that will log all requests.
// It can be called using
// http.ListenAndServe(":8080", Log(http.DefaultServeMux))
// From: https://groups.google.com/forum/#!topic/golang-nuts/s7Xk1q0LSU0
func Log(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
// NewServerTimeouts returns a `*http.Server` with all timeouts set to a single value provided.
func NewServerTimeouts(addr string, handler http.Handler, timeout time.Duration) *http.Server {
if timeout < 0 {
timeout = 0
}
return &http.Server{
Addr: addr,
Handler: handler,
IdleTimeout: timeout,
ReadHeaderTimeout: timeout,
ReadTimeout: timeout,
WriteTimeout: timeout,
MaxHeaderBytes: 1 << 20,
}
}