You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.1 KiB
59 lines
1.1 KiB
2 years ago
|
package transport
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"net/url"
|
||
|
)
|
||
|
|
||
|
type Server interface {
|
||
|
Start(context.Context) error
|
||
|
Stop(context.Context) error
|
||
|
}
|
||
|
|
||
|
type Endpointer interface {
|
||
|
Endpoint() (*url.URL, error)
|
||
|
}
|
||
|
type Header interface {
|
||
|
Get(key string) string
|
||
|
Set(key string, value string)
|
||
|
Keys() []string
|
||
|
}
|
||
|
type Transporter interface {
|
||
|
Kind() Kind
|
||
|
Endpoint() string
|
||
|
Operation() string
|
||
|
RequestHeader() Header
|
||
|
ReplyHeader() Header
|
||
|
}
|
||
|
type Kind string
|
||
|
|
||
|
func (k Kind) String() string {
|
||
|
return string(k)
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
KindGRPC Kind = "grpc"
|
||
|
KindHTTP Kind = "http"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
serverTransportKey struct{}
|
||
|
clientTransportKey struct{}
|
||
|
)
|
||
|
|
||
|
func NewServerContext(ctx context.Context, tr Transporter) context.Context {
|
||
|
return context.WithValue(ctx, serverTransportKey{}, tr)
|
||
|
}
|
||
|
func FromServerContext(ctx context.Context) (tr Transporter, ok bool) {
|
||
|
tr, ok = ctx.Value(serverTransportKey{}).(Transporter)
|
||
|
return
|
||
|
}
|
||
|
func NewClientContext(ctx context.Context, tr Transporter) context.Context {
|
||
|
return context.WithValue(ctx, clientTransportKey{}, tr)
|
||
|
}
|
||
|
|
||
|
func FromClientContext(ctx context.Context) (tr Transporter, ok bool) {
|
||
|
tr, ok = ctx.Value(clientTransportKey{}).(Transporter)
|
||
|
return
|
||
|
}
|