up(proxy response)
This commit is contained in:
1 parent
1a46b678ef
commit
f802b2000d
11 files changed
+108
-159
No files matched your search
@@ -12,7 +12,7 @@ import (
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/grpc/handlers"
|
||||
"isp-gate-service/proxy/response"
|
||||
"isp-gate-service/utils"
|
||||
)
|
||||
|
||||
type grpcProxy struct {
|
||||
@@ -36,7 +36,11 @@ func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
|
||||
if p.client.InternalGrpcClient == nil {
|
||||
msg := "client undefined"
|
||||
log.Error(log_code.ErrorClientGrpc, msg)
|
||||
return response.Create(ctx, response.Option.SetAndSendError(msg, codes.Internal, errors.New(msg)))
|
||||
utils.WriteError(ctx, msg, codes.Internal, nil)
|
||||
return domain.Create().
|
||||
SetRequestBody(ctx.Request.Body()).
|
||||
SetResponseBody(ctx.Response.Body()).
|
||||
SetError(errors.New(msg))
|
||||
}
|
||||
|
||||
uri := string(ctx.RequestURI())
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/response"
|
||||
utils2 "isp-gate-service/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -149,7 +149,8 @@ func checkError(err error, ctx *fasthttp.RequestCtx) (bool, bool, domain.ProxyRe
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
logHandlerError(log_code.TypeData.GetFile, "", err)
|
||||
resp = response.Create(ctx, response.Option.SetAndSendError(errorMsgInternal, codes.Internal, err))
|
||||
utils2.WriteError(ctx, errorMsgInternal, codes.Internal, nil)
|
||||
resp = domain.Create().SetError(err)
|
||||
ok, eof = false, false
|
||||
} else {
|
||||
ok, eof = true, true
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
u "github.com/integration-system/isp-lib/utils"
|
||||
"github.com/valyala/fasthttp"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/proxy/response"
|
||||
"isp-gate-service/utils"
|
||||
"mime"
|
||||
)
|
||||
|
||||
@@ -24,12 +24,12 @@ func (h handlerHelper) Get(ctx *fasthttp.RequestCtx) handler {
|
||||
isExpectFile := string(ctx.Request.Header.Peek(u.ExpectFileHeader)) == "true"
|
||||
|
||||
if isMultipart {
|
||||
ctx.Response.Header.SetContentType(response.JsonContentType)
|
||||
ctx.Response.Header.SetContentType(utils.JsonContentType)
|
||||
return sendMultipartData
|
||||
} else if isExpectFile {
|
||||
return getFile
|
||||
} else {
|
||||
ctx.Response.Header.SetContentType(response.JsonContentType)
|
||||
ctx.Response.Header.SetContentType(utils.JsonContentType)
|
||||
return handleJson
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/response"
|
||||
"isp-gate-service/utils"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -29,12 +29,8 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
|
||||
req, err := g.readJsonBody(ctx)
|
||||
if err != nil {
|
||||
logHandlerError(log_code.TypeData.GetFile, method, err)
|
||||
return response.Create(
|
||||
ctx,
|
||||
response.Option.SetAndSendError(err.Error(), codes.InvalidArgument, err),
|
||||
response.Option.EmptyRequest(),
|
||||
response.Option.EmptyResponse(),
|
||||
)
|
||||
utils.WriteError(ctx, err.Error(), codes.InvalidArgument, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
stream, cancel, err := openStream(&ctx.Request.Header, method, timeout, client)
|
||||
@@ -45,12 +41,8 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
|
||||
}()
|
||||
if err != nil {
|
||||
logHandlerError(log_code.TypeData.GetFile, method, err)
|
||||
return response.Create(
|
||||
ctx,
|
||||
response.Option.SetAndSendError(errorMsgInternal, codes.Internal, err),
|
||||
response.Option.EmptyRequest(),
|
||||
response.Option.EmptyResponse(),
|
||||
)
|
||||
utils.WriteError(ctx, errorMsgInternal, codes.Internal, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
if req != nil {
|
||||
@@ -58,12 +50,8 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
|
||||
err := stream.Send(backend.WrapBody(value))
|
||||
if err != nil {
|
||||
logHandlerError(log_code.TypeData.GetFile, method, err)
|
||||
return response.Create(
|
||||
ctx,
|
||||
response.Option.SetAndSendError(errorMsgInternal, codes.Internal, err),
|
||||
response.Option.EmptyRequest(),
|
||||
response.Option.EmptyResponse(),
|
||||
)
|
||||
utils.WriteError(ctx, errorMsgInternal, codes.Internal, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +62,7 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
|
||||
ctx.SetStatusCode(status)
|
||||
ctx.SetBody(bytes)
|
||||
}
|
||||
return response.Create(ctx, response.Option.EmptyResponse(), response.Option.EmptyRequest())
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
bf := s.BeginFile{}
|
||||
err = bf.FromMessage(msg)
|
||||
@@ -84,7 +72,7 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
|
||||
ctx.SetStatusCode(status)
|
||||
ctx.SetBody(bytes)
|
||||
}
|
||||
return response.Create(ctx, response.Option.EmptyResponse(), response.Option.EmptyRequest())
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
header := &ctx.Response.Header
|
||||
header.Set(headerKeyContentDisposition, fmt.Sprintf("attachment; filename=%s", bf.FileName))
|
||||
@@ -118,7 +106,7 @@ func (g getFileDesc) Complete(ctx *fasthttp.RequestCtx, method string, client *b
|
||||
break
|
||||
}
|
||||
}
|
||||
return response.Create(ctx, response.Option.EmptyResponse(), response.Option.EmptyRequest())
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
func (getFileDesc) readJsonBody(ctx *fasthttp.RequestCtx) (interface{}, error) {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/response"
|
||||
"isp-gate-service/utils"
|
||||
)
|
||||
|
||||
var handleJson handleJsonDesc
|
||||
@@ -36,12 +36,8 @@ func (p handleJsonDesc) Complete(c *fasthttp.RequestCtx, method string, client *
|
||||
cli, err := client.Conn()
|
||||
if err != nil {
|
||||
logHandlerError(log_code.TypeData.JsonContent, methodName, err)
|
||||
return response.Create(
|
||||
c,
|
||||
response.Option.SetAndSendError(errorMsgInternal, codes.Internal, err),
|
||||
response.Option.EmptyRequest(),
|
||||
response.Option.EmptyResponse(),
|
||||
)
|
||||
utils.WriteError(c, errorMsgInternal, codes.Internal, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
//structBody := u.ConvertInterfaceToGrpcStruct(body)
|
||||
@@ -55,14 +51,10 @@ func (p handleJsonDesc) Complete(c *fasthttp.RequestCtx, method string, client *
|
||||
if data, status, err := getResponse(resp, invokerErr); err == nil {
|
||||
c.SetStatusCode(status)
|
||||
_, _ = c.Write(data)
|
||||
return response.Create(c, response.Option.SetError(invokerErr))
|
||||
return domain.Create().SetError(invokerErr)
|
||||
} else {
|
||||
logHandlerError(log_code.TypeData.JsonContent, methodName, err)
|
||||
return response.Create(
|
||||
c,
|
||||
response.Option.SetAndSendError(errorMsgInternal, codes.Internal, err),
|
||||
response.Option.EmptyRequest(),
|
||||
response.Option.EmptyResponse(),
|
||||
)
|
||||
utils.WriteError(c, errorMsgInternal, codes.Internal, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/response"
|
||||
"isp-gate-service/utils"
|
||||
"mime/multipart"
|
||||
"strings"
|
||||
)
|
||||
@@ -32,12 +32,8 @@ func (h sendMultipartDataDesc) Complete(ctx *fasthttp.RequestCtx, method string,
|
||||
}()
|
||||
if err != nil {
|
||||
logHandlerError(log_code.TypeData.SendMultipart, method, err)
|
||||
return response.Create(
|
||||
ctx,
|
||||
response.Option.SetAndSendError(errorMsgInternal, codes.Internal, err),
|
||||
response.Option.EmptyRequest(),
|
||||
response.Option.EmptyResponse(),
|
||||
)
|
||||
utils.WriteError(ctx, errorMsgInternal, codes.Internal, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
form, err := ctx.MultipartForm()
|
||||
@@ -49,12 +45,8 @@ func (h sendMultipartDataDesc) Complete(ctx *fasthttp.RequestCtx, method string,
|
||||
|
||||
if err != nil {
|
||||
logHandlerError(log_code.TypeData.SendMultipart, method, err)
|
||||
return response.Create(
|
||||
ctx,
|
||||
response.Option.SetAndSendError(errorMsgInvalidArg, codes.InvalidArgument, err),
|
||||
response.Option.EmptyRequest(),
|
||||
response.Option.EmptyResponse(),
|
||||
)
|
||||
utils.WriteError(ctx, errorMsgInvalidArg, codes.InvalidArgument, nil)
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
formData := make(map[string]interface{}, len(form.Value))
|
||||
|
||||
+12
-4
@@ -8,7 +8,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/proxy/response"
|
||||
"isp-gate-service/utils"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
@@ -37,7 +37,11 @@ func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
|
||||
if p.client == nil {
|
||||
msg := "client undefined"
|
||||
log.Error(log_code.ErrorClientHttp, msg)
|
||||
return response.Create(ctx, response.Option.SetAndSendError(msg, codes.Internal, errors.New(msg)))
|
||||
utils.WriteError(ctx, msg, codes.Internal, nil)
|
||||
return domain.Create().
|
||||
SetRequestBody(ctx.Request.Body()).
|
||||
SetResponseBody(ctx.Response.Body()).
|
||||
SetError(errors.New(msg))
|
||||
}
|
||||
|
||||
req := &ctx.Request
|
||||
@@ -47,10 +51,14 @@ func (p *httpProxy) ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
|
||||
req.Header.Add("X-Forwarded-For", addr)
|
||||
}
|
||||
|
||||
if err := p.client.Do(req, res); err != nil {
|
||||
err := p.client.Do(req, res)
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorClientHttp, err)
|
||||
}
|
||||
return response.Create(ctx)
|
||||
return domain.Create().
|
||||
SetRequestBody(ctx.Request.Body()).
|
||||
SetResponseBody(ctx.Response.Body()).
|
||||
SetError(err)
|
||||
}
|
||||
|
||||
func (p *httpProxy) Close() {
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/integration-system/isp-lib/http"
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc/codes"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
const JsonContentType = "application/json; charset=utf-8"
|
||||
|
||||
var Option optionStruct
|
||||
|
||||
type (
|
||||
optionStruct struct{}
|
||||
option func(ctx *fasthttp.RequestCtx, response *domain.ProxyResponse)
|
||||
)
|
||||
|
||||
func Create(ctx *fasthttp.RequestCtx, options ...option) domain.ProxyResponse {
|
||||
response := domain.ProxyResponse{
|
||||
RequestBody: ctx.Request.Body(),
|
||||
ResponseBody: ctx.Response.Body(),
|
||||
}
|
||||
for _, option := range options {
|
||||
option(ctx, &response)
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
func (optionStruct) EmptyRequest() option {
|
||||
return func(ctx *fasthttp.RequestCtx, response *domain.ProxyResponse) {
|
||||
response.RequestBody = make([]byte, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func (optionStruct) EmptyResponse() option {
|
||||
return func(ctx *fasthttp.RequestCtx, response *domain.ProxyResponse) {
|
||||
response.ResponseBody = make([]byte, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func (optionStruct) SetError(err error) option {
|
||||
return func(ctx *fasthttp.RequestCtx, response *domain.ProxyResponse) {
|
||||
response.Error = err
|
||||
}
|
||||
}
|
||||
|
||||
func (optionStruct) SetAndSendError(msg string, code codes.Code, err error) option {
|
||||
return func(ctx *fasthttp.RequestCtx, response *domain.ProxyResponse) {
|
||||
response.Error = err
|
||||
|
||||
grpcCode := code.String()
|
||||
|
||||
structureError := structure.GrpcError{
|
||||
ErrorMessage: msg,
|
||||
ErrorCode: grpcCode,
|
||||
Details: []interface{}{err.Error()},
|
||||
}
|
||||
|
||||
ctx.SetContentType(JsonContentType)
|
||||
ctx.SetStatusCode(http.CodeToHttpStatus(code))
|
||||
msg, _ := json.Marshal(structureError)
|
||||
_, _ = ctx.Write(msg)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user