Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46f1634baa | ||
|
|
187aaba578 | ||
|
|
a42aeacad0 | ||
|
|
d4091f8805 |
No files matched your search
@@ -1,5 +1,3 @@
|
|||||||
### v1.2.0
|
|
||||||
* Добавлена трансляция заголовков `x-content-encoding` и `x-accept-encoding` в стандартные для http - `Content-Encoding` и `Accept-Encoding`
|
|
||||||
### v1.1.2
|
### v1.1.2
|
||||||
* обновлены зависимости
|
* обновлены зависимости
|
||||||
### v1.1.1
|
### v1.1.1
|
||||||
|
|||||||
+29
-106
@@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/txix-open/isp-kit/grpc/isp"
|
"github.com/txix-open/isp-kit/grpc/isp"
|
||||||
"github.com/txix-open/isp-kit/json"
|
"github.com/txix-open/isp-kit/json"
|
||||||
"github.com/txix-open/isp-kit/requestid"
|
"github.com/txix-open/isp-kit/requestid"
|
||||||
grpc2 "google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
@@ -46,12 +45,6 @@ var (
|
|||||||
inverseCodeMap = map[codes.Code]int{}
|
inverseCodeMap = map[codes.Code]int{}
|
||||||
)
|
)
|
||||||
|
|
||||||
type grpcResponse struct {
|
|
||||||
StatusCode int
|
|
||||||
Body []byte
|
|
||||||
Headers metadata.MD
|
|
||||||
}
|
|
||||||
|
|
||||||
type Grpc struct {
|
type Grpc struct {
|
||||||
cli *client.Client
|
cli *client.Client
|
||||||
}
|
}
|
||||||
@@ -88,120 +81,72 @@ func (p Grpc) Handle(ctx *domain.RequestContext) error {
|
|||||||
|
|
||||||
requestContext := metadata.NewOutgoingContext(ctx.Context(), md)
|
requestContext := metadata.NewOutgoingContext(ctx.Context(), md)
|
||||||
|
|
||||||
var header metadata.MD
|
|
||||||
result, err := p.cli.BackendClient().Request(requestContext, &isp.Message{
|
result, err := p.cli.BackendClient().Request(requestContext, &isp.Message{
|
||||||
Body: &isp.Message_BytesBody{BytesBody: body},
|
Body: &isp.Message_BytesBody{BytesBody: body},
|
||||||
}, grpc2.Header(&header))
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp, err := p.grpcError(err, ctx.Endpoint(), header)
|
return p.writeGrpcError(err, ctx.ResponseWriter(), ctx.Endpoint(), requestId)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return p.writeResponse(resp, requestId, ctx.ResponseWriter())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := grpcResponse{
|
return p.writeResponse(http.StatusOK, result.GetBytesBody(), requestId, ctx.ResponseWriter())
|
||||||
StatusCode: http.StatusOK,
|
|
||||||
Body: result.GetBytesBody(),
|
|
||||||
Headers: header,
|
|
||||||
}
|
|
||||||
return p.writeResponse(resp, requestId, ctx.ResponseWriter())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Grpc) Close() error {
|
func (p Grpc) Close() error {
|
||||||
return p.cli.Close()
|
return p.cli.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Grpc) grpcError(
|
func (p Grpc) writeGrpcError(err error, w http.ResponseWriter, endpoint string, requestId string) error {
|
||||||
err error,
|
status, ok := status.FromError(err)
|
||||||
endpoint string,
|
|
||||||
headers metadata.MD,
|
|
||||||
) (grpcResponse, error) {
|
|
||||||
st, ok := status.FromError(err)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return grpcResponse{}, domain.NewHttpError(
|
return domain.NewHttpError(
|
||||||
http.StatusServiceUnavailable,
|
http.StatusServiceUnavailable,
|
||||||
"upstream is not available",
|
"upstream is not available",
|
||||||
errors.WithMessage(err, "grpc proxy"),
|
errors.WithMessage(err, "grpc proxy"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
statusCode := p.codeToHttpStatus(st.Code())
|
statusCode := p.codeToHttpStatus(status.Code())
|
||||||
for _, detail := range st.Details() {
|
for _, detail := range status.Details() {
|
||||||
msg, ok := detail.(*isp.Message)
|
switch typeOfDetail := detail.(type) {
|
||||||
if !ok {
|
case *isp.Message:
|
||||||
return p.protoResponse(statusCode, detail, headers)
|
switch {
|
||||||
}
|
case typeOfDetail.GetBytesBody() != nil:
|
||||||
|
return p.writeResponse(statusCode, typeOfDetail.GetBytesBody(), requestId, w)
|
||||||
switch {
|
case typeOfDetail.GetListBody() != nil:
|
||||||
case msg.GetBytesBody() != nil:
|
return p.writeProto(statusCode, typeOfDetail.GetListBody(), requestId, w)
|
||||||
return grpcResponse{
|
case typeOfDetail.GetStructBody() != nil:
|
||||||
StatusCode: statusCode,
|
return p.writeProto(statusCode, typeOfDetail.GetStructBody(), requestId, w)
|
||||||
Body: msg.GetBytesBody(),
|
}
|
||||||
Headers: headers,
|
default:
|
||||||
}, nil
|
return p.writeProto(statusCode, typeOfDetail, requestId, w)
|
||||||
|
|
||||||
case msg.GetListBody() != nil:
|
|
||||||
return p.protoResponse(statusCode, msg.GetListBody(), headers)
|
|
||||||
|
|
||||||
case msg.GetStructBody() != nil:
|
|
||||||
return p.protoResponse(statusCode, msg.GetStructBody(), headers)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return grpcResponse{}, domain.NewHttpError(
|
return domain.NewHttpError(
|
||||||
statusCode,
|
statusCode,
|
||||||
st.Message(),
|
status.Message(),
|
||||||
errors.WithMessagef(err, "proxy '%s'", endpoint),
|
errors.WithMessagef(err, "proxy '%s'", endpoint),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Grpc) protoResponse(
|
func (p Grpc) writeProto(statusCode int, proto any, requestId string, w http.ResponseWriter) error {
|
||||||
statusCode int,
|
|
||||||
proto any,
|
|
||||||
headers metadata.MD,
|
|
||||||
) (grpcResponse, error) {
|
|
||||||
data, err := json.Marshal(proto)
|
data, err := json.Marshal(proto)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return grpcResponse{}, errors.WithMessage(err, "marshal grpc details to json")
|
return errors.WithMessage(err, "marshal grpc details to json")
|
||||||
}
|
}
|
||||||
|
return p.writeResponse(statusCode, data, requestId, w)
|
||||||
return grpcResponse{
|
|
||||||
StatusCode: statusCode,
|
|
||||||
Body: data,
|
|
||||||
Headers: headers,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Grpc) writeResponse(
|
func (p Grpc) writeResponse(statusCode int, data []byte, requestId string, w http.ResponseWriter) error {
|
||||||
resp grpcResponse,
|
|
||||||
requestId string,
|
|
||||||
w http.ResponseWriter,
|
|
||||||
) error {
|
|
||||||
for header, values := range resp.Headers {
|
|
||||||
header = grpcHeaderToHttp(header)
|
|
||||||
if header == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, value := range values {
|
|
||||||
w.Header().Add(header, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
|
||||||
if requestId != "" {
|
if requestId != "" {
|
||||||
w.Header().Set(requestid.Header, requestId)
|
w.Header().Set(requestid.Header, requestId)
|
||||||
}
|
}
|
||||||
|
w.WriteHeader(statusCode)
|
||||||
w.WriteHeader(resp.StatusCode)
|
_, err := w.Write(data)
|
||||||
_, err := w.Write(resp.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "response write")
|
return errors.WithMessage(err, "response write")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,32 +162,10 @@ func (p Grpc) codeToHttpStatus(code codes.Code) int {
|
|||||||
func httpHeadersToGrpcMetadata(h http.Header) metadata.MD {
|
func httpHeadersToGrpcMetadata(h http.Header) metadata.MD {
|
||||||
md := metadata.MD{}
|
md := metadata.MD{}
|
||||||
for header, values := range h {
|
for header, values := range h {
|
||||||
header := httpHeaderToGrpc(header)
|
header = strings.ToLower(header)
|
||||||
if strings.HasPrefix(header, "x-") {
|
if strings.HasPrefix(header, "x-") {
|
||||||
md.Set(header, values...)
|
md.Set(header, values...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return md
|
return md
|
||||||
}
|
}
|
||||||
|
|
||||||
func grpcHeaderToHttp(grpcHeader string) string {
|
|
||||||
switch grpcHeader {
|
|
||||||
case grpc.ContentEncodingHeader:
|
|
||||||
return "Content-Encoding"
|
|
||||||
case grpc.AcceptEncodingHeader:
|
|
||||||
return "Accept-Encoding"
|
|
||||||
default:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func httpHeaderToGrpc(httpHeader string) string {
|
|
||||||
switch httpHeader {
|
|
||||||
case "Content-Encoding":
|
|
||||||
return grpc.ContentEncodingHeader
|
|
||||||
case "Accept-Encoding":
|
|
||||||
return grpc.AcceptEncodingHeader
|
|
||||||
default:
|
|
||||||
return strings.ToLower(httpHeader)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user