forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 12
/
http_server.go
36 lines (29 loc) · 830 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
35
36
package http_api
import (
"fmt"
"log"
"net"
"net/http"
"strings"
"github.com/nsqio/nsq/internal/app"
)
type logWriter struct {
app.Logger
}
func (l logWriter) Write(p []byte) (int, error) {
l.Logger.Output(2, string(p))
return len(p), nil
}
func Serve(listener net.Listener, handler http.Handler, proto string, l app.Logger) {
l.Output(2, fmt.Sprintf("%s: listening on %s", proto, listener.Addr()))
server := &http.Server{
Handler: handler,
ErrorLog: log.New(logWriter{l}, "", 0),
}
err := server.Serve(listener)
// theres no direct way to detect this error because it is not exposed
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
l.Output(2, fmt.Sprintf("ERROR: http.Serve() - %s", err))
}
l.Output(2, fmt.Sprintf("%s: closing %s", proto, listener.Addr()))
}