-
Notifications
You must be signed in to change notification settings - Fork 110
/
server.go
111 lines (94 loc) · 2.78 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package module
import (
"context"
"crypto/tls"
"net"
"net/http"
"sync"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/pkg/errors"
"go.viam.com/utils/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// NewServer returns a new (module specific) rpc.Server.
func NewServer(unary []grpc.UnaryServerInterceptor, stream []grpc.StreamServerInterceptor) rpc.Server {
s := &Server{server: grpc.NewServer(
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unary...)),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(stream...)),
)}
reflection.Register(s.server)
return s
}
// Server provides an rpc.Server wrapper around a grpc.Server.
type Server struct {
mu sync.RWMutex
server *grpc.Server
addr net.Addr
}
// InternalAddr returns the internal address of the server.
func (s *Server) InternalAddr() net.Addr {
s.mu.RLock()
defer s.mu.RUnlock()
return s.addr
}
// InstanceNames is unsupported.
func (s *Server) InstanceNames() []string {
return []string{}
}
// EnsureAuthed is unsupported.
func (s *Server) EnsureAuthed(ctx context.Context) (context.Context, error) {
return nil, errors.New("EnsureAuthed is unsupported")
}
// Start is unsupported.
func (s *Server) Start() error {
return errors.New("start unsupported on module grpc server")
}
// Serve begins listening/serving grpc.
func (s *Server) Serve(listener net.Listener) error {
s.mu.Lock()
s.addr = listener.Addr()
s.mu.Unlock()
return s.server.Serve(listener)
}
// ServeTLS is unsupported.
func (s *Server) ServeTLS(listener net.Listener, certFile, keyFile string, tlsConfig *tls.Config) error {
return errors.New("tls unsupported on module grpc server")
}
// Stop performs a GracefulStop() on the underlying grpc server.
func (s *Server) Stop() error {
s.mu.Lock()
defer s.mu.Unlock()
s.addr = &net.UnixAddr{}
s.server.GracefulStop()
return nil
}
// RegisterServiceServer associates a service description with
// its implementation along with any gateway handlers.
func (s *Server) RegisterServiceServer(
ctx context.Context,
svcDesc *grpc.ServiceDesc,
svcServer interface{},
svcHandlers ...rpc.RegisterServiceHandlerFromEndpointFunc,
) error {
s.server.RegisterService(svcDesc, svcServer)
return nil
}
// GatewayHandler is unsupported.
func (s *Server) GatewayHandler() http.Handler {
return &httpHandler{}
}
// GRPCHandler is unsupported.
func (s *Server) GRPCHandler() http.Handler {
return &httpHandler{}
}
// ServeHTTP is unsupported.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h := &httpHandler{}
h.ServeHTTP(w, r)
}
type httpHandler struct{}
// ServeHTTP returns only an error message.
func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "http unsupported", http.StatusInternalServerError)
}