/
server.go
171 lines (154 loc) · 3.91 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package bhttp
import (
"context"
"fmt"
"io"
"net"
"net/http"
"path"
"strings"
"github.com/icexin/brpc-go"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type service struct {
desc *grpc.ServiceDesc
methods map[string]*grpc.MethodDesc
srv interface{}
}
type server struct {
services map[string]*service
httpServer *http.Server
opts *brpc.ServerOptions
}
func newServer(options ...brpc.ServerOption) *server {
var opts brpc.ServerOptions
for _, opt := range options {
if o, ok := opt.(brpc.BServerOption); ok {
o(&opts)
}
}
return &server{
services: make(map[string]*service),
httpServer: &http.Server{},
opts: &opts,
}
}
// Serve accepts incoming connections on the listener l, creating a new ServerConn and service goroutine for each. The service goroutines read pbrpc requests and then call the registered handlers to reply to them. Serve returns when l.Accept fails with errors.
// TODO Handle non fatal errors
func (s *server) Serve(l net.Listener) error {
return s.httpServer.Serve(l)
}
func (s *server) Shutdown() {
s.httpServer.Shutdown(context.Background())
}
func (s *server) handleService(srv *service, w http.ResponseWriter, r *http.Request) {
methodName := path.Base(r.URL.Path)
method, ok := srv.methods[methodName]
if !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "method %s not found", methodName)
return
}
buf, err := s.serveRequest(srv, method, r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
w.Write(buf)
}
func (s *server) serveRequest(srv *service, method *grpc.MethodDesc, r *http.Request) ([]byte, error) {
var (
enc func(interface{}) ([]byte, error)
dec func([]byte, interface{}) error
)
switch r.Header.Get("Content-Type") {
case "application/json", "":
enc = jsonEncode
dec = jsonDecode
case "application/proto", "application/protobuf":
enc = protoEncode
dec = protoDecode
default:
enc = jsonEncode
dec = jsonDecode
}
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("read body error:%w", err)
}
decFunc := func(v interface{}) error {
err := dec(body, v)
if err != nil {
return err
}
return nil
}
resp, err := method.Handler(srv.srv, r.Context(), decFunc, s.opts.Interceptor)
if err != nil {
return nil, err
}
buf, err := enc(resp)
if err != nil {
return nil, fmt.Errorf("encode response error:%w", err)
}
return buf, nil
}
func (s *server) RegisterService(sd *grpc.ServiceDesc, srv interface{}) {
methods := make(map[string]*grpc.MethodDesc)
for i := range sd.Methods {
m := &sd.Methods[i]
methods[m.MethodName] = m
}
service := &service{
desc: sd,
methods: methods,
srv: srv,
}
fs := strings.Split(sd.ServiceName, ".")
shortName := fs[len(fs)-1]
s.services[sd.ServiceName] = service
s.services[shortName] = service
handler := func(w http.ResponseWriter, r *http.Request) {
s.handleService(service, w, r)
}
http.HandleFunc("/"+shortName+"/", handler)
http.HandleFunc("/"+sd.ServiceName+"/", handler)
}
func jsonEncode(v interface{}) ([]byte, error) {
msg, ok := v.(proto.Message)
if !ok {
return nil, fmt.Errorf("invalid type:%T", v)
}
opts := protojson.MarshalOptions{
UseProtoNames: true,
}
return opts.Marshal(msg)
}
func jsonDecode(buf []byte, v interface{}) error {
msg, ok := v.(proto.Message)
if !ok {
return fmt.Errorf("invalid type:%T", v)
}
if len(buf) == 0 {
return nil
}
return protojson.Unmarshal(buf, msg)
}
func protoEncode(v interface{}) ([]byte, error) {
msg, ok := v.(proto.Message)
if !ok {
return nil, fmt.Errorf("invalid type:%T", v)
}
return proto.Marshal(msg)
}
func protoDecode(buf []byte, v interface{}) error {
msg, ok := v.(proto.Message)
if !ok {
return fmt.Errorf("invalid type:%T", v)
}
return proto.Unmarshal(buf, msg)
}