Compare commits

...

35 Commits

Author SHA1 Message Date
eb763ed49c fix in consul discovery 2024-07-19 16:52:54 +02:00
6537d79b19 fix in consul discovery 2024-07-19 16:48:08 +02:00
e55a2f42fe fix in consul discovery 2024-07-19 16:45:42 +02:00
82d68e91b8 fix in consul discovery 2024-07-19 16:41:37 +02:00
767eb5688c fix in consul discovery 2024-07-19 16:39:11 +02:00
37fa05402e fix in consul discovery 2024-07-19 16:30:04 +02:00
615281ae9a fix in consul discovery lib 2024-07-19 16:25:41 +02:00
42532b1d44 FIX Remove RetryIf plugin 2024-07-17 22:59:16 +02:00
1d4164711e FIX RetryIf plugin config 2024-07-17 22:46:28 +02:00
a50778380e Added RetryIf plugin with config 2024-07-17 22:39:49 +02:00
159bce1ff2 Added RetryIf plugin with config 2024-07-17 22:37:26 +02:00
22bc1e29a3 Fix: changed GetFullAddr() method: add debug point 2024-07-17 22:07:16 +02:00
4ce9064fbb Fix: changed GetFullAddr() method 2024-07-17 21:53:35 +02:00
75f7a75379 Fix: switch between domain or ip address for consul registering 2024-07-17 21:18:13 +02:00
398d7a2074 Fix in consul/discovery.go 2024-07-17 20:39:19 +02:00
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
2 changed files with 41 additions and 21 deletions

View File

@@ -3,7 +3,6 @@ package config
import (
"os"
"github.com/gofiber/fiber/v2/log"
"github.com/joho/godotenv"
)
@@ -16,7 +15,7 @@ func init() {
func GetEnv(name string, defVal string) string {
env := os.Getenv(name)
if env == "" {
log.Panicf("Missing %s env variable", name)
return defVal
}
return env

View File

@@ -23,20 +23,27 @@ type Service struct {
agent *consul.Agent
connect *connect.Service
kv *consul.KV
hcTicker *time.Ticker
ttlTicker *time.Ticker
}
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
func NewService(servAddr, id, name, hostname, domain, pathPrefix string, appPort int) (*Service, error) {
func NewService(servAddr, id, name, useDomainOverIp, addr, domain, pathPrefix string, appPort int) (*Service, error) {
s := new(Service)
s.Name = name
s.Address = hostname
s.Address = addr
s.appID = id
s.domain = domain
s.pathPrefix = pathPrefix
s.port = appPort
s.ttl = time.Second * 10
if useDomainOverIp == "true" { // FIXME types...
s.Address = domain
}
client, err := consul.NewClient(newClientConfig(servAddr))
if err != nil {
return nil, err
@@ -66,7 +73,7 @@ func (s *Service) GetFullAddr() string {
if isTLS {
proto = "https"
}
return fmt.Sprintf("%s://%s:%d/", proto, s.domain, s.port)
return fmt.Sprintf("%s://%s:%d/", proto, s.Address, s.port)
}
func (s *Service) Register() error {
@@ -95,25 +102,34 @@ func (s *Service) Register() error {
return nil
}
func (s *Service) Unregister() error {
// s.ttlTicker.Stop()
// s.hcTicker.Stop()
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 {
s.hcTicker = time.NewTicker(time.Second)
for range s.hcTicker.C {
if ok, _ := s.healthCheck(); ok {
ticker.Stop()
fmt.Println("Stoping HC Ticker goroutine")
s.hcTicker.Stop()
}
}
}()
go func() { // TTL
interval := s.ttl - (time.Second * 2) // 2 seconds overhead
ticker := time.NewTicker(interval)
for range ticker.C {
s.ttlTicker = time.NewTicker(s.ttl)
for range s.ttlTicker.C {
if _, err := s.healthCheck(); err != nil {
fmt.Printf("HealthCheck endpoint not available #: %v\n", err)
fmt.Printf("HealthCheck endpoint not available (%s)#: %v\n", s.GetFullAddr(), err)
s.ttlTicker.Stop()
}
}
}()
@@ -178,22 +194,27 @@ func (s *Service) healthCheck() (bool, error) {
func (s *Service) getTags() []string {
tags := []string{
"traefik.enable=true",
// "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 + ".plugin.auth.handlerURL=" + config.GetEnv("AUTH_HANDLER_URL", "http://identity.service.ego.io/api/v1/traefik"),
"traefik.http.middlewares.stripprefix_" + s.Name + ".stripprefix.prefixes=" + s.pathPrefix,
"traefik.http.middlewares.requestid_" + s.Name + ".plugin.requestid.headerName=X-Request-ID",
// "treafik.http.middlewares.retryif_" + s.Name + ".plugin.retryif.attempts=3",
// "treafik.http.middlewares.retryif_" + s.Name + ".plugin.retryif.statusCode=503",
"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=stripprefix_" + s.Name,
// "traefik.http.routers." + s.Name + ".middlewares=auth_" + s.Name + ",requestid_" + s.Name + ",stripprefix_" + s.Name,
// "traefik.http.routers." + s.Name + ".middlewares=auth_" + s.Name + ",stripprefix_" + s.Name,
"traefik.http.routers." + s.Name + ".middlewares=auth_" + s.Name + ",stripprefix_" + s.Name + ",requestid_" + 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.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.requestid_" + s.Name + ".plugin.requestid.headerName=X-Request-ID",
"traefik.http.middlewares.stripprefix_" + s.Name + ".stripprefix.prefixes=" + s.pathPrefix,
"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",
}