File tree Expand file tree Collapse file tree 1 file changed +46
-6
lines changed Expand file tree Collapse file tree 1 file changed +46
-6
lines changed Original file line number Diff line number Diff line change 11package servers
22
33import (
4+ "context"
45 "net"
6+ "net/http"
57
68 "google.golang.org/grpc"
79)
810
9- type (
10- grpcTransportMananger struct {
11- addr string
12- server * grpc.Server
13- }
14- )
11+ type grpcTransportMananger struct {
12+ addr string
13+ server * grpc.Server
14+ }
1515
1616func (t * grpcTransportMananger ) Start () <- chan error {
1717 errC := make (chan error )
@@ -48,3 +48,43 @@ func WithGRPCServer(addr string, server *grpc.Server) Option {
4848 return nil
4949 }
5050}
51+
52+ type httpTransportMananger struct {
53+ addr string
54+ server * http.Server
55+ }
56+
57+ func (t * httpTransportMananger ) Start () <- chan error {
58+ errC := make (chan error )
59+
60+ go func () {
61+ nl , err := net .Listen ("tcp" , t .addr )
62+ if err != nil {
63+ errC <- err
64+ return
65+ }
66+
67+ errC <- t .server .Serve (nl )
68+ }()
69+
70+ return (<- chan error )(errC )
71+ }
72+
73+ func (t * httpTransportMananger ) Stop () error {
74+ return t .server .Shutdown (context .Background ())
75+ }
76+
77+ // WithHTTPServer will run a http server when the Server is Run
78+ func WithHTTPServer (addr string , server * http.Server ) Option {
79+ if server == nil {
80+ panic ("must provide both a net listener and HTTP server to WithHTTPServer" )
81+ }
82+
83+ return func (o * serverOptions ) error {
84+ o .transports = append (o .transports , & httpTransportMananger {
85+ addr : addr ,
86+ server : server ,
87+ })
88+ return nil
89+ }
90+ }
You can’t perform that action at this time.
0 commit comments