Merge pull request #372 from micro/codec
Switch default codec and add default codec for server
This commit is contained in:
commit
9544058af3
@ -99,7 +99,7 @@ func newOptions(options ...Option) Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(opts.ContentType) == 0 {
|
if len(opts.ContentType) == 0 {
|
||||||
opts.ContentType = defaultContentType
|
opts.ContentType = DefaultContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Broker == nil {
|
if opts.Broker == nil {
|
||||||
|
@ -49,7 +49,7 @@ func (r *rpcClient) newCodec(contentType string) (codec.NewCodec, error) {
|
|||||||
if c, ok := r.opts.Codecs[contentType]; ok {
|
if c, ok := r.opts.Codecs[contentType]; ok {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
if cf, ok := defaultCodecs[contentType]; ok {
|
if cf, ok := DefaultCodecs[contentType]; ok {
|
||||||
return cf, nil
|
return cf, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/micro/go-micro/codec"
|
"github.com/micro/go-micro/codec"
|
||||||
|
raw "github.com/micro/go-micro/codec/bytes"
|
||||||
"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"
|
||||||
@ -66,14 +67,14 @@ type response struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultContentType = "application/octet-stream"
|
DefaultContentType = "application/protobuf"
|
||||||
|
|
||||||
defaultCodecs = map[string]codec.NewCodec{
|
DefaultCodecs = map[string]codec.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,
|
||||||
"application/proto-rpc": protorpc.NewCodec,
|
"application/proto-rpc": protorpc.NewCodec,
|
||||||
"application/octet-stream": protorpc.NewCodec,
|
"application/octet-stream": raw.NewCodec,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/micro/go-micro/codec"
|
"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/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"
|
||||||
@ -29,7 +30,9 @@ type readWriteCloser struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultCodecs = map[string]codec.NewCodec{
|
DefaultContentType = "application/protobuf"
|
||||||
|
|
||||||
|
DefaultCodecs = map[string]codec.NewCodec{
|
||||||
"application/grpc": grpc.NewCodec,
|
"application/grpc": grpc.NewCodec,
|
||||||
"application/grpc+json": grpc.NewCodec,
|
"application/grpc+json": grpc.NewCodec,
|
||||||
"application/grpc+proto": grpc.NewCodec,
|
"application/grpc+proto": grpc.NewCodec,
|
||||||
@ -37,7 +40,7 @@ var (
|
|||||||
"application/json-rpc": jsonrpc.NewCodec,
|
"application/json-rpc": jsonrpc.NewCodec,
|
||||||
"application/protobuf": proto.NewCodec,
|
"application/protobuf": proto.NewCodec,
|
||||||
"application/proto-rpc": protorpc.NewCodec,
|
"application/proto-rpc": protorpc.NewCodec,
|
||||||
"application/octet-stream": protorpc.NewCodec,
|
"application/octet-stream": raw.NewCodec,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -70,8 +70,13 @@ func (s *rpcServer) accept(sock transport.Socket) {
|
|||||||
// we use this Content-Type header to identify the codec needed
|
// we use this Content-Type header to identify the codec needed
|
||||||
ct := msg.Header["Content-Type"]
|
ct := msg.Header["Content-Type"]
|
||||||
|
|
||||||
cf, err := s.newCodec(ct)
|
// no content type
|
||||||
|
if len(ct) == 0 {
|
||||||
|
ct = DefaultContentType
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: needs better error handling
|
// TODO: needs better error handling
|
||||||
|
cf, err := s.newCodec(ct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sock.Send(&transport.Message{
|
sock.Send(&transport.Message{
|
||||||
Header: map[string]string{
|
Header: map[string]string{
|
||||||
@ -120,7 +125,7 @@ func (s *rpcServer) newCodec(contentType string) (codec.NewCodec, error) {
|
|||||||
if cf, ok := s.opts.Codecs[contentType]; ok {
|
if cf, ok := s.opts.Codecs[contentType]; ok {
|
||||||
return cf, nil
|
return cf, nil
|
||||||
}
|
}
|
||||||
if cf, ok := defaultCodecs[contentType]; ok {
|
if cf, ok := DefaultCodecs[contentType]; ok {
|
||||||
return cf, nil
|
return cf, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user