grpc request
This commit is contained in:
parent
e1bc240a14
commit
c17d0fcc0f
@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/micro/go-micro/codec"
|
||||
raw "github.com/micro/go-micro/codec/bytes"
|
||||
"github.com/micro/go-micro/codec/grpc"
|
||||
"github.com/micro/go-micro/codec/json"
|
||||
"github.com/micro/go-micro/codec/jsonrpc"
|
||||
"github.com/micro/go-micro/codec/proto"
|
||||
@ -48,6 +49,9 @@ var (
|
||||
DefaultContentType = "application/protobuf"
|
||||
|
||||
DefaultCodecs = map[string]codec.NewCodec{
|
||||
"application/grpc": grpc.NewCodec,
|
||||
"application/grpc+json": grpc.NewCodec,
|
||||
"application/grpc+proto": grpc.NewCodec,
|
||||
"application/protobuf": proto.NewCodec,
|
||||
"application/json": json.NewCodec,
|
||||
"application/json-rpc": jsonrpc.NewCodec,
|
||||
|
@ -4,6 +4,7 @@ package grpc
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
@ -77,8 +78,23 @@ func (c *Codec) Write(m *codec.Message, b interface{}) error {
|
||||
c.ContentType = ct
|
||||
}
|
||||
|
||||
m.Header["Trailer"] = "grpc-status, grpc-message"
|
||||
switch m.Type {
|
||||
case codec.Request:
|
||||
parts := strings.Split(m.Endpoint, ".")
|
||||
m.Header[":method"] = "POST"
|
||||
m.Header[":path"] = fmt.Sprintf("/%s.%s/%s", m.Target, parts[0], parts[1])
|
||||
m.Header[":proto"] = "HTTP/2.0"
|
||||
m.Header["te"] = "trailers"
|
||||
m.Header["user-agent"] = "grpc-go/1.0.0"
|
||||
m.Header[":authority"] = m.Target
|
||||
m.Header["content-type"] = c.ContentType
|
||||
case codec.Response:
|
||||
m.Header["Trailer"] = "grpc-status, grpc-message"
|
||||
m.Header["grpc-status"] = "0"
|
||||
m.Header["grpc-message"] = ""
|
||||
}
|
||||
|
||||
// marshal content
|
||||
switch c.ContentType {
|
||||
case "application/grpc+json":
|
||||
buf, err = json.Marshal(b)
|
||||
@ -90,16 +106,13 @@ func (c *Codec) Write(m *codec.Message, b interface{}) error {
|
||||
default:
|
||||
err = errors.New("Unsupported Content-Type")
|
||||
}
|
||||
|
||||
// check error
|
||||
if err != nil {
|
||||
m.Header["grpc-status"] = "8"
|
||||
m.Header["grpc-message"] = err.Error()
|
||||
return err
|
||||
}
|
||||
|
||||
m.Header["grpc-status"] = "0"
|
||||
m.Header["grpc-message"] = ""
|
||||
|
||||
return encode(0, buf, c.Conn)
|
||||
}
|
||||
|
||||
|
@ -153,8 +153,8 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
|
||||
// write an error response
|
||||
rcodec.Write(&codec.Message{
|
||||
Header: msg.Header,
|
||||
Error: err.Error(),
|
||||
Type: codec.Error,
|
||||
Error: err.Error(),
|
||||
Type: codec.Error,
|
||||
}, nil)
|
||||
|
||||
s.wg.Done()
|
||||
|
Loading…
x
Reference in New Issue
Block a user