审查视图

cmd/discuss/interanl/pkg/gateway/base_gateway.go 3.3 KB
yangfu authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package gateway

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/zeromicro/go-zero/core/mapping"
	"github.com/zeromicro/go-zero/rest/httpc"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

type Service struct {
	Timeout     time.Duration
yangfu authored
17
	Host        string
yangfu authored
18 19 20 21 22 23 24 25 26 27
	Interceptor func(msg string)
	ServiceName string
	service     httpc.Service
}

func NewService(name string, host string, timeout time.Duration, opts ...httpc.Option) Service {
	client := &http.Client{}
	//client.Timeout = timeout

	service := Service{
yangfu authored
28
		Host:    host,
yangfu authored
29 30 31 32 33 34 35 36 37 38 39
		service: httpc.NewServiceWithClient(name, client, opts...),
	}
	return service
}

func (gateway Service) Do(ctx context.Context, url string, method string, val interface{}, result interface{}) error {
	var (
		baseResponse = Response{}
		begin        = time.Now()
		body         []byte
	)
yangfu authored
40
	response, err := gateway.service.Do(ctx, method, gateway.Host+url, val)
yangfu authored
41 42 43 44 45 46 47
	defer func() {
		jsonParam, _ := json.Marshal(val)
		jsonData, _ := json.Marshal(result)
		if err != nil {
			result = err.Error()
		}
		if gateway.Interceptor != nil {
yangfu authored
48
			gateway.Interceptor(fmt.Sprintf("【网关】%v | %v%v | %v : %v \n-->> %v \n<<-- %v", time.Since(begin), gateway.Host, url, strings.ToUpper(method),
yangfu authored
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
				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
}
yangfu authored
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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
}
yangfu authored
143 144 145 146 147 148 149 150 151 152
func Bytes(resp *http.Response) ([]byte, error) {
	var body []byte
	if resp.Body == nil {
		return nil, nil
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	return body, err
}