-
Notifications
You must be signed in to change notification settings - Fork 47
/
options.go
99 lines (83 loc) · 2.61 KB
/
options.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
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package drpchttp
import (
"net/http"
"storj.io/drpc"
)
// Option configures some aspect of the handler.
type Option struct{ apply func(*options) }
type options struct {
protocols map[string]Protocol
}
// Protocol is used by the handler to create drpc.Streams from incoming
// requests and format responses.
type Protocol interface {
// NewStream takes an incoming request and response writer and returns
// a drpc.Stream that should be used for the RPC.
NewStream(rw http.ResponseWriter, req *http.Request) Stream
}
// Stream wraps a drpc.Stream type with a Finish method that knows how to
// send and format the error/response to an http request.
type Stream interface {
drpc.Stream
// Finish is passed the possibly-nil error that was generated handling
// the RPC and is expected to write any error reporting or otherwise
// finalize the request.
Finish(err error)
}
// WithProtocol associates the given Protocol with some content type. The
// match is exact, with the special case that the content type "*" is the
// fallback Protocol used when nothing matches.
func WithProtocol(contentType string, pr Protocol) Option {
return Option{apply: func(opts *options) {
opts.protocols[contentType] = pr
}}
}
func defaultProtocols() map[string]Protocol {
return map[string]Protocol{
"*": twirpProtocol{
ct: "application/proto",
marshal: protoMarshal,
unmarshal: protoUnmarshal,
},
"application/proto": twirpProtocol{
ct: "application/proto",
marshal: protoMarshal,
unmarshal: protoUnmarshal,
},
"application/json": twirpProtocol{
ct: "application/json",
marshal: JSONMarshal,
unmarshal: JSONUnmarshal,
},
"application/grpc-web+proto": grpcWebProtocol{
ct: "application/grpc-web+proto",
read: grpcRead,
write: normalWrite,
marshal: protoMarshal,
unmarshal: protoUnmarshal,
},
"application/grpc-web+json": grpcWebProtocol{
ct: "application/grpc-web+json",
read: grpcRead,
write: normalWrite,
marshal: JSONMarshal,
unmarshal: JSONUnmarshal,
},
"application/grpc-web-text+proto": grpcWebProtocol{
ct: "application/grpc-web-text+proto",
read: base64Read(grpcRead),
write: base64Write(normalWrite),
marshal: protoMarshal,
unmarshal: protoUnmarshal,
},
"application/grpc-web-text+json": grpcWebProtocol{
ct: "application/grpc-web-text+json",
read: base64Read(grpcRead),
write: base64Write(normalWrite),
marshal: JSONMarshal,
unmarshal: JSONUnmarshal,
},
}
}