Compare commits

...

24 Commits

Author SHA1 Message Date
e3f8352a73 fix traefik auth plugin base config 2023-02-24 00:57:02 +01:00
db07562c5a fix traefik auth plugin base config 2023-02-24 00:50:55 +01:00
57f12d3563 added traefik auth plugin base config 2023-02-24 00:43:27 +01:00
2e40ca75c9 API fix 2022-12-25 22:59:04 +01:00
f141f6c34f Added Internal API Client's classes 2022-12-25 21:39:20 +01:00
706ad5481e Consul catalog refactor 2022-12-24 20:28:37 +01:00
b163480e4a Route fix 2022-12-24 19:47:10 +01:00
ca4d8f8eb5 Route fix 2022-12-24 17:57:59 +01:00
c052904266 Route fix 2022-12-24 17:50:29 +01:00
73447dfa80 Route fix 2022-12-24 17:45:56 +01:00
ebb1eab10b DEBUG change 2022-12-24 17:25:41 +01:00
cb5933ccf0 DEBUG change 2022-12-24 17:10:47 +01:00
e6bec060db DEBUG change 2022-12-24 16:43:19 +01:00
4daefbe1c7 DEBUG change 2022-12-24 16:40:31 +01:00
1271467476 DEBUG change 2022-12-24 16:39:21 +01:00
7a44e3c839 DEBUG change 2022-12-24 16:27:10 +01:00
847bd0e41e router change 2022-12-24 15:09:01 +01:00
31172dee69 CORS off 2022-12-24 08:33:09 +01:00
f5d75ae991 added CORS dev config 2022-12-24 06:07:03 +01:00
867e7d57b7 f* CORS, define it on app level 2022-12-24 06:03:21 +01:00
31f6a7d7e6 f* CORS, define it on app level 2022-12-24 06:01:12 +01:00
e04efcade4 added CORS dev config 2022-12-24 05:58:38 +01:00
c44da161b1 added CORS dev config 2022-12-24 05:50:46 +01:00
ab36ac80ed added CORS dev config 2022-12-24 05:49:58 +01:00
4 changed files with 179 additions and 19 deletions

36
api/basket.go Normal file
View 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
View 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
View 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
}

View File

@@ -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,24 +147,19 @@ func (s *Service) healthCheck() (bool, error) {
func (s *Service) getTags() []string {
tags := []string{
"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 + ".tls=true",
"traefik.http.routers." + s.Name + ".service=" + s.Name,
"traefik.http.routers." + s.Name + ".middlewares=compress,requestid",
"traefik.http.routers." + s.Name + ".middlewares=auth,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.auth.forwardauth.address=http://identity-svc/api/v1/login",
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
"traefik.http.middlewares.cors.headers.accesscontrolallowmethods=*",
"traefik.http.middlewares.cors.headers.accesscontrolalloworiginlist=*",
"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