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