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