Merge branch 'dev' into 'main'
Dev See merge request isp/isp-router-service!4
This commit is contained in:
commit
a647860a40
3 files changed
+109
-30
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.2
|
||||||
* обновлены зависимости
|
* обновлены зависимости
|
||||||
### v1.1.1
|
### v1.1.1
|
||||||
|
|||||||
+106
-29
@@ -13,6 +13,7 @@ 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"
|
||||||
@@ -45,6 +46,12 @@ 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
|
||||||
}
|
}
|
||||||
@@ -81,72 +88,120 @@ 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 {
|
||||||
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 {
|
func (p Grpc) Close() error {
|
||||||
return p.cli.Close()
|
return p.cli.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Grpc) writeGrpcError(err error, w http.ResponseWriter, endpoint string, requestId string) error {
|
func (p Grpc) grpcError(
|
||||||
status, ok := status.FromError(err)
|
err error,
|
||||||
|
endpoint string,
|
||||||
|
headers metadata.MD,
|
||||||
|
) (grpcResponse, error) {
|
||||||
|
st, ok := status.FromError(err)
|
||||||
if !ok {
|
if !ok {
|
||||||
return domain.NewHttpError(
|
return grpcResponse{}, 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(status.Code())
|
statusCode := p.codeToHttpStatus(st.Code())
|
||||||
for _, detail := range status.Details() {
|
for _, detail := range st.Details() {
|
||||||
switch typeOfDetail := detail.(type) {
|
msg, ok := detail.(*isp.Message)
|
||||||
case *isp.Message:
|
if !ok {
|
||||||
switch {
|
return p.protoResponse(statusCode, detail, headers)
|
||||||
case typeOfDetail.GetBytesBody() != nil:
|
}
|
||||||
return p.writeResponse(statusCode, typeOfDetail.GetBytesBody(), requestId, w)
|
|
||||||
case typeOfDetail.GetListBody() != nil:
|
switch {
|
||||||
return p.writeProto(statusCode, typeOfDetail.GetListBody(), requestId, w)
|
case msg.GetBytesBody() != nil:
|
||||||
case typeOfDetail.GetStructBody() != nil:
|
return grpcResponse{
|
||||||
return p.writeProto(statusCode, typeOfDetail.GetStructBody(), requestId, w)
|
StatusCode: statusCode,
|
||||||
}
|
Body: msg.GetBytesBody(),
|
||||||
default:
|
Headers: headers,
|
||||||
return p.writeProto(statusCode, typeOfDetail, requestId, w)
|
}, 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,
|
statusCode,
|
||||||
status.Message(),
|
st.Message(),
|
||||||
errors.WithMessagef(err, "proxy '%s'", endpoint),
|
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)
|
data, err := json.Marshal(proto)
|
||||||
if err != nil {
|
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")
|
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)
|
|
||||||
_, err := w.Write(data)
|
w.WriteHeader(resp.StatusCode)
|
||||||
|
_, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,10 +217,32 @@ 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 = strings.ToLower(header)
|
header := httpHeaderToGrpc(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