2019-06-03 20:44:43 +03:00
|
|
|
// Package rpc is a go-micro rpc handler.
|
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-04-15 01:37:15 +03:00
|
|
|
"net/textproto"
|
2019-06-03 20:44:43 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-03-27 10:59:31 +03:00
|
|
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
2019-06-03 20:44:43 +03:00
|
|
|
"github.com/joncalhoun/qson"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/api"
|
|
|
|
"github.com/micro/go-micro/v2/api/handler"
|
2020-03-30 11:04:59 +03:00
|
|
|
"github.com/micro/go-micro/v2/api/internal/proto"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/client/selector"
|
|
|
|
"github.com/micro/go-micro/v2/codec"
|
|
|
|
"github.com/micro/go-micro/v2/codec/jsonrpc"
|
|
|
|
"github.com/micro/go-micro/v2/codec/protorpc"
|
|
|
|
"github.com/micro/go-micro/v2/errors"
|
2020-03-23 10:31:35 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-03-30 11:04:59 +03:00
|
|
|
"github.com/micro/go-micro/v2/metadata"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
|
|
|
"github.com/micro/go-micro/v2/util/ctx"
|
2020-03-26 14:29:28 +03:00
|
|
|
"github.com/oxtoacart/bpool"
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Handler = "rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// supported json codecs
|
|
|
|
jsonCodecs = []string{
|
|
|
|
"application/grpc+json",
|
|
|
|
"application/json",
|
|
|
|
"application/json-rpc",
|
|
|
|
}
|
|
|
|
|
|
|
|
// support proto codecs
|
|
|
|
protoCodecs = []string{
|
|
|
|
"application/grpc",
|
|
|
|
"application/grpc+proto",
|
|
|
|
"application/proto",
|
|
|
|
"application/protobuf",
|
|
|
|
"application/proto-rpc",
|
|
|
|
"application/octet-stream",
|
|
|
|
}
|
2020-03-26 14:29:28 +03:00
|
|
|
|
|
|
|
bufferPool = bpool.NewSizedBufferPool(1024, 8)
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type rpcHandler struct {
|
|
|
|
opts handler.Options
|
|
|
|
s *api.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
type buffer struct {
|
|
|
|
io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) Write(_ []byte) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// strategy is a hack for selection
|
|
|
|
func strategy(services []*registry.Service) selector.Strategy {
|
|
|
|
return func(_ []*registry.Service) selector.Next {
|
|
|
|
// ignore input to this function, use services above
|
|
|
|
return selector.Random(services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2020-03-26 14:29:28 +03:00
|
|
|
bsize := handler.DefaultMaxRecvSize
|
|
|
|
if h.opts.MaxRecvSize > 0 {
|
|
|
|
bsize = h.opts.MaxRecvSize
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, bsize)
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
defer r.Body.Close()
|
|
|
|
var service *api.Service
|
|
|
|
|
|
|
|
if h.s != nil {
|
|
|
|
// we were given the service
|
|
|
|
service = h.s
|
|
|
|
} else if h.opts.Router != nil {
|
|
|
|
// try get service from router
|
|
|
|
s, err := h.opts.Router.Route(r)
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, r, errors.InternalServerError("go.micro.api", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
service = s
|
|
|
|
} else {
|
|
|
|
// we have no way of routing the request
|
|
|
|
writeError(w, r, errors.InternalServerError("go.micro.api", "no route found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ct := r.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
// Strip charset from Content-Type (like `application/json; charset=UTF-8`)
|
|
|
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
|
|
|
ct = ct[:idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// micro client
|
2020-04-12 16:29:38 +03:00
|
|
|
c := h.opts.Client
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2020-03-10 18:21:43 +03:00
|
|
|
// create context
|
|
|
|
cx := ctx.FromRequest(r)
|
2020-03-31 22:36:51 +03:00
|
|
|
// get context from http handler wrappers
|
2020-04-12 13:16:08 +03:00
|
|
|
md, ok := metadata.FromContext(r.Context())
|
2020-03-31 22:36:51 +03:00
|
|
|
if !ok {
|
|
|
|
md = make(metadata.Metadata)
|
|
|
|
}
|
2020-04-11 22:21:55 +03:00
|
|
|
// fill contex with http headers
|
|
|
|
md["Host"] = r.Host
|
2020-04-15 01:37:15 +03:00
|
|
|
md["Method"] = r.Method
|
|
|
|
// get canonical headers
|
|
|
|
for k, _ := range r.Header {
|
|
|
|
// may be need to get all values for key like r.Header.Values() provide in go 1.14
|
|
|
|
md[textproto.CanonicalMIMEHeaderKey(k)] = r.Header.Get(k)
|
|
|
|
}
|
|
|
|
|
2020-03-31 22:36:51 +03:00
|
|
|
// merge context with overwrite
|
|
|
|
cx = metadata.MergeContext(cx, md, true)
|
|
|
|
|
|
|
|
// set merged context to request
|
2020-04-01 01:50:37 +03:00
|
|
|
*r = *r.Clone(cx)
|
2020-03-10 18:21:43 +03:00
|
|
|
// if stream we currently only support json
|
|
|
|
if isStream(r, service) {
|
2020-04-02 12:13:04 +03:00
|
|
|
// drop older context as it can have timeouts and create new
|
|
|
|
// md, _ := metadata.FromContext(cx)
|
|
|
|
//serveWebsocket(context.TODO(), w, r, service, c)
|
2020-03-10 18:21:43 +03:00
|
|
|
serveWebsocket(cx, w, r, service, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
// create strategy
|
|
|
|
so := selector.WithStrategy(strategy(service.Services))
|
|
|
|
|
2020-03-10 18:21:43 +03:00
|
|
|
// walk the standard call path
|
2019-06-03 20:44:43 +03:00
|
|
|
// get payload
|
|
|
|
br, err := requestPayload(r)
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rsp []byte
|
|
|
|
|
|
|
|
switch {
|
2019-06-24 16:49:19 +03:00
|
|
|
// proto codecs
|
|
|
|
case hasCodec(ct, protoCodecs):
|
|
|
|
request := &proto.Message{}
|
2019-06-03 20:44:43 +03:00
|
|
|
// if the extracted payload isn't empty lets use it
|
|
|
|
if len(br) > 0 {
|
2019-06-24 16:49:19 +03:00
|
|
|
request = proto.NewMessage(br)
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// create request/response
|
2019-06-24 16:49:19 +03:00
|
|
|
response := &proto.Message{}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
req := c.NewRequest(
|
|
|
|
service.Name,
|
|
|
|
service.Endpoint.Name,
|
2019-06-24 16:49:19 +03:00
|
|
|
request,
|
2019-06-03 20:44:43 +03:00
|
|
|
client.WithContentType(ct),
|
|
|
|
)
|
|
|
|
|
|
|
|
// make the call
|
2019-06-24 16:49:19 +03:00
|
|
|
if err := c.Call(cx, req, response, client.WithSelectOption(so)); err != nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
writeError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshall response
|
2020-03-30 11:04:59 +03:00
|
|
|
rsp, err = response.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-24 16:49:19 +03:00
|
|
|
default:
|
|
|
|
// if json codec is not present set to json
|
|
|
|
if !hasCodec(ct, jsonCodecs) {
|
|
|
|
ct = "application/json"
|
|
|
|
}
|
|
|
|
|
|
|
|
// default to trying json
|
|
|
|
var request json.RawMessage
|
2019-06-03 20:44:43 +03:00
|
|
|
// if the extracted payload isn't empty lets use it
|
|
|
|
if len(br) > 0 {
|
2019-06-24 16:49:19 +03:00
|
|
|
request = json.RawMessage(br)
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// create request/response
|
2019-06-24 16:49:19 +03:00
|
|
|
var response json.RawMessage
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
req := c.NewRequest(
|
|
|
|
service.Name,
|
|
|
|
service.Endpoint.Name,
|
2019-06-24 16:49:19 +03:00
|
|
|
&request,
|
2019-06-03 20:44:43 +03:00
|
|
|
client.WithContentType(ct),
|
|
|
|
)
|
|
|
|
|
|
|
|
// make the call
|
2019-06-24 16:49:19 +03:00
|
|
|
if err := c.Call(cx, req, &response, client.WithSelectOption(so)); err != nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
writeError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshall response
|
2020-03-30 11:04:59 +03:00
|
|
|
rsp, err = response.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the response
|
|
|
|
writeResponse(w, r, rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rh *rpcHandler) String() string {
|
|
|
|
return "rpc"
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasCodec(ct string, codecs []string) bool {
|
|
|
|
for _, codec := range codecs {
|
|
|
|
if ct == codec {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// requestPayload takes a *http.Request.
|
|
|
|
// If the request is a GET the query string parameters are extracted and marshaled to JSON and the raw bytes are returned.
|
|
|
|
// If the request method is a POST the request body is read and returned
|
|
|
|
func requestPayload(r *http.Request) ([]byte, error) {
|
2020-03-30 11:04:59 +03:00
|
|
|
var err error
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
// we have to decode json-rpc and proto-rpc because we suck
|
|
|
|
// well actually because there's no proxy codec right now
|
2020-03-30 11:04:59 +03:00
|
|
|
|
2020-02-11 14:27:16 +03:00
|
|
|
ct := r.Header.Get("Content-Type")
|
|
|
|
switch {
|
|
|
|
case strings.Contains(ct, "application/json-rpc"):
|
2019-06-03 20:44:43 +03:00
|
|
|
msg := codec.Message{
|
|
|
|
Type: codec.Request,
|
|
|
|
Header: make(map[string]string),
|
|
|
|
}
|
|
|
|
c := jsonrpc.NewCodec(&buffer{r.Body})
|
2020-03-30 11:04:59 +03:00
|
|
|
if err = c.ReadHeader(&msg, codec.Request); err != nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var raw json.RawMessage
|
2020-03-30 11:04:59 +03:00
|
|
|
if err = c.ReadBody(&raw); err != nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ([]byte)(raw), nil
|
2020-02-11 14:27:16 +03:00
|
|
|
case strings.Contains(ct, "application/proto-rpc"), strings.Contains(ct, "application/octet-stream"):
|
2019-06-03 20:44:43 +03:00
|
|
|
msg := codec.Message{
|
|
|
|
Type: codec.Request,
|
|
|
|
Header: make(map[string]string),
|
|
|
|
}
|
|
|
|
c := protorpc.NewCodec(&buffer{r.Body})
|
2020-03-30 11:04:59 +03:00
|
|
|
if err = c.ReadHeader(&msg, codec.Request); err != nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var raw proto.Message
|
2020-03-30 11:04:59 +03:00
|
|
|
if err = c.ReadBody(&raw); err != nil {
|
2019-06-03 20:44:43 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-30 11:04:59 +03:00
|
|
|
return raw.Marshal()
|
2020-02-11 14:27:16 +03:00
|
|
|
case strings.Contains(ct, "application/www-x-form-urlencoded"):
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
// generate a new set of values from the form
|
|
|
|
vals := make(map[string]string)
|
|
|
|
for k, v := range r.Form {
|
|
|
|
vals[k] = strings.Join(v, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal
|
2020-03-30 11:04:59 +03:00
|
|
|
return json.Marshal(vals)
|
2020-02-11 14:27:16 +03:00
|
|
|
// TODO: application/grpc
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise as per usual
|
2020-03-30 11:04:59 +03:00
|
|
|
ctx := r.Context()
|
|
|
|
// dont user meadata.FromContext as it mangles names
|
2020-04-12 13:16:08 +03:00
|
|
|
md, ok := metadata.FromContext(ctx)
|
2020-03-30 11:04:59 +03:00
|
|
|
if !ok {
|
|
|
|
md = make(map[string]string)
|
|
|
|
}
|
2020-04-01 01:26:58 +03:00
|
|
|
|
2020-03-30 11:04:59 +03:00
|
|
|
// allocate maximum
|
2020-04-01 01:26:58 +03:00
|
|
|
matches := make(map[string]interface{}, len(md))
|
2020-04-07 17:38:27 +03:00
|
|
|
bodydst := ""
|
2020-04-01 00:23:17 +03:00
|
|
|
|
|
|
|
// get fields from url path
|
2020-03-30 11:04:59 +03:00
|
|
|
for k, v := range md {
|
2020-04-12 16:40:37 +03:00
|
|
|
k = strings.ToLower(k)
|
2020-03-31 22:36:51 +03:00
|
|
|
// filter own keys
|
2020-03-30 11:04:59 +03:00
|
|
|
if strings.HasPrefix(k, "x-api-field-") {
|
|
|
|
matches[strings.TrimPrefix(k, "x-api-field-")] = v
|
2020-03-31 21:59:35 +03:00
|
|
|
delete(md, k)
|
2020-04-07 17:38:27 +03:00
|
|
|
} else if k == "x-api-body" {
|
|
|
|
bodydst = v
|
|
|
|
delete(md, k)
|
2020-03-30 11:04:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:26:58 +03:00
|
|
|
// map of all fields
|
|
|
|
req := make(map[string]interface{}, len(md))
|
|
|
|
|
2020-04-01 00:23:17 +03:00
|
|
|
// get fields from url values
|
|
|
|
if len(r.URL.RawQuery) > 0 {
|
2020-04-01 01:26:58 +03:00
|
|
|
umd := make(map[string]interface{})
|
2020-04-01 00:23:17 +03:00
|
|
|
err = qson.Unmarshal(&umd, r.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range umd {
|
|
|
|
matches[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 11:04:59 +03:00
|
|
|
// restore context without fields
|
2020-04-01 01:50:37 +03:00
|
|
|
*r = *r.Clone(metadata.NewContext(ctx, md))
|
2020-03-31 22:36:51 +03:00
|
|
|
|
2020-03-30 11:04:59 +03:00
|
|
|
for k, v := range matches {
|
|
|
|
ps := strings.Split(k, ".")
|
|
|
|
if len(ps) == 1 {
|
|
|
|
req[k] = v
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
em := make(map[string]interface{})
|
|
|
|
em[ps[len(ps)-1]] = v
|
|
|
|
for i := len(ps) - 2; i > 0; i-- {
|
|
|
|
nm := make(map[string]interface{})
|
|
|
|
nm[ps[i]] = em
|
|
|
|
em = nm
|
|
|
|
}
|
2020-04-01 00:23:17 +03:00
|
|
|
if vm, ok := req[ps[0]]; ok {
|
|
|
|
// nested map
|
|
|
|
nm := vm.(map[string]interface{})
|
|
|
|
for vk, vv := range em {
|
|
|
|
nm[vk] = vv
|
|
|
|
}
|
2020-04-01 01:26:58 +03:00
|
|
|
req[ps[0]] = nm
|
2020-04-01 00:23:17 +03:00
|
|
|
} else {
|
|
|
|
req[ps[0]] = em
|
|
|
|
}
|
2020-03-30 11:04:59 +03:00
|
|
|
}
|
|
|
|
pathbuf := []byte("{}")
|
|
|
|
if len(req) > 0 {
|
|
|
|
pathbuf, err = json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 00:23:17 +03:00
|
|
|
urlbuf := []byte("{}")
|
2020-03-30 11:04:59 +03:00
|
|
|
out, err := jsonpatch.MergeMergePatches(urlbuf, pathbuf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
2020-03-30 11:04:59 +03:00
|
|
|
// empty response
|
|
|
|
if strings.Contains(ct, "application/json") && string(out) == "{}" {
|
|
|
|
return out, nil
|
|
|
|
} else if string(out) == "{}" && !strings.Contains(ct, "application/json") {
|
|
|
|
return []byte{}, nil
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2020-03-30 11:04:59 +03:00
|
|
|
return out, nil
|
2020-03-27 10:59:31 +03:00
|
|
|
case "PATCH", "POST", "PUT", "DELETE":
|
2020-03-30 11:04:59 +03:00
|
|
|
bodybuf := []byte("{}")
|
2020-03-26 14:29:28 +03:00
|
|
|
buf := bufferPool.Get()
|
|
|
|
defer bufferPool.Put(buf)
|
|
|
|
if _, err := buf.ReadFrom(r.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-27 10:59:31 +03:00
|
|
|
if b := buf.Bytes(); len(b) > 0 {
|
2020-03-30 11:04:59 +03:00
|
|
|
bodybuf = b
|
2020-03-27 10:59:31 +03:00
|
|
|
}
|
2020-04-07 17:38:27 +03:00
|
|
|
if bodydst == "" || bodydst == "*" {
|
|
|
|
if out, err = jsonpatch.MergeMergePatches(out, bodybuf); err == nil {
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dstmap := make(map[string]interface{})
|
|
|
|
ps := strings.Split(bodydst, ".")
|
|
|
|
if len(ps) == 1 {
|
|
|
|
dstmap[ps[0]] = bodybuf
|
|
|
|
} else {
|
|
|
|
em := make(map[string]interface{})
|
|
|
|
em[ps[len(ps)-1]] = bodybuf
|
|
|
|
for i := len(ps) - 2; i > 0; i-- {
|
|
|
|
nm := make(map[string]interface{})
|
|
|
|
nm[ps[i]] = em
|
|
|
|
em = nm
|
|
|
|
}
|
|
|
|
dstmap[ps[0]] = em
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyout, err := json.Marshal(dstmap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-27 10:59:31 +03:00
|
|
|
|
2020-04-07 17:38:27 +03:00
|
|
|
if out, err = jsonpatch.MergeMergePatches(out, bodyout); err == nil {
|
2020-03-27 10:59:31 +03:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//fallback to previous unknown behaviour
|
2020-03-30 11:04:59 +03:00
|
|
|
return bodybuf, nil
|
2020-03-27 10:59:31 +03:00
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeError(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
ce := errors.Parse(err.Error())
|
|
|
|
|
|
|
|
switch ce.Code {
|
|
|
|
case 0:
|
|
|
|
// assuming it's totally screwed
|
|
|
|
ce.Code = 500
|
|
|
|
ce.Id = "go.micro.api"
|
|
|
|
ce.Status = http.StatusText(500)
|
|
|
|
ce.Detail = "error during request: " + ce.Detail
|
|
|
|
w.WriteHeader(500)
|
|
|
|
default:
|
|
|
|
w.WriteHeader(int(ce.Code))
|
|
|
|
}
|
|
|
|
|
|
|
|
// response content type
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
// Set trailers
|
|
|
|
if strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
|
|
|
|
w.Header().Set("Trailer", "grpc-status")
|
|
|
|
w.Header().Set("Trailer", "grpc-message")
|
|
|
|
w.Header().Set("grpc-status", "13")
|
|
|
|
w.Header().Set("grpc-message", ce.Detail)
|
|
|
|
}
|
|
|
|
|
2020-03-23 10:31:35 +03:00
|
|
|
_, werr := w.Write([]byte(ce.Error()))
|
2020-03-30 11:04:59 +03:00
|
|
|
if werr != nil {
|
2020-03-23 10:31:35 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(werr)
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponse(w http.ResponseWriter, r *http.Request, rsp []byte) {
|
|
|
|
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(rsp)))
|
|
|
|
|
|
|
|
// Set trailers
|
|
|
|
if strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
|
|
|
|
w.Header().Set("Trailer", "grpc-status")
|
|
|
|
w.Header().Set("Trailer", "grpc-message")
|
|
|
|
w.Header().Set("grpc-status", "0")
|
|
|
|
w.Header().Set("grpc-message", "")
|
|
|
|
}
|
|
|
|
|
2020-03-30 11:04:59 +03:00
|
|
|
// write 204 status if rsp is nil
|
|
|
|
if len(rsp) == 0 {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
// write response
|
2020-03-23 10:31:35 +03:00
|
|
|
_, err := w.Write(rsp)
|
|
|
|
if err != nil {
|
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
|
|
|
options := handler.NewOptions(opts...)
|
|
|
|
return &rpcHandler{
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithService(s *api.Service, opts ...handler.Option) handler.Handler {
|
|
|
|
options := handler.NewOptions(opts...)
|
|
|
|
return &rpcHandler{
|
|
|
|
opts: options,
|
|
|
|
s: s,
|
|
|
|
}
|
|
|
|
}
|