forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.go
More file actions
38 lines (30 loc) · 838 Bytes
/
Copy pathhttp_server.go
File metadata and controls
38 lines (30 loc) · 838 Bytes
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
package http_api
import (
"fmt"
"log"
"net"
"net/http"
"strings"
"github.com/nsqio/nsq/internal/lg"
)
type logWriter struct {
logf lg.AppLogFunc
}
func (l logWriter) Write(p []byte) (int, error) {
l.logf(lg.WARN, "%s", string(p))
return len(p), nil
}
func Serve(listener net.Listener, handler http.Handler, proto string, logf lg.AppLogFunc) error {
logf(lg.INFO, "%s: listening on %s", proto, listener.Addr())
server := &http.Server{
Handler: handler,
ErrorLog: log.New(logWriter{logf}, "", 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") {
return fmt.Errorf("http.Serve() error - %s", err)
}
logf(lg.INFO, "%s: closing %s", proto, listener.Addr())
return nil
}