Compare commits

..

8 Commits

Author SHA1 Message Date
7a35d1f44a Consul debug 2023-06-28 22:54:58 +02:00
7bfd048051 Consul debug 2023-06-28 22:50:16 +02:00
1433b69abe Moar HR 2023-06-28 16:37:18 +02:00
c49ec85b3b update config lib 2023-06-21 20:48:32 +02:00
bdac1d5bc9 update configl lib 2023-06-21 20:45:46 +02:00
8baf928a5b added config package 2023-06-21 20:38:51 +02:00
20b01eda68 update getTags() func 2023-06-21 20:33:09 +02:00
3685269236 Fixed traefik custom auth plugin config 2023-06-14 15:50:53 +02:00
4 changed files with 32 additions and 3 deletions

22
config/config.go Normal file
View 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
}

View File

@@ -6,6 +6,7 @@ import (
"strconv"
"time"
"git.pbiernat.dev/egommerce/go-api-pkg/config"
consul "github.com/hashicorp/consul/api"
)
@@ -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 {
@@ -91,7 +92,7 @@ func (s *Service) Register() error {
ticker := time.NewTicker(interval)
for range ticker.C {
if _, err := s.healthCheck(); err != nil {
fmt.Printf("TTL Error: %v\n", err)
fmt.Printf("TTL Error #: %v\n", err)
}
}
}(s)
@@ -114,14 +115,17 @@ func (s *Service) healthCheck() (bool, error) {
alive := func() bool {
client := &http.Client{}
healthUrl := s.GetFullAddr() + "health"
fmt.Printf("Sending request to the %s", healthUrl)
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
if err != nil {
fmt.Printf("Failed to create new Consul request: %v\n", err)
return false
}
req.Header.Set("User-Agent", "Health Check")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send Consul request: %v\n", err)
return false
}
defer resp.Body.Close()
@@ -153,7 +157,7 @@ func (s *Service) getTags() []string {
"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 + ".auth.handlerURL=http://identity-svc/api/v1/traefik",
"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",

1
go.mod
View File

@@ -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
View File

@@ -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=