-
Notifications
You must be signed in to change notification settings - Fork 3
/
transport.go
86 lines (70 loc) · 1.62 KB
/
transport.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package transport
import (
"context"
"fmt"
"net"
"github.com/alexfalkowski/go-service/transport/grpc"
"github.com/alexfalkowski/go-service/transport/http"
"github.com/soheilhy/cmux"
"go.uber.org/fx"
)
// RegisterParams for transport.
type RegisterParams struct {
fx.In
Lifecycle fx.Lifecycle
Shutdowner fx.Shutdowner
Config *Config
HTTP *http.Server
GRPC *grpc.Server
}
// Register all the transports.
func Register(params RegisterParams) {
server := NewServer(params)
params.Lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
return server.Start()
},
OnStop: func(ctx context.Context) error {
server.Stop(ctx)
return nil
},
})
}
// NewServer for transport.
func NewServer(params RegisterParams) *Server {
return &Server{sh: params.Shutdowner, cfg: params.Config, http: params.HTTP, grpc: params.GRPC}
}
// Server handles all the transports.
type Server struct {
mux cmux.CMux
sh fx.Shutdowner
cfg *Config
http *http.Server
grpc *grpc.Server
}
// Start all the servers.
func (s *Server) Start() error {
l, err := net.Listen("tcp", fmt.Sprintf(":%s", s.cfg.Port))
if err != nil {
return err
}
s.mux = cmux.New(l)
gl := s.mux.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
hl := s.mux.Match(cmux.HTTP1())
go s.grpc.Start(gl)
go s.http.Start(hl)
go s.start()
return nil
}
// Stop all the servers.
func (s *Server) Stop(ctx context.Context) {
s.grpc.Stop(ctx)
s.http.Stop(ctx)
s.mux.Close()
}
func (s *Server) start() error {
if err := s.mux.Serve(); err != nil {
return s.sh.Shutdown()
}
return nil
}