Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 57f12d3563 | |||
| 2e40ca75c9 | |||
| f141f6c34f | |||
| 706ad5481e | |||
| b163480e4a | |||
| ca4d8f8eb5 | |||
| c052904266 | |||
| 73447dfa80 | |||
| ebb1eab10b | |||
| cb5933ccf0 | |||
| e6bec060db | |||
| 4daefbe1c7 | |||
| 1271467476 | |||
| 7a44e3c839 | |||
| 847bd0e41e |
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(ua string, redis *redis.Client) *BasketAPI {
|
||||||
|
return &BasketAPI{NewHttpClient(ua, 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-svc", "/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-svc", url, nil, &res); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
95
api/http.go
Normal file
95
api/http.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HttpClient struct {
|
||||||
|
ua string
|
||||||
|
redis *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHttpClient(ua string, redis *redis.Client) *HttpClient {
|
||||||
|
return &HttpClient{ua, 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", c.ua)
|
||||||
|
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(ua string, redis *redis.Client) *PricingAPI {
|
||||||
|
return &PricingAPI{NewHttpClient(ua, 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-svc", url, nil, res); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
@@ -10,24 +10,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
Name string
|
Name string
|
||||||
Address string
|
Address string
|
||||||
appID string
|
appID string
|
||||||
domain string
|
domain string
|
||||||
port int
|
pathPrefix string
|
||||||
ttl time.Duration
|
port int
|
||||||
agent *consul.Agent
|
ttl time.Duration
|
||||||
kv *consul.KV
|
agent *consul.Agent
|
||||||
|
kv *consul.KV
|
||||||
|
catalog *consul.Catalog
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
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 := new(Service)
|
||||||
s.Name = name
|
s.Name = name
|
||||||
s.Address = hostname
|
s.Address = hostname
|
||||||
s.appID = id
|
s.appID = id
|
||||||
s.domain = domain
|
s.domain = domain
|
||||||
|
s.pathPrefix = pathPrefix
|
||||||
s.port = appPort
|
s.port = appPort
|
||||||
s.ttl = time.Second * 15
|
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.agent = client.Agent()
|
||||||
s.kv = client.KV()
|
s.kv = client.KV()
|
||||||
|
s.catalog = client.Catalog()
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
@@ -104,6 +108,10 @@ func (s *Service) KV() *consul.KV {
|
|||||||
return s.kv
|
return s.kv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) Catalog() *consul.Catalog {
|
||||||
|
return s.catalog
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) healthCheck() (bool, error) {
|
func (s *Service) healthCheck() (bool, error) {
|
||||||
alive := func() bool {
|
alive := func() bool {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
@@ -139,27 +147,19 @@ func (s *Service) healthCheck() (bool, error) {
|
|||||||
func (s *Service) getTags() []string {
|
func (s *Service) getTags() []string {
|
||||||
tags := []string{
|
tags := []string{
|
||||||
"traefik.enable=true",
|
"traefik.enable=true",
|
||||||
"traefik.http.routers." + s.Name + ".rule=Header(`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 + ".entryPoints=https",
|
||||||
"traefik.http.routers." + s.Name + ".tls=true",
|
"traefik.http.routers." + s.Name + ".tls=true",
|
||||||
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
"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.scheme=http",
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.port),
|
"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.auth.forwardauth.address=https://test/auth",
|
||||||
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
||||||
// "traefik.http.middlewares.cors.headers.accesscontrolallowmethods=*",
|
"traefik.http.middlewares.stripprefix_" + s.Name + ".stripprefix.prefixes=" + s.pathPrefix,
|
||||||
// "traefik.http.middlewares.cors.headers.accesscontrolalloworiginlist=*",
|
|
||||||
// "traefik.http.middlewares.cors.headers.accessControlExposeHeaders=true",
|
|
||||||
// "traefik.http.middlewares.cors.headers.accesscontrolmaxage=100",
|
|
||||||
// "traefik.http.middlewares.cors.headers.addvaryheader=true",
|
|
||||||
"traefik.tls.certificates.certfile=/certs/client.cert",
|
"traefik.tls.certificates.certfile=/certs/client.cert",
|
||||||
"traefik.tls.certificates.keyfile=/certs/client.key",
|
"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
|
return tags
|
||||||
|
|||||||
Reference in New Issue
Block a user