...
|
...
|
@@ -14,7 +14,7 @@ import ( |
|
|
|
|
|
type Service struct {
|
|
|
Timeout time.Duration
|
|
|
host string
|
|
|
Host string
|
|
|
Interceptor func(msg string)
|
|
|
ServiceName string
|
|
|
service httpc.Service
|
...
|
...
|
@@ -25,7 +25,7 @@ func NewService(name string, host string, timeout time.Duration, opts ...httpc.O |
|
|
//client.Timeout = timeout
|
|
|
|
|
|
service := Service{
|
|
|
host: host,
|
|
|
Host: host,
|
|
|
service: httpc.NewServiceWithClient(name, client, opts...),
|
|
|
}
|
|
|
return service
|
...
|
...
|
@@ -37,7 +37,7 @@ func (gateway Service) Do(ctx context.Context, url string, method string, val in |
|
|
begin = time.Now()
|
|
|
body []byte
|
|
|
)
|
|
|
response, err := gateway.service.Do(ctx, method, gateway.host+url, val)
|
|
|
response, err := gateway.service.Do(ctx, method, gateway.Host+url, val)
|
|
|
defer func() {
|
|
|
jsonParam, _ := json.Marshal(val)
|
|
|
jsonData, _ := json.Marshal(result)
|
...
|
...
|
@@ -45,7 +45,7 @@ func (gateway Service) Do(ctx context.Context, url string, method string, val in |
|
|
result = err.Error()
|
|
|
}
|
|
|
if gateway.Interceptor != nil {
|
|
|
gateway.Interceptor(fmt.Sprintf("【网关】%v | %v%v | %v : %v \n-->> %v \n<<-- %v", time.Since(begin), gateway.host, url, strings.ToUpper(method),
|
|
|
gateway.Interceptor(fmt.Sprintf("【网关】%v | %v%v | %v : %v \n-->> %v \n<<-- %v", time.Since(begin), gateway.Host, url, strings.ToUpper(method),
|
|
|
result,
|
|
|
string(jsonParam),
|
|
|
string(jsonData),
|
...
|
...
|
@@ -84,6 +84,62 @@ func (gateway Service) Do(ctx context.Context, url string, method string, val in |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (gateway Service) HandlerResponse(ctx context.Context, request *http.Request, response *http.Response, val, result interface{}) error {
|
|
|
var (
|
|
|
baseResponse = Response{}
|
|
|
begin = time.Now()
|
|
|
body []byte
|
|
|
err error
|
|
|
)
|
|
|
defer func() {
|
|
|
jsonParam, _ := json.Marshal(val)
|
|
|
jsonData, _ := json.Marshal(result)
|
|
|
if err != nil {
|
|
|
result = err.Error()
|
|
|
}
|
|
|
if gateway.Interceptor != nil {
|
|
|
gateway.Interceptor(fmt.Sprintf("【网关】%v | %v%v | %v : %v \n-->> %v \n<<-- %v", time.Since(begin), gateway.Host, request.URL.Path, request.Method,
|
|
|
result,
|
|
|
string(jsonParam),
|
|
|
string(jsonData),
|
|
|
))
|
|
|
}
|
|
|
}()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
return HttpError{
|
|
|
Base: Response{
|
|
|
Code: response.StatusCode,
|
|
|
Msg: response.Status,
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
body, err = Bytes(response)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if err = json.Unmarshal(body, &baseResponse); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if baseResponse.Code != 0 {
|
|
|
return HttpError{
|
|
|
Base: Response{
|
|
|
Code: baseResponse.Code,
|
|
|
Msg: baseResponse.Msg,
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
if err = mapping.UnmarshalJsonBytes(baseResponse.Data, result); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (gateway Service) GetService() httpc.Service {
|
|
|
return gateway.service
|
|
|
}
|
|
|
func Bytes(resp *http.Response) ([]byte, error) {
|
|
|
var body []byte
|
|
|
if resp.Body == nil {
|
...
|
...
|
|