|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.diulo.com/mogfee/kit/transport"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Transporter interface {
|
|
|
|
transport.Transporter
|
|
|
|
Request() *http.Request
|
|
|
|
PathTemplate() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Transport struct {
|
|
|
|
endpoint string
|
|
|
|
operation string
|
|
|
|
reqHeader headerCarrier
|
|
|
|
replyHeader headerCarrier
|
|
|
|
request *http.Request
|
|
|
|
pathTemplate string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) Kind() transport.Kind {
|
|
|
|
return transport.KindHTTP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) Endpoint() string {
|
|
|
|
return t.endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) Operation() string {
|
|
|
|
return t.operation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) RequestHeader() transport.Header {
|
|
|
|
return t.reqHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) ReplyHeader() transport.Header {
|
|
|
|
return t.replyHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) Request() *http.Request {
|
|
|
|
return t.request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transport) PathTemplate() string {
|
|
|
|
return t.pathTemplate
|
|
|
|
}
|
|
|
|
func SetOperation(ctx context.Context, op string) {
|
|
|
|
if tr, ok := transport.FromServerContext(ctx); ok {
|
|
|
|
if tr, ok := tr.(*Transport); ok {
|
|
|
|
tr.operation = op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func RequestFromServerContext(ctx context.Context) (*http.Request, bool) {
|
|
|
|
if tr, ok := transport.FromServerContext(ctx); ok {
|
|
|
|
if tr, ok := tr.(*Transport); ok {
|
|
|
|
return tr.request, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerCarrier http.Header
|
|
|
|
|
|
|
|
func (h headerCarrier) Get(key string) string {
|
|
|
|
return http.Header(h).Get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h headerCarrier) Set(key string, value string) {
|
|
|
|
http.Header(h).Set(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h headerCarrier) Keys() []string {
|
|
|
|
kvs := make([]string, 0, len(h))
|
|
|
|
for k := range http.Header(h) {
|
|
|
|
kvs = append(kvs, k)
|
|
|
|
}
|
|
|
|
return kvs
|
|
|
|
}
|