/
http_server.go
62 lines (50 loc) · 1.16 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package http_server
import (
"context"
"log"
"net"
"net/http"
"time"
"github.com/bigflood/leaderboard/api"
"github.com/bigflood/leaderboard/pkg/http_handler"
"github.com/bigflood/leaderboard/pkg/logging_mw"
"github.com/labstack/echo/v4"
)
type Server struct {
httpServer *http.Server
e *echo.Echo
}
func New(lb api.LeaderBoard, logger *log.Logger) *Server {
if logger != nil {
lb = &logging_mw.LoggingMiddleware{
Receiver: lb,
Logger: logger,
}
}
handler := http_handler.New(lb)
e := echo.New()
e.HideBanner = true
e.HidePort = true
handler.Setup(e)
httpServer := &http.Server{
ReadHeaderTimeout: 30 * time.Second,
ReadTimeout: 60 * time.Second,
WriteTimeout: 120 * time.Second,
IdleTimeout: 330 * time.Second,
}
return &Server{
httpServer: httpServer,
e: e,
}
}
func (s *Server) ListenAndServe(addr string) error {
s.httpServer.Addr = addr
return s.e.StartServer(s.httpServer)
}
func (s *Server) Serve(listener net.Listener) error {
s.e.Listener = listener
return s.e.StartServer(s.httpServer)
}
func (s *Server) Shutdown(ctx context.Context) error {
return s.httpServer.Shutdown(ctx)
}