Compare commits

...

68 Commits

Author SHA1 Message Date
b699008fa3 Fix for better unregistering? 2024-07-17 18:57:50 +02:00
b1f04badd5 Added healthcheck params in consul tags 2024-07-17 18:32:46 +02:00
f0b5e56e43 Added healthcheck params in consul tags 2024-07-17 18:27:21 +02:00
c644f42ea9 Fixed missing AUTH_HANDLER_URL env issue 2024-07-17 17:52:35 +02:00
941c5fdf92 Fixed missing AUTH_HANDLER_URL env issue 2024-07-17 17:48:24 +02:00
03de7082ab Try to fix consul middlewares 2024-07-17 17:15:39 +02:00
def22f26f1 Try to fix consul middlewares 2024-07-17 17:02:02 +02:00
fd37622ad6 Try to fix consul middlewares 2024-07-17 16:44:41 +02:00
96e08fce4c Try to fix consul middleware provider 2024-07-17 16:40:13 +02:00
0f8eda40ad Try to fix consul middleware provider 2024-07-17 16:32:26 +02:00
3285ee65da Try to fix consul middleware provider 2024-07-17 16:29:38 +02:00
47339bda36 Try to fix consul middleware provider 2024-07-17 16:26:31 +02:00
33269f9726 Try to fix consul middleware provider 2024-07-17 16:23:56 +02:00
5b5a8318a4 Fixed consul tags ordering 2024-07-17 16:20:02 +02:00
42b3a44364 Fixed consul tags ordering 2024-07-17 16:11:14 +02:00
20278f24b3 Fixed consul registered middlewares 2024-07-17 16:07:20 +02:00
ae49c9cdb8 Fixed consul registered middlewares 2024-07-17 16:03:46 +02:00
ea08da8d10 Fixed consul registered address 2024-07-17 15:34:55 +02:00
0aa6fc2aa3 Fixed consul registered tags 2024-07-17 15:22:30 +02:00
ab6189e855 Fixed consul registered address 2024-07-17 15:14:20 +02:00
1c244db6ea Fixed consul registered address 2024-07-17 15:09:37 +02:00
3394040a8c Fixed consul registered address 2024-07-17 15:07:09 +02:00
bc439fd4d2 revert consul router rules 2024-07-16 22:23:12 +02:00
16cfa5fead fix consul router rules 2024-07-16 22:17:51 +02:00
044c32e783 fix consul router rules 2024-07-16 21:53:59 +02:00
0d35173aab fix consul router rules 2024-07-16 21:47:58 +02:00
255c67ff92 fix consul tags 2024-07-16 21:39:53 +02:00
9dc85a1963 revert consul tags 2024-07-16 21:34:58 +02:00
91c27b30bf fix in consul register method 2024-07-16 19:13:40 +02:00
3ed094ff55 remove deprecated amqp mod, added official amqp mod 2024-07-16 18:54:28 +02:00
050aef8b7a remove deprecated amqp mod, added official amqp mod 2024-07-16 18:40:03 +02:00
420515f5b6 make error message more human readable 2024-07-16 18:19:49 +02:00
5dcd27e8b8 Update traefik router tag 2024-07-15 20:10:06 +02:00
0333862e3c error print fix 2024-05-30 17:19:16 +02:00
081e1edd24 go.mod update priv git url 2024-05-30 17:16:39 +02:00
c454307d56 consul fixes 2024-04-19 19:17:09 +02:00
359a89ba62 consul fixes 2024-04-19 19:01:16 +02:00
3b73091cfb consul fixes 2023-07-29 15:11:22 +02:00
c8beab76f1 consul fixes 2023-07-29 14:57:36 +02:00
599eaa3712 consul fixes 2023-07-29 13:47:51 +02:00
1de38fdfdf consul fixes 2023-07-26 23:19:17 +02:00
4d1f3644b0 consul fixes 2023-07-26 23:14:25 +02:00
71b3f13284 consul fixes 2023-07-08 17:18:19 +02:00
c64bc3fe6b consul fixes 2023-07-08 17:16:53 +02:00
016bddb5c4 register fix 2 2023-07-08 17:11:53 +02:00
98d85810f3 register fix 2 2023-07-08 13:02:13 +02:00
3fc1191f6e register fix 2 2023-07-08 02:22:28 +02:00
52793ac244 register fix 2 2023-07-06 23:02:05 +02:00
f8a1ae841d register fix 2 2023-07-06 21:49:12 +02:00
9d424742d6 register fix 2 2023-07-06 21:45:45 +02:00
a9b73a6536 register fix 2 2023-07-06 19:30:00 +02:00
95584eb407 register fix 2 2023-07-05 23:44:47 +02:00
ce7e2bc75a register fix 2 2023-07-05 23:37:55 +02:00
4bedc5d926 register fix 2 2023-07-05 23:35:17 +02:00
ea7f98f8bb register fix 2023-07-05 22:45:12 +02:00
bbdce9b25a register fix 2023-07-05 22:25:56 +02:00
1d75153698 consul register fix 2023-07-04 18:54:12 +02:00
c177133daf consul register fix 2023-07-04 18:46:00 +02:00
dad915f2ec consul register fix 2023-07-04 18:41:52 +02:00
54e4006d0d consul connect fixing 2023-07-04 00:02:23 +02:00
9cb590bf7a consul register as sidecar_service fix 2023-07-03 00:36:07 +02:00
70c41dab96 consul register as sidecar_service fix 2023-07-02 23:04:26 +02:00
99ac0a83c6 consul register as sidecar_service 2023-07-02 23:03:05 +02:00
30c45b4503 consul https temp fix 2023-07-02 22:38:13 +02:00
49f552bf68 consul register as sidecar_service 2023-07-02 22:30:53 +02:00
6f875cbb94 consul register as sidecar_service 2023-07-02 22:24:55 +02:00
f9331deec1 consul debug 2023-07-02 01:46:11 +02:00
c29bb94c3d consul debug 2023-07-02 01:32:45 +02:00
8 changed files with 102 additions and 43 deletions

View File

@@ -3,7 +3,7 @@ package api
import (
"fmt"
def "git.pbiernat.dev/egommerce/api-entities/http"
def "git.pbiernat.io/egommerce/api-entities/http"
"github.com/go-redis/redis/v8"
)

View File

@@ -3,7 +3,7 @@ package api
import (
"fmt"
def "git.pbiernat.dev/egommerce/api-entities/http"
def "git.pbiernat.io/egommerce/api-entities/http"
"github.com/go-redis/redis/v8"
)

View File

@@ -6,7 +6,7 @@ import (
"strconv"
"time"
"git.pbiernat.dev/egommerce/go-api-pkg/config"
"git.pbiernat.io/egommerce/go-api-pkg/config"
consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/connect"
)
@@ -61,16 +61,26 @@ func (s *Service) GetID() string {
}
func (s *Service) GetFullAddr() string {
return fmt.Sprintf("https://%s:%d/", s.domain, s.port)
isTLS := s.port == 443
proto := "http"
if isTLS {
proto = "https"
}
return fmt.Sprintf("%s://%s:%d/", proto, s.domain, s.port)
}
func (s *Service) Register() error {
def := &consul.AgentServiceRegistration{
ID: s.GetID(),
ID: s.GetID(),
// Kind: consul.ServiceKindConnectProxy,
Name: s.Name,
Address: s.Address,
Port: s.port,
Tags: s.getTags(),
// Connect: &consul.AgentServiceConnect{Native: true},
// Proxy: &consul.AgentServiceConnectProxyConfig{
// DestinationServiceName: s.Name,
// },
Check: &consul.AgentServiceCheck{
TTL: s.ttl.String(),
Status: "passing",
@@ -82,38 +92,47 @@ func (s *Service) Register() error {
return err
}
go func(s *Service) { // startup register
return nil
}
func (s *Service) Unregister() error {
s.client.Catalog().Deregister(&consul.CatalogDeregistration{
Address: s.Address,
ServiceID: s.GetID(),
}, nil)
return s.agent.ServiceDeregister(s.GetID())
}
func (s *Service) RegisterHealthChecks() {
go func() { // startup register
ticker := time.NewTicker(time.Second * 1)
for range ticker.C {
if ok, _ := s.healthCheck(); ok {
ticker.Stop()
}
}
}(s)
}()
go func(s *Service) { // TTL
interval := s.ttl - time.Second*2 // 2 seconds overhead
go func() { // TTL
interval := s.ttl - (time.Second * 2) // 2 seconds overhead
ticker := time.NewTicker(interval)
for range ticker.C {
if _, err := s.healthCheck(); err != nil {
fmt.Printf("TTL Error #: %v\n", err)
fmt.Printf("HealthCheck endpoint not available #: %v\n", err)
}
}
}(s)
return nil
}
func (s *Service) Unregister() error {
return s.agent.ServiceDeregister(s.GetID())
}()
}
func (s *Service) Connect() (*connect.Service, error) {
// srvName := s.Name
srvName := "egommerce"
fmt.Printf("CONNECT SERVER: %s\n", srvName)
svc, err := connect.NewService(srvName, s.client)
// l := hclog.New(&hclog.LoggerOptions{
// Name: "consul-registry",
// Level: hclog.Trace,
// })
svc, err := connect.NewService(s.Name, s.client)
s.connect = svc
cnf := svc.ServerTLSConfig()
fmt.Printf("CONNECT SERVER:: %s CONFIG:: %v\n", s.Name, cnf)
for k, c := range cnf.Certificates {
fmt.Printf("CONNECT CERT %d: %v", k, c)
}
@@ -135,7 +154,6 @@ func (s *Service) healthCheck() (bool, error) {
}
req.Header.Set("User-Agent", "service/internal")
fmt.Printf("Sending HEALTH CHECK request to: %s\n", healthUrl)
resp, err := client.Do(req)
if err != nil {
return false
@@ -144,7 +162,6 @@ func (s *Service) healthCheck() (bool, error) {
var body []byte
resp.Body.Read(body)
fmt.Printf("HEALTH CHECK response to: %v -- %v\n", resp, body)
return resp.StatusCode == http.StatusOK
}()
@@ -166,20 +183,25 @@ func (s *Service) healthCheck() (bool, error) {
func (s *Service) getTags() []string {
tags := []string{
"traefik.enable=true",
"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=auth_" + s.Name + ",requestid_" + s.Name + ",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.services." + s.Name + ".loadbalancer.passhostheader=false",
"traefik.http.middlewares.auth_" + s.Name + ".plugin.auth.handlerURL=" + config.GetEnv("AUTH_HANDLER_URL", ""),
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.trustForwardHeader=true",
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.authRequestHeaders=Cookie",
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.authResponseHeaders=Set-Cookie, Server",
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.trustForwardHeader=true",
"traefik.http.middlewares.auth_" + s.Name + ".plugin.auth.handlerURL=" + config.GetEnv("AUTH_HANDLER_URL", "http://identity.service.ego.io/api/v1/traefik"),
"traefik.http.middlewares.requestid_" + s.Name + ".plugin.requestid.headerName=X-Request-ID",
"traefik.http.middlewares.stripprefix_" + s.Name + ".stripprefix.prefixes=" + s.pathPrefix,
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)",
// "traefik.http.routers." + s.Name + ".rule=Host(`" + s.domain + "`)",
"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=auth_" + s.Name + ",stripprefix_" + s.Name,
"traefik.http.routers." + s.Name + ".middlewares=auth_" + s.Name + ",requestid_" + s.Name + ",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.services." + s.Name + ".loadbalancer.passhostheader=true",
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=1s",
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.timeout=1s",
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.path=/health",
"traefik.tls.certificates.certfile=/certs/client.cert",
"traefik.tls.certificates.keyfile=/certs/client.key",
}

6
go.mod
View File

@@ -1,15 +1,14 @@
module git.pbiernat.dev/egommerce/go-api-pkg
module git.pbiernat.io/egommerce/go-api-pkg
go 1.18
require (
git.pbiernat.dev/egommerce/api-entities v0.0.26
git.pbiernat.io/egommerce/api-entities v0.2.3
github.com/fluent/fluent-logger-golang v1.9.0
github.com/go-redis/redis/v8 v8.11.5
github.com/hashicorp/consul v1.16.0
github.com/hashicorp/consul/api v1.22.0
github.com/joho/godotenv v1.5.1
github.com/streadway/amqp v1.0.0
)
require (
@@ -71,6 +70,7 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rabbitmq/amqp091-go v1.10.0 // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.3 // indirect

7
go.sum
View File

@@ -35,8 +35,7 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.pbiernat.dev/egommerce/api-entities v0.0.26 h1:Avz02GINwuYWOjw1fmZIJ3QgGEIz3a5vRQZNaxxUQIk=
git.pbiernat.dev/egommerce/api-entities v0.0.26/go.mod h1:+BXvUcr6Cr6QNpJsW8BUfe1vVILdWDADNE0e3u0lNvU=
git.pbiernat.io/egommerce/api-entities v0.2.3/go.mod h1:INXAG5x4+i+vNwg1NpfPHiDW8nY1kn1K7pgLOtX+/I0=
github.com/Azure/azure-sdk-for-go v44.0.0+incompatible h1:e82Yv2HNpS0kuyeCrV29OPKvEiqfs2/uJHic3/3iKdg=
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
github.com/Azure/go-autorest/autorest v0.11.18 h1:90Y4srNYrwOtAgVo3ndrQkTYn6kf1Eg/AjTFJ8Is2aM=
@@ -470,6 +469,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 h1:Wdi9nwnhFNAlseAOekn6B5G/+GMtks9UKbvRU/CMM/o=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
@@ -487,8 +488,6 @@ github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/softlayer/softlayer-go v0.0.0-20180806151055-260589d94c7d h1:bVQRCxQvfjNUeRqaY/uT0tFuvuFY0ulgnczuR684Xic=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=

View File

@@ -3,7 +3,7 @@ package rabbitmq
import (
"log"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
)
func Open(url string) (*amqp.Connection, *amqp.Channel, error) {

View File

@@ -6,12 +6,12 @@ import (
"fmt"
"log"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
)
type Message map[string]interface{}
func Serialize(msg any) (string, error) {
func Serialize(msg any) (string, error) { // FIXME move to separate service
var b bytes.Buffer
encoder := json.NewEncoder(&b)
err := encoder.Encode(msg)
@@ -19,7 +19,7 @@ func Serialize(msg any) (string, error) {
return b.String(), err
}
func Deserialize(b []byte) (Message, error) {
func Deserialize(b []byte) (Message, error) { // FIXME move to separate service
var msg Message
buf := bytes.NewBuffer(b)
decoder := json.NewDecoder(buf)

38
redis/cache.go Normal file
View File

@@ -0,0 +1,38 @@
package redis
// import (
// "context"
// "strconv"
// "time"
// "github.com/go-redis/redis/v8"
// )
// func NewCache(host string, port int, password string) *redis.Client {
// redis := redis.NewClient(&redis.Options{
// Addr: host + ":" + strconv.Itoa(port),
// Password: password,
// DB: 0,
// })
// defer redis.Close()
// return redis
// }
// func Get(ctx context.Context, key string) (float64, error) {
// // ctx := context.Background() // FIXME
// price, err := s.cache.Get(ctx, key).Float64()
// if err != nil {
// s.log.Log("cache read error(key not exists): %#v", err)
// return 0, err
// }
// return price, nil
// }
// func Set(ctx context.Context, key string, value any, exp time.Duration) error {
// // ctx := context.Background() // FIXME
// s.cache.Set(ctx, key, value, exp)
// return nil
// }