-
Notifications
You must be signed in to change notification settings - Fork 1
/
apis.go
136 lines (116 loc) · 3.57 KB
/
apis.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
package doris
import (
"net/http"
"connectrpc.com/connect"
"github.com/altipla-consulting/errors"
"github.com/rs/cors"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"libs.altipla.consulting/routing"
)
// ConnectHub helps mounting Connect APIs to their correct endpoints.
type ConnectHub struct {
r *Router
cors []string
interceptors []connect.Interceptor
}
// NewConnectHub creates a new hub prepared to mount Connect APIs.
func NewConnectHub(r *Router, opts ...ConnectHubOption) *ConnectHub {
hub := &ConnectHub{
r: r,
cors: []string{"https://studio.buf.build"},
}
for _, opt := range opts {
opt(hub)
}
return hub
}
// MountFn should be implemented by a global function in the API package to
// register itself.
type MountFn func(opts ...connect.HandlerOption) (string, http.Handler)
// Mount a new API.
func (hub *ConnectHub) Mount(fn MountFn) {
pattern, handler := fn(hub.opts()...)
if len(hub.cors) > 0 {
cnf := cors.Options{
AllowedOrigins: hub.cors,
AllowedMethods: []string{http.MethodPost, http.MethodOptions},
AllowedHeaders: []string{
// Generic headers of any request.
"authorization",
"content-type",
// Connect specific headers.
"connect-timeout-ms",
"connect-protocol-version",
// Added by iPhone Safari.
"user-agent",
},
MaxAge: 300,
}
handler = cors.New(cnf).Handler(handler)
}
hub.r.PathPrefixHandlerHTTP(pattern, handler)
}
func (hub *ConnectHub) opts() []connect.HandlerOption {
return []connect.HandlerOption{
connect.WithInterceptors(ServerInterceptors()...),
connect.WithInterceptors(hub.interceptors...),
connect.WithCodec(new(codecJSON)),
}
}
// ConnectHubOption configures the Connect hub.
type ConnectHubOption func(cnf *ConnectHub)
// WithCORS configures the domains authorized to access the API.
// The standard studio.buf.build is always authorized by default.
func WithCORS(domains ...string) ConnectHubOption {
return func(cnf *ConnectHub) {
cnf.cors = append(cnf.cors, domains...)
}
}
// WithInterceptors configures the interceptors to configure in the APIs.
func WithInterceptors(interceptors ...connect.Interceptor) ConnectHubOption {
return func(cnf *ConnectHub) {
cnf.interceptors = append(cnf.interceptors, interceptors...)
}
}
// Deprecated: Use NewConnectHub instead.
type RegisterFn func() (pattern string, handler http.Handler)
// Deprecated: Use NewConnectHub instead.
func Connect(r *routing.Router, fn RegisterFn) {
pattern, handler := fn()
r.PathPrefixHandler(pattern, routing.NewHandlerFromHTTP(handler))
}
// Deprecated: Use NewConnectHub instead.
func ConnectCORS(origins []string) cors.Options {
return cors.Options{
AllowedOrigins: origins,
AllowedMethods: []string{http.MethodPost, http.MethodOptions},
AllowedHeaders: []string{"authorization", "content-type"},
MaxAge: 300,
}
}
// Deprecated: Use NewConnectHub instead.
func ConnectOptions(interceptors ...connect.Interceptor) []connect.HandlerOption {
return []connect.HandlerOption{}
}
type codecJSON struct{}
func (c *codecJSON) Name() string {
return "json"
}
func (c *codecJSON) Marshal(message any) ([]byte, error) {
msg, ok := message.(proto.Message)
if !ok {
return nil, errors.Errorf("%T doesn't implement proto.Message", message)
}
m := protojson.MarshalOptions{
EmitUnpopulated: true,
}
return m.Marshal(msg)
}
func (c *codecJSON) Unmarshal(binary []byte, message any) error {
msg, ok := message.(proto.Message)
if !ok {
return errors.Errorf("%T doesn't implement proto.Message", message)
}
return protojson.Unmarshal(binary, msg)
}