Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fc88125fe4 | |||
| 0aa79c7d35 | |||
| d484c0e3b0 | |||
| 7a35d1f44a | |||
| 7bfd048051 | |||
| 1433b69abe | |||
| c49ec85b3b | |||
| bdac1d5bc9 | |||
| 8baf928a5b | |||
| 20b01eda68 | |||
| 3685269236 | |||
| 2a6671654a | |||
| 3a42715b5d | |||
| 222300d8e3 | |||
| 6c00fc98f5 | |||
| effc7a6039 |
22
config/config.go
Normal file
22
config/config.go
Normal file
@@ -0,0 +1,22 @@
|
||||
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
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.pbiernat.dev/egommerce/go-api-pkg/config"
|
||||
consul "github.com/hashicorp/consul/api"
|
||||
)
|
||||
|
||||
@@ -32,7 +33,7 @@ func NewService(servAddr, id, name, hostname, domain, pathPrefix string, appPort
|
||||
s.domain = domain
|
||||
s.pathPrefix = pathPrefix
|
||||
s.port = appPort
|
||||
s.ttl = time.Second * 15
|
||||
s.ttl = time.Second * 10
|
||||
|
||||
client, err := consul.NewClient(newClientConfig(servAddr))
|
||||
if err != nil {
|
||||
@@ -53,7 +54,7 @@ func newClientConfig(serverAddr string) *consul.Config {
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -69,7 +70,7 @@ func (s *Service) Register() error {
|
||||
Tags: s.getTags(),
|
||||
Check: &consul.AgentServiceCheck{
|
||||
TTL: s.ttl.String(),
|
||||
DeregisterCriticalServiceAfter: "5s",
|
||||
DeregisterCriticalServiceAfter: "10s",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -78,25 +79,23 @@ func (s *Service) Register() error {
|
||||
}
|
||||
|
||||
go func(s *Service) { // startup register
|
||||
ticker := time.NewTicker(time.Millisecond * 100)
|
||||
ticker := time.NewTicker(time.Second * 1)
|
||||
for range ticker.C {
|
||||
ok, _ := s.healthCheck()
|
||||
if ok {
|
||||
if ok, _ := s.healthCheck(); ok {
|
||||
ticker.Stop()
|
||||
}
|
||||
}
|
||||
}(s)
|
||||
|
||||
go func(s *Service) { // TTL
|
||||
interval := s.ttl - time.Second*2
|
||||
ticker := time.NewTicker(interval)
|
||||
for range ticker.C {
|
||||
_, err := s.healthCheck()
|
||||
if err != nil {
|
||||
fmt.Printf("TTL Error: %v\n", err)
|
||||
}
|
||||
}
|
||||
}(s)
|
||||
// go func(s *Service) { // 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)
|
||||
// }
|
||||
// }
|
||||
// }(s)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -120,7 +119,7 @@ func (s *Service) healthCheck() (bool, error) {
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
req.Header.Set("User-Agent", "Health Check")
|
||||
req.Header.Set("User-Agent", "service/internal")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
@@ -133,6 +132,7 @@ func (s *Service) healthCheck() (bool, error) {
|
||||
|
||||
if alive {
|
||||
if err := s.agent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
|
||||
fmt.Printf("Failed to pass TTL: %v", err)
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
@@ -151,14 +151,15 @@ func (s *Service) getTags() []string {
|
||||
"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,requestid,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=false",
|
||||
"traefik.http.middlewares.auth.forwardauth.address=http://identity-svc/api/v1/traefik",
|
||||
"traefik.http.middlewares.auth.forwardauth.authResponseHeaders=Set-Cookie, Server",
|
||||
"traefik.http.middlewares.auth.forwardauth.trustForwardHeader=true",
|
||||
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
||||
"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.tls.certificates.certfile=/certs/client.cert",
|
||||
"traefik.tls.certificates.keyfile=/certs/client.key",
|
||||
|
||||
1
go.mod
1
go.mod
@@ -21,6 +21,7 @@ require (
|
||||
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.4 // 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-isatty v0.0.12 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -64,6 +64,8 @@ github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR
|
||||
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/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.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
|
||||
Reference in New Issue
Block a user