github.com/profzone/eden-framework@v1.0.10/pkg/courier/client/client.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "time" 8 9 "github.com/google/uuid" 10 "github.com/sirupsen/logrus" 11 12 "github.com/profzone/eden-framework/pkg/context" 13 "github.com/profzone/eden-framework/pkg/courier" 14 "github.com/profzone/eden-framework/pkg/courier/httpx" 15 "github.com/profzone/eden-framework/pkg/courier/transport_grpc" 16 "github.com/profzone/eden-framework/pkg/courier/transport_http" 17 "github.com/profzone/eden-framework/pkg/env" 18 ) 19 20 type Client struct { 21 Name string 22 // used in service 23 Service string 24 Version string 25 Host string `conf:"upstream" validate:"@hostname"` 26 Mode string 27 Port int16 28 Timeout time.Duration 29 WrapTransport transport_http.TransportWrapper `json:"-"` 30 } 31 32 func (Client) MarshalDefaults(v interface{}) { 33 if client, ok := v.(*Client); ok { 34 if client.Service == "" { 35 client.Service = os.Getenv("PROJECT_NAME") 36 } 37 if client.Version == "" { 38 client.Version = os.Getenv("PROJECT_REF") 39 } 40 if client.Mode == "" { 41 client.Mode = "http" 42 } 43 if client.Host == "" { 44 client.Host = fmt.Sprintf("service-%s.staging.g7pay.net", client.Name) 45 } 46 if client.Port == 0 { 47 client.Port = 80 48 } 49 if client.Timeout == 0 { 50 client.Timeout = 5 * time.Second 51 } 52 } 53 } 54 55 func (c Client) GetBaseURL(protocol string) (url string) { 56 url = c.Host 57 if protocol != "" { 58 url = fmt.Sprintf("%s://%s", protocol, c.Host) 59 } 60 if c.Port > 0 && c.Port != 80 { 61 url = fmt.Sprintf("%s:%d", url, c.Port) 62 } 63 return 64 } 65 66 func (c *Client) Request(id, httpMethod, uri string, req interface{}, metas ...courier.Metadata) IRequest { 67 requestID := context.GetLogID() 68 metadata := courier.MetadataMerge(metas...) 69 70 if !env.IsOnline() { 71 if requestIDInMeta := metadata.Get(httpx.HeaderRequestID); requestIDInMeta != "" { 72 requestID = requestIDInMeta 73 } 74 mocker, err := ParseMockID(c.Service, requestID) 75 if err == nil { 76 if m, exists := mocker.Mocks[id]; exists { 77 logrus.Errorf("mocking %s with %s", id, m) 78 79 return &MockRequest{ 80 MockData: m, 81 } 82 } 83 } 84 } 85 86 if metadata.Has(courier.VersionSwitchKey) { 87 requestID = courier.ModifyRequestIDWithVersionSwitch(requestID, metadata.Get(courier.VersionSwitchKey)) 88 } else { 89 if _, v, exists := courier.ParseVersionSwitch(requestID); exists { 90 metadata.Set(courier.VersionSwitchKey, v) 91 } 92 } 93 94 if requestID == "" { 95 requestID = uuid.New().String() 96 } 97 98 metadata.Add(httpx.HeaderRequestID, requestID) 99 metadata.Add(httpx.HeaderUserAgent, c.Service+" "+c.Version) 100 101 switch strings.ToLower(c.Mode) { 102 case "grpc": 103 serverName, method := parseID(id) 104 return &transport_grpc.GRPCRequest{ 105 BaseURL: c.GetBaseURL(""), 106 ServerName: serverName, 107 Method: method, 108 Timeout: c.Timeout, 109 RequestID: requestID, 110 Req: req, 111 Metadata: metadata, 112 } 113 default: 114 return &transport_http.HttpRequest{ 115 BaseURL: c.GetBaseURL(c.Mode), 116 Method: httpMethod, 117 URI: uri, 118 ID: id, 119 Timeout: c.Timeout, 120 WrapTransport: c.WrapTransport, 121 Req: req, 122 Metadata: metadata, 123 } 124 } 125 } 126 127 func parseID(id string) (serverName string, method string) { 128 values := strings.Split(id, ".") 129 if len(values) == 2 { 130 serverName = strings.ToLower(strings.Replace(values[0], "Client", "", -1)) 131 method = values[1] 132 } 133 return 134 }