-
Notifications
You must be signed in to change notification settings - Fork 10
/
http.go
59 lines (49 loc) · 1.34 KB
/
http.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
52
53
54
55
56
57
58
59
package svc
import (
"context"
"log"
"net"
"net/http"
"time"
"go.uber.org/zap"
)
var _ Worker = (*httpServer)(nil)
// httpServer defines the internal HTTP Server worker.
type httpServer struct {
logger *zap.Logger
addr string
httpServer *http.Server
}
func newHTTPServer(port string, handler http.Handler, logger *log.Logger) *httpServer {
addr := net.JoinHostPort("", port)
return &httpServer{
addr: addr,
httpServer: &http.Server{
Addr: addr,
Handler: handler,
ErrorLog: logger,
ReadHeaderTimeout: 5 * time.Second, // https://medium.com/a-journey-with-go/go-understand-and-mitigate-slowloris-attack-711c1b1403f6
},
}
}
// Init implements the Worker interface.
func (s *httpServer) Init(logger *zap.Logger) error {
s.logger = logger
return nil
}
// Healthy implements the Healther interface.
func (s *httpServer) Healthy() error {
return nil
}
// Run implements the Worker interface.
func (s *httpServer) Run() error {
s.logger.Info("Listening and serving HTTP", zap.String("address", s.addr))
if err := s.httpServer.ListenAndServe(); err != http.ErrServerClosed {
s.logger.Error("Failed to serve HTTP", zap.Error(err))
}
return nil
}
// Terminate implements the Worker interface.
func (s *httpServer) Terminate() error {
return s.httpServer.Shutdown(context.Background())
}