This repository has been archived by the owner on May 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
71 lines (61 loc) · 1.57 KB
/
grpc.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
package server
import (
"context"
"net/http"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
"github.com/go-sdk/lib/consts"
)
var (
serveMarshaler = &runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseEnumNumbers: true,
EmitUnpopulated: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
},
}
serveMetadata = func(ctx context.Context, req *http.Request) metadata.MD {
q := req.URL.Query()
md := metadata.MD{}
md.Set(consts.Query, q.Encode())
for k, vs := range q {
md.Append(consts.Query+"-"+k, vs...)
}
return md
}
serveIncomingHeaderMatcher = func(key string) (string, bool) {
switch k := strings.ToLower(key); k {
case "user-agent":
return "x-" + k, true
default:
if strings.HasPrefix(k, "x-") {
return k, true
}
return runtime.DefaultHeaderMatcher(key)
}
}
serveOutgoingHeaderMatcher = func(key string) (string, bool) {
if strings.HasPrefix(key, "x-") {
return key, true
}
return "", false
}
)
type ServiceHandler func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error
func (s *Server) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
s.gsd[desc] = impl
}
func (s *Server) RegisterServiceHandler(h ServiceHandler, opts ...grpc.DialOption) {
if len(opts) == 0 {
opts = gDialOptions
}
s.gsh = append(s.gsh, h)
s.gso = append(s.gso, opts)
}