Compare commits

...

4 Commits

Author SHA1 Message Date
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

View File

@@ -9,7 +9,6 @@ import (
"git.pbiernat.dev/egommerce/go-api-pkg/config" "git.pbiernat.dev/egommerce/go-api-pkg/config"
consul "github.com/hashicorp/consul/api" consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/connect" "github.com/hashicorp/consul/connect"
"github.com/hashicorp/go-hclog"
) )
type Service struct { type Service struct {
@@ -62,7 +61,12 @@ func (s *Service) GetID() string {
} }
func (s *Service) GetFullAddr() 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 { func (s *Service) Register() error {
@@ -73,9 +77,7 @@ func (s *Service) Register() error {
Address: s.Address, Address: s.Address,
Port: s.port, Port: s.port,
Tags: s.getTags(), Tags: s.getTags(),
Connect: &consul.AgentServiceConnect{ // Connect: &consul.AgentServiceConnect{Native: true},
Native: true,
},
// Proxy: &consul.AgentServiceConnectProxyConfig{ // Proxy: &consul.AgentServiceConnectProxyConfig{
// DestinationServiceName: s.Name, // DestinationServiceName: s.Name,
// }, // },
@@ -107,7 +109,7 @@ func (s *Service) RegisterHealthChecks() {
}() }()
go func() { // TTL go func() { // TTL
interval := s.ttl - time.Second*2 // 2 seconds overhead interval := s.ttl - (time.Second * 2) // 2 seconds overhead
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 {
@@ -118,11 +120,11 @@ func (s *Service) RegisterHealthChecks() {
} }
func (s *Service) Connect() (*connect.Service, error) { func (s *Service) Connect() (*connect.Service, error) {
l := hclog.New(&hclog.LoggerOptions{ // l := hclog.New(&hclog.LoggerOptions{
Name: "consul-registry", // Name: "consul-registry",
Level: hclog.Trace, // Level: hclog.Trace,
}) // })
svc, err := connect.NewServiceWithLogger(s.Name, s.client, l) svc, err := connect.NewService(s.Name, s.client)
s.connect = svc s.connect = svc
cnf := svc.ServerTLSConfig() cnf := svc.ServerTLSConfig()
fmt.Printf("CONNECT SERVER:: %s CONFIG:: %v\n", s.Name, cnf) fmt.Printf("CONNECT SERVER:: %s CONFIG:: %v\n", s.Name, cnf)
@@ -147,17 +149,14 @@ func (s *Service) healthCheck() (bool, error) {
} }
req.Header.Set("User-Agent", "service/internal") req.Header.Set("User-Agent", "service/internal")
fmt.Printf("Sending HEALTH CHECK request to: %s\n", healthUrl)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
fmt.Printf("Sending HEALTH CHECK request error: %v\n", err)
return false return false
} }
defer resp.Body.Close() defer resp.Body.Close()
var body []byte var body []byte
resp.Body.Read(body) resp.Body.Read(body)
fmt.Printf("HEALTH CHECK response to: %v -- %v\n", resp, body)
return resp.StatusCode == http.StatusOK return resp.StatusCode == http.StatusOK
}() }()
@@ -181,7 +180,7 @@ func (s *Service) getTags() []string {
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)", "traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)",
"traefik.http.routers." + s.Name + ".entryPoints=https", "traefik.http.routers." + s.Name + ".entryPoints=https",
"traefik.http.routers." + s.Name + ".tls=true", // "traefik.http.routers." + s.Name + ".tls=true",
"traefik.http.routers." + s.Name + ".service=" + s.Name, "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.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.scheme=http",