migrate to go mod

This commit is contained in:
DeniskaRediska committed 2020-03-06 12:14:52 +03:00
1 parent 665641c88b
commit 72e6bbe6d7
45 files changed
+784 -1029

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
package proxy
import (
"github.com/integration-system/isp-lib/structure"
"github.com/integration-system/isp-lib/v2/structure"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"isp-gate-service/domain"
+2 -2
View File
@@ -1,8 +1,8 @@
package grpc
import (
"github.com/integration-system/isp-lib/backend"
"github.com/integration-system/isp-lib/structure"
"github.com/integration-system/isp-lib/v2/backend"
"github.com/integration-system/isp-lib/v2/structure"
log "github.com/integration-system/isp-log"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
+40 -45
View File
@@ -8,12 +8,12 @@ import (
"github.com/golang/protobuf/proto"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/integration-system/isp-lib/backend"
"github.com/integration-system/isp-lib/config"
http2 "github.com/integration-system/isp-lib/http"
isp "github.com/integration-system/isp-lib/proto/stubs"
"github.com/integration-system/isp-lib/structure"
"github.com/integration-system/isp-lib/utils"
"github.com/integration-system/isp-lib/v2/backend"
"github.com/integration-system/isp-lib/v2/config"
http2 "github.com/integration-system/isp-lib/v2/http"
isp "github.com/integration-system/isp-lib/v2/proto/stubs"
"github.com/integration-system/isp-lib/v2/structure"
"github.com/integration-system/isp-lib/v2/utils"
log "github.com/integration-system/isp-log"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
@@ -37,55 +37,52 @@ const (
errorMsgInvalidArg = "Not able to read request body"
)
var (
json = jsoniter.ConfigFastest
emptyBody = make([]byte, 0)
)
var json = jsoniter.ConfigFastest
func convertError(err error) ([]byte, int) {
s, ok := status.FromError(err)
if ok {
cfg := config.GetRemote().(*conf.RemoteConfig).GrpcSetting
if cfg.EnableOriginalProtoErrors {
if body, err := proto.Marshal(s.Proto()); err != nil {
body, err := proto.Marshal(s.Proto())
if err != nil {
return []byte(utils.ServiceError), http.StatusServiceUnavailable
} else {
return body, http2.CodeToHttpStatus(s.Code())
}
} else {
details := s.Details()
newDetails := make([]interface{}, len(details))
for i, detail := range details {
switch typeOfDetail := detail.(type) {
case *structpb.Struct:
newDetails[i] = utils.ConvertGrpcStructToInterface(
&structpb.Value{Kind: &structpb.Value_StructValue{StructValue: typeOfDetail}},
)
case *isp.Message:
newDetails[i] = utils.ConvertGrpcStructToInterface(
backend.ResolveBody(typeOfDetail),
)
default:
newDetails[i] = typeOfDetail
}
}
return body, http2.CodeToHttpStatus(s.Code())
}
var respBody interface{}
if cfg.ProxyGrpcErrorDetails && len(newDetails) > 0 {
respBody = newDetails[0]
} else {
respBody = structure.GrpcError{ErrorMessage: s.Message(), ErrorCode: s.Code().String(), Details: newDetails}
}
if errorData, err := json.Marshal(respBody); err != nil {
log.Warn(log_code.WarnConvertErrorDataMarshalResponse, err)
return []byte(utils.ServiceError), http.StatusServiceUnavailable
} else {
return errorData, http2.CodeToHttpStatus(s.Code())
details := s.Details()
newDetails := make([]interface{}, len(details))
for i, detail := range details {
switch typeOfDetail := detail.(type) {
case *structpb.Struct:
newDetails[i] = utils.ConvertGrpcStructToInterface(
&structpb.Value{Kind: &structpb.Value_StructValue{StructValue: typeOfDetail}},
)
case *isp.Message:
newDetails[i] = utils.ConvertGrpcStructToInterface(
backend.ResolveBody(typeOfDetail),
)
default:
newDetails[i] = typeOfDetail
}
}
} else {
return []byte(utils.ServiceError), http.StatusServiceUnavailable
var respBody interface{}
if cfg.ProxyGrpcErrorDetails && len(newDetails) > 0 {
respBody = newDetails[0]
} else {
respBody = structure.GrpcError{ErrorMessage: s.Message(), ErrorCode: s.Code().String(), Details: newDetails}
}
errorData, err := json.Marshal(respBody)
if err != nil {
log.Warn(log_code.WarnConvertErrorDataMarshalResponse, err)
return []byte(utils.ServiceError), http.StatusServiceUnavailable
}
return errorData, http2.CodeToHttpStatus(s.Code())
}
return []byte(utils.ServiceError), http.StatusServiceUnavailable
}
func getResponse(msg *isp.Message, err error) ([]byte, int, error) {
@@ -113,7 +110,6 @@ func logHandlerError(typeData, method string, err error) {
func openStream(headers *fasthttp.RequestHeader, method string, timeout time.Duration, client *backend.RxGrpcClient) (
isp.BackendService_RequestStreamClient, context.CancelFunc, error) {
cli, err := client.Conn()
if err != nil {
return nil, nil, err
@@ -129,7 +125,6 @@ func openStream(headers *fasthttp.RequestHeader, method string, timeout time.Dur
}
func makeMetadata(r *fasthttp.RequestHeader, method string) (metadata.MD, string) {
//method = strings.TrimPrefix(method, "/api/")
md := make(metadata.MD, 5)
md[utils.ProxyMethodNameHeader] = []string{method}
r.VisitAll(func(key, v []byte) {
+8 -11
View File
@@ -1,8 +1,8 @@
package handlers
import (
"github.com/integration-system/isp-lib/backend"
u "github.com/integration-system/isp-lib/utils"
"github.com/integration-system/isp-lib/v2/backend"
u "github.com/integration-system/isp-lib/v2/utils"
"github.com/valyala/fasthttp"
"isp-gate-service/domain"
"isp-gate-service/utils"
@@ -26,12 +26,12 @@ func (h handlerHelper) Get(ctx *fasthttp.RequestCtx) handler {
if isMultipart {
ctx.Response.Header.SetContentType(utils.JsonContentType)
return sendMultipartData
} else if isExpectFile {
return getFile
} else {
ctx.Response.Header.SetContentType(utils.JsonContentType)
return handleJson
}
if isExpectFile {
return getFile
}
ctx.Response.Header.SetContentType(utils.JsonContentType)
return handleJson
}
func (h handlerHelper) isMultipart(ctx *fasthttp.RequestCtx) bool {
@@ -47,8 +47,5 @@ func (h handlerHelper) isMultipart(ctx *fasthttp.RequestCtx) bool {
return false
}
_, ok := params["boundary"]
if !ok {
return false
}
return true
return ok
}
+30 -26
View File
@@ -2,10 +2,11 @@ package handlers
import (
"fmt"
"github.com/integration-system/isp-lib/backend"
"github.com/integration-system/isp-lib/config"
s "github.com/integration-system/isp-lib/streaming"
u "github.com/integration-system/isp-lib/utils"
"github.com/integration-system/isp-lib/v2/backend"
"github.com/integration-system/isp-lib/v2/config"
isp "github.com/integration-system/isp-lib/v2/proto/stubs"
s "github.com/integration-system/isp-lib/v2/streaming"
u "github.com/integration-system/isp-lib/v2/utils"
log "github.com/integration-system/isp-log"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
@@ -23,16 +24,12 @@ var getFile getFileDesc
type getFileDesc struct{}
func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *backend.RxGrpcClient) domain.ProxyResponse {
cfg := config.GetRemote().(*conf.RemoteConfig).GrpcSetting
timeout := cfg.GetStreamInvokeTimeout()
req, err := g.readJsonBody(ctx)
if err != nil {
logHandlerError(log_code.TypeData.GetFile, method, err)
utils.WriteError(ctx, err.Error(), codes.InvalidArgument, nil)
return domain.Create().SetError(err)
return g.formError(ctx, err, method, err.Error(), codes.InvalidArgument)
}
timeout := config.GetRemote().(*conf.RemoteConfig).GrpcSetting.GetStreamInvokeTimeout()
stream, cancel, err := openStream(&ctx.Request.Header, method, timeout, client)
defer func() {
if cancel != nil {
@@ -40,18 +37,14 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
}
}()
if err != nil {
logHandlerError(log_code.TypeData.GetFile, method, err)
utils.WriteError(ctx, errorMsgInternal, codes.Internal, nil)
return domain.Create().SetError(err)
return g.formError(ctx, err, method, errorMsgInternal, codes.Internal)
}
if req != nil {
value := u.ConvertInterfaceToGrpcStruct(req)
err := stream.Send(backend.WrapBody(value))
if err != nil {
logHandlerError(log_code.TypeData.GetFile, method, err)
utils.WriteError(ctx, errorMsgInternal, codes.Internal, nil)
return domain.Create().SetError(err)
return g.formError(ctx, err, method, errorMsgInternal, codes.Internal)
}
}
@@ -64,6 +57,7 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
}
return domain.Create().SetError(err)
}
bf := s.BeginFile{}
err = bf.FromMessage(msg)
if err != nil {
@@ -83,14 +77,20 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
header.Set(headerKeyTransferEncoding, "chunked")
}
err = g.write(ctx, stream, method)
return domain.Create().SetError(err)
}
//nolint
func (getFileDesc) write(ctx *fasthttp.RequestCtx, stream isp.BackendService_RequestStreamClient, method string) error {
for {
msg, err := stream.Recv()
if s.IsEndOfFile(msg) || err == io.EOF {
break
return nil
}
if err != nil {
logHandlerError(log_code.TypeData.GetFile, method, err)
break
return err
}
bytes := msg.GetBytesBody()
if bytes == nil {
@@ -98,15 +98,14 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
log_code.MdTypeData: log_code.TypeData.GetFile,
log_code.MdMethod: method,
}).Errorf(log_code.WarnProxyGrpcHandler, "Method %s. Expected bytes array", method)
break
return nil
}
_, err = ctx.Write(bytes)
if err != nil {
logHandlerError(log_code.TypeData.GetFile, method, err)
break
return err
}
}
return domain.Create().SetError(err)
}
func (getFileDesc) readJsonBody(ctx *fasthttp.RequestCtx) (interface{}, error) {
@@ -115,18 +114,23 @@ func (getFileDesc) readJsonBody(ctx *fasthttp.RequestCtx) (interface{}, error) {
if len(requestBody) == 0 {
requestBody = []byte("{}")
}
if requestBody[0] == '{' {
switch requestBody[0] {
case '{':
body = make(map[string]interface{})
} else if requestBody[0] == '[' {
case '[':
body = make([]interface{}, 0)
} else {
default:
return nil, errors.New("Invalid json format. Expected object or array")
}
err := json.Unmarshal(requestBody, &body)
if err != nil {
return nil, errors.New("Not able to read request body")
}
return body, err
}
func (getFileDesc) formError(ctx *fasthttp.RequestCtx, err error, method, msg string, code codes.Code) domain.ProxyResponse {
logHandlerError(log_code.TypeData.GetFile, method, err)
utils.WriteError(ctx, msg, code, nil)
return domain.Create().SetError(err)
}
+3 -3
View File
@@ -1,9 +1,9 @@
package handlers
import (
"github.com/integration-system/isp-lib/backend"
"github.com/integration-system/isp-lib/config"
isp "github.com/integration-system/isp-lib/proto/stubs"
"github.com/integration-system/isp-lib/v2/backend"
"github.com/integration-system/isp-lib/v2/config"
isp "github.com/integration-system/isp-lib/v2/proto/stubs"
"github.com/valyala/fasthttp"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
@@ -1,10 +1,10 @@
package handlers
import (
"github.com/integration-system/isp-lib/backend"
"github.com/integration-system/isp-lib/config"
isp "github.com/integration-system/isp-lib/proto/stubs"
s "github.com/integration-system/isp-lib/streaming"
"github.com/integration-system/isp-lib/v2/backend"
"github.com/integration-system/isp-lib/v2/config"
isp "github.com/integration-system/isp-lib/v2/proto/stubs"
s "github.com/integration-system/isp-lib/v2/streaming"
"github.com/valyala/fasthttp"
"google.golang.org/grpc/codes"
"isp-gate-service/conf"
@@ -22,7 +22,6 @@ type sendMultipartDataDesc struct{}
func (h sendMultipartDataDesc) Complete(c *fasthttp.RequestCtx, method string, client *backend.RxGrpcClient) domain.ProxyResponse {
cfg := config.GetRemote().(*conf.RemoteConfig).GrpcSetting
timeout := cfg.GetStreamInvokeTimeout()
bufferSize := cfg.GetTransferFileBufferSize()
stream, cancel, err := openStream(&c.Request.Header, method, timeout, client)
defer func() {
@@ -49,8 +48,25 @@ func (h sendMultipartDataDesc) Complete(c *fasthttp.RequestCtx, method string, c
return domain.Create().SetError(err)
}
formData := make(map[string]interface{}, len(form.Value))
resp, proxyResp, ok := h.formResponse(c, stream, form, method)
err = stream.CloseSend()
if err != nil {
logHandlerError(log_code.TypeData.SendMultipart, method, err)
}
if ok {
arrayBody := strings.Join(resp, ",")
_, err = c.WriteString("[" + arrayBody + "]")
if err != nil {
logHandlerError(log_code.TypeData.SendMultipart, method, err)
}
}
return proxyResp
}
func (h sendMultipartDataDesc) formResponse(c *fasthttp.RequestCtx, stream isp.BackendService_RequestStreamClient,
form *multipart.Form, method string) ([]string, domain.ProxyResponse, bool) {
formData := make(map[string]interface{}, len(form.Value))
for k, v := range form.Value {
if len(v) > 0 {
formData[k] = v[0]
@@ -58,13 +74,13 @@ func (h sendMultipartDataDesc) Complete(c *fasthttp.RequestCtx, method string, c
}
var (
proxyResp domain.ProxyResponse
resp = make([]string, 0)
buffer = make([]byte, bufferSize)
buffer = make([]byte, config.GetRemote().(*conf.RemoteConfig).GrpcSetting.GetTransferFileBufferSize())
proxyResp domain.ProxyResponse
ok = true
eof = false
eof bool
err error
)
for formDataName, files := range form.File {
if len(files) == 0 {
continue
@@ -110,28 +126,14 @@ func (h sendMultipartDataDesc) Complete(c *fasthttp.RequestCtx, method string, c
}
resp = append(resp, string(response))
}
err = stream.CloseSend()
if err != nil {
logHandlerError(log_code.TypeData.SendMultipart, method, err)
}
if ok {
arrayBody := strings.Join(resp, ",")
_, err = c.WriteString("[" + arrayBody + "]")
if err != nil {
logHandlerError(log_code.TypeData.SendMultipart, method, err)
}
}
return proxyResp
return resp, proxyResp, ok
}
func (sendMultipartDataDesc) transferFile(f multipart.File, stream isp.BackendService_RequestStreamClient,
buffer []byte, ctx *fasthttp.RequestCtx, method string) (bool, bool, domain.ProxyResponse) {
var (
ok = true
eof = false
eof bool
proxyResp domain.ProxyResponse
)
for {
+1 -1
View File
@@ -1,7 +1,7 @@
package health_check
import (
"github.com/integration-system/isp-lib/structure"
"github.com/integration-system/isp-lib/v2/structure"
"github.com/valyala/fasthttp"
"isp-gate-service/domain"
)
+1 -1
View File
@@ -2,7 +2,7 @@ package http
import (
"errors"
"github.com/integration-system/isp-lib/structure"
"github.com/integration-system/isp-lib/v2/structure"
log "github.com/integration-system/isp-log"
"github.com/valyala/fasthttp"
"google.golang.org/grpc/codes"
+1 -1
View File
@@ -1,7 +1,7 @@
package websocket
import (
"github.com/integration-system/isp-lib/structure"
"github.com/integration-system/isp-lib/v2/structure"
"sync"
)
+5 -5
View File
@@ -10,7 +10,7 @@ import (
"sync"
"github.com/fasthttp/websocket"
"github.com/integration-system/isp-lib/structure"
"github.com/integration-system/isp-lib/v2/structure"
log "github.com/integration-system/isp-log"
"github.com/valyala/fasthttp"
"google.golang.org/grpc/codes"
@@ -90,9 +90,7 @@ func (p *websocketProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) dom
outgoingDialer := websocket.Dialer{
ReadBufferSize: readBufSize,
WriteBufferSize: writeBufSize,
NetDial: func(network, address string) (net.Conn, error) {
return net.Dial(network, address)
},
NetDial: net.Dial,
}
addr := addrs.Get()
@@ -114,6 +112,7 @@ func (p *websocketProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) dom
cookies.SetCookies(&url.URL{Host: reqHost, Scheme: reqScheme}, cookie)
outgoingDialer.Jar = cookies
//nolint
outgoingConn, _, err := outgoingDialer.Dial(urlAddr, header)
if err == nil {
go func() {
@@ -123,6 +122,7 @@ func (p *websocketProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) dom
} else {
log.Errorf(log_code.ErrorWebsocketProxy, "unable to connect to service %s: %v", urlAddr, err)
_ = incomingConn.Close()
_ = outgoingConn.Close()
}
})
@@ -149,7 +149,7 @@ func (p *websocketProxy) proxyConn(from, to *websocket.Conn) error {
defer func() {
_ = from.Close()
_ = to.Close()
pool.Put(buf)
pool.Put(&buf)
}()
for {
msgType, reader, err := from.NextReader()