2021-02-17 17:14:47 +08:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-31 16:10:17 +08:00
|
|
|
"net/url"
|
2021-02-17 17:14:47 +08:00
|
|
|
|
|
|
|
// init encoding
|
2021-07-01 20:36:12 +08:00
|
|
|
_ "github.com/go-kratos/kratos/v2/encoding/form"
|
2021-02-17 17:14:47 +08:00
|
|
|
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
|
|
|
_ "github.com/go-kratos/kratos/v2/encoding/proto"
|
2021-05-11 11:36:20 +08:00
|
|
|
_ "github.com/go-kratos/kratos/v2/encoding/xml"
|
2021-05-17 21:52:23 +08:00
|
|
|
_ "github.com/go-kratos/kratos/v2/encoding/yaml"
|
2021-02-17 17:14:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server is transport server.
|
|
|
|
type Server interface {
|
2021-05-29 23:52:05 +08:00
|
|
|
Start(context.Context) error
|
|
|
|
Stop(context.Context) error
|
2021-02-17 17:14:47 +08:00
|
|
|
}
|
|
|
|
|
2021-05-28 15:30:55 +08:00
|
|
|
// Endpointer is registry endpoint.
|
|
|
|
type Endpointer interface {
|
2021-05-31 16:10:17 +08:00
|
|
|
Endpoint() (*url.URL, error)
|
2021-05-28 15:30:55 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 00:59:07 +08:00
|
|
|
// Header is the storage medium used by a Header.
|
|
|
|
type Header interface {
|
|
|
|
Get(key string) string
|
|
|
|
Set(key string, value string)
|
2023-04-10 09:35:38 +03:00
|
|
|
Add(key string, value string)
|
2021-06-14 00:59:07 +08:00
|
|
|
Keys() []string
|
2023-04-10 09:35:38 +03:00
|
|
|
Values(key string) []string
|
2021-06-14 00:59:07 +08:00
|
|
|
}
|
|
|
|
|
2021-06-11 15:05:57 +08:00
|
|
|
// Transporter is transport context value interface.
|
|
|
|
type Transporter interface {
|
2021-08-16 10:35:08 +08:00
|
|
|
// Kind transporter
|
2021-06-17 10:26:45 +08:00
|
|
|
// grpc
|
|
|
|
// http
|
2021-06-15 11:05:13 +08:00
|
|
|
Kind() Kind
|
2021-08-16 10:35:08 +08:00
|
|
|
// Endpoint return server or client endpoint
|
2021-06-17 10:26:45 +08:00
|
|
|
// Server Transport: grpc://127.0.0.1:9000
|
|
|
|
// Client Transport: discovery:///provider-demo
|
2021-06-11 15:05:57 +08:00
|
|
|
Endpoint() string
|
2021-08-16 10:35:08 +08:00
|
|
|
// Operation Service full method selector generated by protobuf
|
2021-06-17 10:26:45 +08:00
|
|
|
// example: /helloworld.Greeter/SayHello
|
2021-06-12 18:30:17 +08:00
|
|
|
Operation() string
|
2021-08-16 10:35:08 +08:00
|
|
|
// RequestHeader return transport request header
|
2021-06-17 10:26:45 +08:00
|
|
|
// http: http.Header
|
|
|
|
// grpc: metadata.MD
|
2021-06-29 15:33:18 +08:00
|
|
|
RequestHeader() Header
|
2021-08-16 10:35:08 +08:00
|
|
|
// ReplyHeader return transport reply/response header
|
2021-06-29 15:33:18 +08:00
|
|
|
// only valid for server transport
|
|
|
|
// http: http.Header
|
|
|
|
// grpc: metadata.MD
|
|
|
|
ReplyHeader() Header
|
2021-02-17 17:14:47 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 11:05:13 +08:00
|
|
|
// Kind defines the type of Transport
|
|
|
|
type Kind string
|
|
|
|
|
2021-06-29 15:33:18 +08:00
|
|
|
func (k Kind) String() string { return string(k) }
|
2021-06-15 11:05:13 +08:00
|
|
|
|
|
|
|
// Defines a set of transport kind
|
|
|
|
const (
|
|
|
|
KindGRPC Kind = "grpc"
|
|
|
|
KindHTTP Kind = "http"
|
|
|
|
)
|
|
|
|
|
2021-08-31 10:14:57 +08:00
|
|
|
type (
|
|
|
|
serverTransportKey struct{}
|
|
|
|
clientTransportKey struct{}
|
|
|
|
)
|
2021-03-05 21:09:02 +08:00
|
|
|
|
2021-06-11 15:05:57 +08:00
|
|
|
// NewServerContext returns a new Context that carries value.
|
|
|
|
func NewServerContext(ctx context.Context, tr Transporter) context.Context {
|
|
|
|
return context.WithValue(ctx, serverTransportKey{}, tr)
|
|
|
|
}
|
2021-03-05 21:09:02 +08:00
|
|
|
|
2021-06-11 15:05:57 +08:00
|
|
|
// FromServerContext returns the Transport value stored in ctx, if any.
|
|
|
|
func FromServerContext(ctx context.Context) (tr Transporter, ok bool) {
|
|
|
|
tr, ok = ctx.Value(serverTransportKey{}).(Transporter)
|
|
|
|
return
|
|
|
|
}
|
2021-02-17 17:14:47 +08:00
|
|
|
|
2021-06-11 15:05:57 +08:00
|
|
|
// NewClientContext returns a new Context that carries value.
|
|
|
|
func NewClientContext(ctx context.Context, tr Transporter) context.Context {
|
|
|
|
return context.WithValue(ctx, clientTransportKey{}, tr)
|
2021-02-17 17:14:47 +08:00
|
|
|
}
|
|
|
|
|
2021-06-11 15:05:57 +08:00
|
|
|
// FromClientContext returns the Transport value stored in ctx, if any.
|
|
|
|
func FromClientContext(ctx context.Context) (tr Transporter, ok bool) {
|
|
|
|
tr, ok = ctx.Value(clientTransportKey{}).(Transporter)
|
2021-02-17 17:14:47 +08:00
|
|
|
return
|
|
|
|
}
|