Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 974f82e9be | |||
| d7dc75c18f | |||
| c62c63249c | |||
| 6c290eb66b | |||
| 31ce7fc48e | |||
| 99957861dc | |||
| 19d27b4ff6 | |||
| 7f2025ef6f | |||
| 3afe78f9e3 | |||
| eb763ed49c | |||
| 6537d79b19 | |||
| e55a2f42fe | |||
| 82d68e91b8 | |||
| 767eb5688c | |||
| 37fa05402e | |||
| 615281ae9a | |||
| 42532b1d44 | |||
| 1d4164711e | |||
| a50778380e | |||
| 159bce1ff2 | |||
| 22bc1e29a3 | |||
| 4ce9064fbb | |||
| 75f7a75379 | |||
| 398d7a2074 | |||
| b699008fa3 | |||
| b1f04badd5 |
@@ -17,26 +17,35 @@ type Service struct {
|
|||||||
appID string
|
appID string
|
||||||
domain string
|
domain string
|
||||||
pathPrefix string
|
pathPrefix string
|
||||||
|
tls bool
|
||||||
port int
|
port int
|
||||||
ttl time.Duration
|
ttl time.Duration
|
||||||
client *consul.Client
|
client *consul.Client
|
||||||
agent *consul.Agent
|
agent *consul.Agent
|
||||||
connect *connect.Service
|
connect *connect.Service
|
||||||
kv *consul.KV
|
kv *consul.KV
|
||||||
|
|
||||||
|
hcTicker *time.Ticker
|
||||||
|
ttlTicker *time.Ticker
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
||||||
|
|
||||||
func NewService(servAddr, id, name, hostname, domain, pathPrefix string, appPort int) (*Service, error) {
|
func NewService(servAddr, id, name, useDomainOverIp, addr, domain, pathPrefix string, appPort int) (*Service, error) {
|
||||||
s := new(Service)
|
s := new(Service)
|
||||||
s.Name = name
|
s.Name = name
|
||||||
s.Address = hostname
|
s.Address = addr
|
||||||
s.appID = id
|
s.appID = id
|
||||||
s.domain = domain
|
s.domain = domain
|
||||||
s.pathPrefix = pathPrefix
|
s.pathPrefix = pathPrefix
|
||||||
|
s.tls = true // FIXME add arg
|
||||||
s.port = appPort
|
s.port = appPort
|
||||||
s.ttl = time.Second * 10
|
s.ttl = time.Second * 10
|
||||||
|
|
||||||
|
if useDomainOverIp == "true" { // FIXME types...
|
||||||
|
s.Address = domain
|
||||||
|
}
|
||||||
|
|
||||||
client, err := consul.NewClient(newClientConfig(servAddr))
|
client, err := consul.NewClient(newClientConfig(servAddr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -61,9 +70,8 @@ func (s *Service) GetID() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetFullAddr() string {
|
func (s *Service) GetFullAddr() string {
|
||||||
isTLS := s.port == 443
|
|
||||||
proto := "http"
|
proto := "http"
|
||||||
if isTLS {
|
if s.tls {
|
||||||
proto = "https"
|
proto = "https"
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s://%s:%d/", proto, s.domain, s.port)
|
return fmt.Sprintf("%s://%s:%d/", proto, s.domain, s.port)
|
||||||
@@ -95,25 +103,33 @@ func (s *Service) Register() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s *Service) Unregister() error {
|
func (s *Service) Unregister() error {
|
||||||
|
// s.ttlTicker.Stop()
|
||||||
|
// s.hcTicker.Stop()
|
||||||
|
|
||||||
|
s.client.Catalog().Deregister(&consul.CatalogDeregistration{
|
||||||
|
Address: s.Address,
|
||||||
|
ServiceID: s.GetID(),
|
||||||
|
}, nil)
|
||||||
|
|
||||||
return s.agent.ServiceDeregister(s.GetID())
|
return s.agent.ServiceDeregister(s.GetID())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) RegisterHealthChecks() {
|
func (s *Service) RegisterHealthChecks() {
|
||||||
go func() { // startup register
|
go func() { // startup register
|
||||||
ticker := time.NewTicker(time.Second * 1)
|
t := time.NewTicker(time.Second)
|
||||||
for range ticker.C {
|
for range t.C {
|
||||||
if ok, _ := s.healthCheck(); ok {
|
if ok, _ := s.healthCheck(); ok {
|
||||||
ticker.Stop()
|
t.Stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() { // TTL
|
go func() { // TTL
|
||||||
interval := s.ttl - (time.Second * 2) // 2 seconds overhead
|
t := time.NewTicker(s.ttl)
|
||||||
ticker := time.NewTicker(interval)
|
for range t.C {
|
||||||
for range ticker.C {
|
|
||||||
if _, err := s.healthCheck(); err != nil {
|
if _, err := s.healthCheck(); err != nil {
|
||||||
fmt.Printf("HealthCheck endpoint not available #: %v\n", err)
|
// fmt.Printf("HealthCheck endpoint not available (%s)#: %v\n", s.GetFullAddr(), err)
|
||||||
|
t.Stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -143,6 +159,7 @@ func (s *Service) healthCheck() (bool, error) {
|
|||||||
alive := func() bool {
|
alive := func() bool {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
healthUrl := fmt.Sprintf("%s%s?name=%s", s.GetFullAddr(), "health", s.Name)
|
healthUrl := fmt.Sprintf("%s%s?name=%s", s.GetFullAddr(), "health", s.Name)
|
||||||
|
fmt.Printf("HealthCheck URL: %s%s?name=%s", s.GetFullAddr(), "health", s.Name)
|
||||||
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
@@ -181,23 +198,26 @@ func (s *Service) getTags() []string {
|
|||||||
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.trustForwardHeader=true",
|
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.trustForwardHeader=true",
|
||||||
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.authRequestHeaders=Cookie",
|
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.authRequestHeaders=Cookie",
|
||||||
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.authResponseHeaders=Set-Cookie, Server",
|
// "traefik.http.middlewares.auth_" + s.Name + ".forwardauth.authResponseHeaders=Set-Cookie, Server",
|
||||||
"traefik.http.middlewares.auth_" + s.Name + ".plugin.auth.handlerURL=" + config.GetEnv("AUTH_HANDLER_URL", "http://identity.service.ego.io/api/v1/traefik"),
|
"traefik.http.middlewares.auth_" + s.Name + ".plugin.auth.handlerURL=" + config.GetEnv("AUTH_HANDLER_URL", "https://identity.service.ego.io/api/v1/traefik"),
|
||||||
"traefik.http.middlewares.requestid_" + s.Name + ".plugin.requestid.headerName=X-Request-ID",
|
|
||||||
"traefik.http.middlewares.stripprefix_" + s.Name + ".stripprefix.prefixes=" + s.pathPrefix,
|
"traefik.http.middlewares.stripprefix_" + s.Name + ".stripprefix.prefixes=" + s.pathPrefix,
|
||||||
|
"traefik.http.middlewares.requestid_" + s.Name + ".plugin.requestid.headerName=X-Request-ID",
|
||||||
|
// "treafik.http.middlewares.retryif_" + s.Name + ".plugin.retryif.attempts=3",
|
||||||
|
// "treafik.http.middlewares.retryif_" + s.Name + ".plugin.retryif.statusCode=503",
|
||||||
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)",
|
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)",
|
||||||
// "traefik.http.routers." + s.Name + ".rule=Host(`" + s.domain + "`)",
|
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.domain + "`)",
|
||||||
"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 + ",stripprefix_" + s.Name,
|
// "traefik.http.routers." + s.Name + ".middlewares=auth_" + s.Name + ",stripprefix_" + 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 + ",stripprefix_" + s.Name + ",requestid_" + s.Name + "",
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=https",
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.port),
|
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.port),
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=true",
|
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=true",
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=1",
|
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=1s",
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.timeout=1",
|
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.timeout=1s",
|
||||||
"traefik.tls.certificates.certfile=/certs/client.cert",
|
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.path=/health",
|
||||||
"traefik.tls.certificates.keyfile=/certs/client.key",
|
"traefik.tls.certificates.certfile=certs/client.cert",
|
||||||
|
"traefik.tls.certificates.keyfile=certs/client.key",
|
||||||
}
|
}
|
||||||
|
|
||||||
return tags
|
return tags
|
||||||
|
|||||||
Reference in New Issue
Block a user