-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
63 lines (49 loc) · 1.62 KB
/
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
63
package http
import (
"context"
"fmt"
"net"
"net/http"
"github.com/alexfalkowski/go-service/pkg/config"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"go.uber.org/fx"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func Register(lc fx.Lifecycle, s fx.Shutdowner, mux *runtime.ServeMux, cfg *config.Config, logger *zap.Logger) {
server := &http.Server{
Addr: fmt.Sprintf(":%s", cfg.HTTPPort),
Handler: mux,
}
lc.Append(fx.Hook{
OnStart: func(context.Context) error {
listener, err := net.Listen("tcp", fmt.Sprintf(":%s", cfg.HTTPPort))
if err != nil {
return err
}
go startServer(s, server, listener, cfg, logger)
return nil
},
OnStop: func(ctx context.Context) error {
return stopServer(ctx, server, cfg, logger)
},
})
}
func startServer(s fx.Shutdowner, server *http.Server, listener net.Listener, cfg *config.Config, logger *zap.Logger) {
logger.Info("starting http server", zap.String("port", cfg.HTTPPort))
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
fields := []zapcore.Field{zap.String("port", cfg.HTTPPort), zap.Error(err)}
if err := s.Shutdown(); err != nil {
fields = append(fields, zap.NamedError("shutdown_error", err))
}
logger.Error("could not start http server", fields...)
}
}
func stopServer(ctx context.Context, server *http.Server, cfg *config.Config, logger *zap.Logger) error {
logger.Info("stopping http server", zap.String("port", cfg.HTTPPort))
if err := server.Shutdown(ctx); err != nil {
logger.Error("could not stop http server", zap.String("port", cfg.HTTPPort), zap.Error(err))
return err
}
return nil
}