-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
52 lines (44 loc) · 992 Bytes
/
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
package main
import (
"context"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
type Server struct {
e *echo.Echo
l net.Listener
logger *zap.Logger
}
func NewServer(e *echo.Echo, l net.Listener, logger *zap.Logger) *Server {
return &Server{
e: e,
l: l,
logger: logger,
}
}
func (s *Server) Run(ctx context.Context) error {
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
eg, ctx := errgroup.WithContext(ctx)
s.e.Listener = s.l
// logger.SetRequestLoggerToEcho(s.e, s.logger)
s.logger.Info("Server is starting...")
eg.Go(func() error {
if err := s.e.Start(""); err != http.ErrServerClosed {
return err
}
return nil
})
<-ctx.Done()
s.logger.Info("Server is shutting down...")
if err := s.e.Shutdown(context.Background()); err != nil {
s.logger.Error("Server shutdown error", zap.Error(err))
}
return eg.Wait()
}