Compare commits

..

8 Commits

Author SHA1 Message Date
2204f6116b register fix 2022-12-02 01:20:55 +01:00
65dbbc16df register fix 2022-12-01 23:37:43 +01:00
e5c95b0476 register fix 2022-12-01 23:24:25 +01:00
34ace207a7 register fix 2022-12-01 23:12:53 +01:00
ad4368cdf8 register fix 2022-12-01 22:55:42 +01:00
a4f4b6e059 register fix 2022-12-01 20:28:23 +01:00
5bf58aad16 register fix 2022-12-01 20:26:22 +01:00
ba6bd4137c register fix 2022-12-01 20:19:28 +01:00

View File

@@ -5,7 +5,6 @@ import (
"log"
"net/http"
"strconv"
"strings"
"time"
consul "github.com/hashicorp/consul/api"
@@ -14,8 +13,8 @@ import (
type Service struct {
AppID string
Name string
Domain string
Address string
IP string
Port int
TTL time.Duration
ConsulAgent *consul.Agent
@@ -23,16 +22,16 @@ type Service struct {
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
func NewService(serverAddr, appID, appName, ip, domain string, appPort int) (*Service, error) {
func NewService(servAddr, id, name, hostname, domain string, appPort int) (*Service, error) {
s := new(Service)
s.AppID = appID
s.Name = strings.Replace(appName, "-", "", -1)
s.Address = domain
s.IP = ip
s.AppID = id
s.Name = name
s.Address = hostname
s.Domain = domain
s.Port = appPort
s.TTL = time.Second * 15
client, err := consul.NewClient(newClientConfig(serverAddr))
client, err := consul.NewClient(newClientConfig(servAddr))
if err != nil {
return nil, err
}
@@ -53,17 +52,17 @@ func (s *Service) GetID() string {
}
func (s *Service) GetFullAddr() string {
return fmt.Sprintf("http://%s:%d/", s.IP, s.Port)
return fmt.Sprintf("http://%s:%d/", s.Address, s.Port)
}
func (s *Service) Register() error {
def := &consul.AgentServiceRegistration{
ID: s.GetID(),
Name: s.Name,
Address: s.IP,
Address: s.Address,
Port: s.Port,
Tags: s.getTags(),
Meta: s.getMetadata(),
// Meta: s.getMetadata(),
Check: &consul.AgentServiceCheck{
TTL: s.TTL.String(),
},
@@ -87,10 +86,12 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
if !ok {
if err := s.ConsulAgent.FailTTL("service:"+s.GetID(), err.Error()); err != nil {
log.Println(err)
fmt.Println(err)
}
} else {
if err := s.ConsulAgent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
log.Println(err)
fmt.Println(err)
}
}
}
@@ -98,7 +99,8 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
func (s *Service) check() (bool, error) {
client := &http.Client{}
healthUrl := fmt.Sprintf("http://%s/health", s.GetFullAddr())
healthUrl := fmt.Sprintf("%shealth", s.GetFullAddr())
fmt.Println("HCheck: " + healthUrl)
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
if err != nil {
return false, ErrServiceUnavailable
@@ -120,18 +122,14 @@ func (s *Service) check() (bool, error) {
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 {
// 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....
tags := []string{
"traefik.enable=true",
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Address + "`)",
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Domain + "`)",
"traefik.http.routers." + s.Name + ".entryPoints=https",
"traefik.http.routers." + s.Name + ".tls=true",
"traefik.http.routers." + s.Name + ".service=" + s.Name,