Compare commits
3 Commits
v0.1.64
...
sh_24-04-1
| Author | SHA1 | Date | |
|---|---|---|---|
| 359a89ba62 | |||
| 3b73091cfb | |||
| c8beab76f1 |
@@ -9,7 +9,6 @@ import (
|
||||
"git.pbiernat.dev/egommerce/go-api-pkg/config"
|
||||
consul "github.com/hashicorp/consul/api"
|
||||
"github.com/hashicorp/consul/connect"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -62,7 +61,12 @@ func (s *Service) GetID() 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 {
|
||||
@@ -73,9 +77,7 @@ func (s *Service) Register() error {
|
||||
Address: s.Address,
|
||||
Port: s.port,
|
||||
Tags: s.getTags(),
|
||||
Connect: &consul.AgentServiceConnect{
|
||||
Native: true,
|
||||
},
|
||||
// Connect: &consul.AgentServiceConnect{Native: true},
|
||||
// Proxy: &consul.AgentServiceConnectProxyConfig{
|
||||
// DestinationServiceName: s.Name,
|
||||
// },
|
||||
@@ -107,7 +109,7 @@ func (s *Service) RegisterHealthChecks() {
|
||||
}()
|
||||
|
||||
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)
|
||||
for range ticker.C {
|
||||
if _, err := s.healthCheck(); err != nil {
|
||||
@@ -118,11 +120,11 @@ func (s *Service) RegisterHealthChecks() {
|
||||
}
|
||||
|
||||
func (s *Service) Connect() (*connect.Service, error) {
|
||||
l := hclog.New(&hclog.LoggerOptions{
|
||||
Name: "consul-registry",
|
||||
Level: hclog.Trace,
|
||||
})
|
||||
svc, err := connect.NewServiceWithLogger(s.Name, s.client, l)
|
||||
// l := hclog.New(&hclog.LoggerOptions{
|
||||
// Name: "consul-registry",
|
||||
// Level: hclog.Trace,
|
||||
// })
|
||||
svc, err := connect.NewService(s.Name, s.client)
|
||||
s.connect = svc
|
||||
cnf := svc.ServerTLSConfig()
|
||||
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")
|
||||
|
||||
fmt.Printf("Sending HEALTH CHECK request to: %s\n", healthUrl)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Printf("Sending HEALTH CHECK request error: %v\n", err)
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var body []byte
|
||||
resp.Body.Read(body)
|
||||
fmt.Printf("HEALTH CHECK response to: %v -- %v\n", resp, body)
|
||||
|
||||
return resp.StatusCode == http.StatusOK
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user