Compare commits

...

2 Commits

Author SHA1 Message Date
ceaeecf1b4 register fix 2022-12-01 19:43:35 +01:00
0b035dfcc9 register fix 2022-12-01 19:34:10 +01:00

View File

@@ -5,6 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time" "time"
consul "github.com/hashicorp/consul/api" consul "github.com/hashicorp/consul/api"
@@ -51,13 +52,18 @@ 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 {
return fmt.Sprintf("http://%s:%d/", s.IP, s.Port)
}
func (s *Service) Register() error { func (s *Service) Register() error {
def := &consul.AgentServiceRegistration{ def := &consul.AgentServiceRegistration{
ID: s.GetID(), ID: s.GetID(),
Name: s.Name, Name: strings.Replace(s.Name, "-", "_", -1),
Address: s.IP, Address: s.IP,
Port: s.Port, Port: s.Port,
Tags: s.getTags(), Tags: s.getTags(),
Meta: s.getMetadata(),
Check: &consul.AgentServiceCheck{ Check: &consul.AgentServiceCheck{
TTL: s.TTL.String(), TTL: s.TTL.String(),
}, },
@@ -92,7 +98,7 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
func (s *Service) check() (bool, error) { func (s *Service) check() (bool, error) {
client := &http.Client{} client := &http.Client{}
healthUrl := fmt.Sprintf("http://%s:%d/health", s.IP, s.Port) healthUrl := fmt.Sprintf("http://%s/health", s.GetFullAddr())
req, err := http.NewRequest(http.MethodGet, healthUrl, nil) req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
if err != nil { if err != nil {
return false, ErrServiceUnavailable return false, ErrServiceUnavailable
@@ -112,6 +118,13 @@ func (s *Service) check() (bool, error) {
return false, ErrServiceUnavailable return false, ErrServiceUnavailable
} }
func (s *Service) getMetadata() map[string]string {
m := map[string]string{}
m["traefik/http/services/"+s.Name+"/loadBalancer/servers/0/url"] = s.GetFullAddr()
return m
}
func (s *Service) getTags() []string { func (s *Service) getTags() []string {
// fullName := fmt.Sprintf("%s-%s", s.Name, s.AppID) // fullName := fmt.Sprintf("%s-%s", s.Name, s.AppID)
// bFullAddr := fmt.Sprintf("http://%s:%d/", s.IP, s.Port) // FIXME: declare one once - dont need to refresh.... // bFullAddr := fmt.Sprintf("http://%s:%d/", s.IP, s.Port) // FIXME: declare one once - dont need to refresh....