Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f141f6c34f | |||
| 706ad5481e | |||
| b163480e4a | |||
| ca4d8f8eb5 | |||
| c052904266 | |||
| 73447dfa80 | |||
| ebb1eab10b | |||
| cb5933ccf0 | |||
| e6bec060db | |||
| 4daefbe1c7 | |||
| 1271467476 | |||
| 7a44e3c839 | |||
| 847bd0e41e | |||
| 31172dee69 | |||
| f5d75ae991 | |||
| 867e7d57b7 | |||
| 31f6a7d7e6 | |||
| e04efcade4 | |||
| c44da161b1 | |||
| ab36ac80ed | |||
| 4dc279440c | |||
| 4f3386d548 |
36
api/basket.go
Normal file
36
api/basket.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
def "git.pbiernat.dev/egommerce/api-entities/http"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
func NewBasketAPI(redis *redis.Client) *BasketAPI {
|
||||
return &BasketAPI{NewHttpClient(redis)}
|
||||
}
|
||||
|
||||
type BasketAPI struct {
|
||||
httpClient *HttpClient
|
||||
}
|
||||
|
||||
func (a *BasketAPI) GetBasket(basketID string) (*def.GetBasketResponse, error) {
|
||||
req := &def.GetBasketRequest{BasketID: basketID}
|
||||
res := new(def.GetBasketResponse)
|
||||
if err := a.httpClient.SendGet("basket", "/api/v1/basket", req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (a *BasketAPI) GetBasketItems(basketID string) ([]*def.GetBasketItemsResponse, error) {
|
||||
url := fmt.Sprintf("/api/v1/basket/%s/items", basketID)
|
||||
var res []*def.GetBasketItemsResponse
|
||||
if err := a.httpClient.SendGet("basket", url, nil, &res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
98
api/http.go
Normal file
98
api/http.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
const (
|
||||
HEADER_USER_AGENT = "order-httpclient"
|
||||
)
|
||||
|
||||
type HttpClient struct {
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewHttpClient(redis *redis.Client) *HttpClient {
|
||||
return &HttpClient{redis}
|
||||
}
|
||||
|
||||
func (c *HttpClient) SendGet(api, url string, data, out any) error {
|
||||
res, err := c.sendRequest(api, url, http.MethodGet, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(&out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *HttpClient) SendPost(api, url string, data, out any) (any, error) {
|
||||
res, err := c.sendRequest(api, url, http.MethodPost, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *HttpClient) sendRequest(api, url, method string, data any) (*http.Response, error) {
|
||||
apiUrl := c.getApiUrl(api) + url
|
||||
client := &http.Client{}
|
||||
|
||||
json, err := json.Marshal(&data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, apiUrl, bytes.NewBuffer(json))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", HEADER_USER_AGENT)
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// defer res.Body.Close()
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *HttpClient) getApiUrl(api string) string {
|
||||
ctx, key, apiAddr := context.Background(), "internal__"+api+"_ips", api
|
||||
// FIXME: key name ^^
|
||||
cmd := c.redis.LLen(ctx, key)
|
||||
if cmd.Err() == nil {
|
||||
len := int(cmd.Val())
|
||||
if len == 0 {
|
||||
apiAddr = c.redis.LIndex(ctx, key, 0).Val()
|
||||
} else {
|
||||
apiAddr = c.redis.LIndex(ctx, key, int64(rand.Intn(len-0)+0)).Val()
|
||||
}
|
||||
}
|
||||
|
||||
if apiAddr == "" {
|
||||
apiAddr = api // default api run on 80 int port
|
||||
}
|
||||
|
||||
return "http://" + apiAddr
|
||||
}
|
||||
26
api/pricing.go
Normal file
26
api/pricing.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
def "git.pbiernat.dev/egommerce/api-entities/http"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
func NewPricingAPI(redis *redis.Client) *PricingAPI {
|
||||
return &PricingAPI{NewHttpClient(redis)}
|
||||
}
|
||||
|
||||
type PricingAPI struct {
|
||||
httpClient *HttpClient
|
||||
}
|
||||
|
||||
func (a *PricingAPI) GetProductPrice(productID int) (*def.ProductPriceResponse, error) {
|
||||
url := fmt.Sprintf("/api/v1/product/%d", productID)
|
||||
res := new(def.ProductPriceResponse)
|
||||
if err := a.httpClient.SendGet("pricing", url, nil, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
@@ -14,20 +14,23 @@ type Service struct {
|
||||
Address string
|
||||
appID string
|
||||
domain string
|
||||
pathPrefix string
|
||||
port int
|
||||
ttl time.Duration
|
||||
agent *consul.Agent
|
||||
kv *consul.KV
|
||||
catalog *consul.Catalog
|
||||
}
|
||||
|
||||
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
||||
|
||||
func NewService(servAddr, id, name, hostname, domain string, appPort int) (*Service, error) {
|
||||
func NewService(servAddr, id, name, hostname, domain, pathPrefix string, appPort int) (*Service, error) {
|
||||
s := new(Service)
|
||||
s.Name = name
|
||||
s.Address = hostname
|
||||
s.appID = id
|
||||
s.domain = domain
|
||||
s.pathPrefix = pathPrefix
|
||||
s.port = appPort
|
||||
s.ttl = time.Second * 15
|
||||
|
||||
@@ -37,6 +40,7 @@ func NewService(servAddr, id, name, hostname, domain string, appPort int) (*Serv
|
||||
}
|
||||
s.agent = client.Agent()
|
||||
s.kv = client.KV()
|
||||
s.catalog = client.Catalog()
|
||||
|
||||
return s, nil
|
||||
}
|
||||
@@ -104,6 +108,10 @@ func (s *Service) KV() *consul.KV {
|
||||
return s.kv
|
||||
}
|
||||
|
||||
func (s *Service) Catalog() *consul.Catalog {
|
||||
return s.catalog
|
||||
}
|
||||
|
||||
func (s *Service) healthCheck() (bool, error) {
|
||||
alive := func() bool {
|
||||
client := &http.Client{}
|
||||
@@ -139,22 +147,18 @@ func (s *Service) healthCheck() (bool, error) {
|
||||
func (s *Service) getTags() []string {
|
||||
tags := []string{
|
||||
"traefik.enable=true",
|
||||
"traefik.http.routers." + s.Name + ".rule=Headers(`X-API-SERVICE`, `" + s.Name + "`)",
|
||||
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)",
|
||||
"traefik.http.routers." + s.Name + ".entryPoints=https",
|
||||
"traefik.http.routers." + s.Name + ".tls=true",
|
||||
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
||||
"traefik.http.routers." + s.Name + ".middlewares=compress,requestid",
|
||||
"traefik.http.routers." + s.Name + ".middlewares=requestid,stripprefix_" + s.Name,
|
||||
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
||||
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.port),
|
||||
"traefik.http.middlewares.compress.compress=true",
|
||||
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
||||
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
||||
"traefik.http.middlewares.stripprefix_" + s.Name + ".stripprefix.prefixes=" + s.pathPrefix,
|
||||
"traefik.tls.certificates.certfile=/certs/client.cert",
|
||||
"traefik.tls.certificates.keyfile=/certs/client.key",
|
||||
// "traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
||||
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + "=" + bFullAddr,
|
||||
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + ".url=" + bFullAddr,
|
||||
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.path=/health",
|
||||
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=10s",
|
||||
}
|
||||
|
||||
return tags
|
||||
|
||||
3
go.mod
3
go.mod
@@ -4,6 +4,7 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/fluent/fluent-logger-golang v1.9.0
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/hashicorp/consul/api v1.18.0
|
||||
github.com/streadway/amqp v1.0.0
|
||||
)
|
||||
@@ -11,6 +12,8 @@ require (
|
||||
require (
|
||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/fatih/color v1.9.0 // indirect
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
|
||||
github.com/hashicorp/go-hclog v0.12.0 // indirect
|
||||
|
||||
13
go.sum
13
go.sum
@@ -6,15 +6,22 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
|
||||
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
|
||||
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
|
||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
|
||||
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
|
||||
github.com/fluent/fluent-logger-golang v1.9.0 h1:zUdY44CHX2oIUc7VTNZc+4m+ORuO/mldQDA7czhWXEg=
|
||||
github.com/fluent/fluent-logger-golang v1.9.0/go.mod h1:2/HCT/jTy78yGyeNGQLGQsjF3zzzAuy6Xlk6FCMV5eU=
|
||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c h1:964Od4U6p2jUkFxvCydnIczKteheJEzHRToSGK3Bnlw=
|
||||
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||
@@ -85,6 +92,9 @@ github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUb
|
||||
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
|
||||
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
||||
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
|
||||
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c h1:Lgl0gzECD8GnQ5QCWA8o6BtfL6mDH5rQgM4/fX3avOs=
|
||||
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ=
|
||||
@@ -149,6 +159,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
@@ -161,7 +172,9 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
Reference in New Issue
Block a user