Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2204f6116b | |||
| 65dbbc16df | |||
| e5c95b0476 | |||
| 34ace207a7 | |||
| ad4368cdf8 | |||
| a4f4b6e059 | |||
| 5bf58aad16 | |||
| ba6bd4137c | |||
| 48a7e968d1 | |||
| 6c46a0e6a4 | |||
| ceaeecf1b4 | |||
| 0b035dfcc9 |
@@ -13,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
|
||||
@@ -22,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 = appName
|
||||
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
|
||||
}
|
||||
@@ -51,13 +51,18 @@ func (s *Service) GetID() string {
|
||||
return fmt.Sprintf("%s_%s", s.Name, s.AppID)
|
||||
}
|
||||
|
||||
func (s *Service) GetFullAddr() string {
|
||||
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(),
|
||||
Check: &consul.AgentServiceCheck{
|
||||
TTL: s.TTL.String(),
|
||||
},
|
||||
@@ -81,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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,7 +99,8 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
|
||||
|
||||
func (s *Service) check() (bool, error) {
|
||||
client := &http.Client{}
|
||||
healthUrl := fmt.Sprintf("http://%s:%d/health", s.IP, s.Port)
|
||||
healthUrl := fmt.Sprintf("%shealth", s.GetFullAddr())
|
||||
fmt.Println("HCheck: " + healthUrl)
|
||||
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
||||
if err != nil {
|
||||
return false, ErrServiceUnavailable
|
||||
@@ -112,13 +120,16 @@ func (s *Service) check() (bool, error) {
|
||||
return false, ErrServiceUnavailable
|
||||
}
|
||||
|
||||
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....
|
||||
func (s *Service) getMetadata() map[string]string {
|
||||
m := map[string]string{}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (s *Service) getTags() []string {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user