Compare commits

..

1 Commits
rm ... v0.1.1

Author SHA1 Message Date
d620dfa704 update getTags() func 2023-06-21 20:28:53 +02:00
4 changed files with 7 additions and 34 deletions

View File

@@ -1,22 +0,0 @@
package config
import (
"os"
"github.com/joho/godotenv"
)
var ErrLoadingEnvs error
func init() {
ErrLoadingEnvs = godotenv.Load()
}
func GetEnv(name string, defVal string) string {
env := os.Getenv(name)
if env == "" {
return defVal
}
return env
}

View File

@@ -6,7 +6,6 @@ import (
"strconv" "strconv"
"time" "time"
"git.pbiernat.dev/egommerce/go-api-pkg/config"
consul "github.com/hashicorp/consul/api" consul "github.com/hashicorp/consul/api"
) )
@@ -33,7 +32,7 @@ func NewService(servAddr, id, name, hostname, domain, pathPrefix string, appPort
s.domain = domain s.domain = domain
s.pathPrefix = pathPrefix s.pathPrefix = pathPrefix
s.port = appPort s.port = appPort
s.ttl = time.Second * 10 s.ttl = time.Second * 15
client, err := consul.NewClient(newClientConfig(servAddr)) client, err := consul.NewClient(newClientConfig(servAddr))
if err != nil { if err != nil {
@@ -54,7 +53,7 @@ func newClientConfig(serverAddr string) *consul.Config {
} }
func (s *Service) GetID() string { func (s *Service) GetID() string {
return fmt.Sprintf("%s:%s", s.Name, s.appID) return fmt.Sprintf("%s_%s", s.Name, s.appID)
} }
func (s *Service) GetFullAddr() string { func (s *Service) GetFullAddr() string {
@@ -70,7 +69,7 @@ func (s *Service) Register() error {
Tags: s.getTags(), Tags: s.getTags(),
Check: &consul.AgentServiceCheck{ Check: &consul.AgentServiceCheck{
TTL: s.ttl.String(), TTL: s.ttl.String(),
DeregisterCriticalServiceAfter: "10s", DeregisterCriticalServiceAfter: "5s",
}, },
} }
@@ -79,7 +78,7 @@ func (s *Service) Register() error {
} }
go func(s *Service) { // startup register go func(s *Service) { // startup register
ticker := time.NewTicker(time.Second * 1) ticker := time.NewTicker(time.Millisecond * 100)
for range ticker.C { for range ticker.C {
if ok, _ := s.healthCheck(); ok { if ok, _ := s.healthCheck(); ok {
ticker.Stop() ticker.Stop()
@@ -92,7 +91,7 @@ func (s *Service) Register() error {
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
for range ticker.C { for range ticker.C {
if _, err := s.healthCheck(); err != nil { if _, err := s.healthCheck(); err != nil {
fmt.Printf("TTL Error #: %v\n", err) fmt.Printf("TTL Error: %v\n", err)
} }
} }
}(s) }(s)
@@ -119,7 +118,7 @@ func (s *Service) healthCheck() (bool, error) {
if err != nil { if err != nil {
return false return false
} }
req.Header.Set("User-Agent", "service/internal") req.Header.Set("User-Agent", "Health Check")
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
@@ -132,7 +131,6 @@ func (s *Service) healthCheck() (bool, error) {
if alive { if alive {
if err := s.agent.PassTTL("service:"+s.GetID(), "OK"); err != nil { if err := s.agent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
fmt.Printf("Failed to pass TTL: %v", err)
return false, err return false, err
} }
return true, nil return true, nil
@@ -155,7 +153,7 @@ func (s *Service) getTags() []string {
"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.services." + s.Name + ".loadbalancer.passhostheader=false", "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 + ".plugin.auth.handlerURL",
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.authRequestHeaders=Cookie", // "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.authResponseHeaders=Set-Cookie, Server",
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.trustForwardHeader=true", // "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.trustForwardHeader=true",

1
go.mod
View File

@@ -21,7 +21,6 @@ require (
github.com/hashicorp/go-rootcerts v1.0.2 // indirect github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.10.1 // indirect github.com/hashicorp/serf v0.10.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect

2
go.sum
View File

@@ -64,8 +64,6 @@ github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR
github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0= github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0=
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY= github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4= github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=