Compare commits

...

11 Commits

Author SHA1 Message Date
c62c63249c debug fix 2024-07-20 14:44:45 +02:00
6c290eb66b tls fix 2024-07-20 14:43:47 +02:00
31ce7fc48e tls fix 2024-07-20 14:42:16 +02:00
99957861dc fix ssl support 2024-07-20 14:24:38 +02:00
19d27b4ff6 add ssl support 2024-07-20 14:20:01 +02:00
7f2025ef6f add debug point 2024-07-20 14:18:08 +02:00
3afe78f9e3 fix in consul discovery 2024-07-19 17:06:39 +02:00
eb763ed49c fix in consul discovery 2024-07-19 16:52:54 +02:00
6537d79b19 fix in consul discovery 2024-07-19 16:48:08 +02:00
e55a2f42fe fix in consul discovery 2024-07-19 16:45:42 +02:00
82d68e91b8 fix in consul discovery 2024-07-19 16:41:37 +02:00

View File

@@ -17,6 +17,7 @@ type Service struct {
appID string
domain string
pathPrefix string
tls bool
port int
ttl time.Duration
client *consul.Client
@@ -37,6 +38,7 @@ func NewService(servAddr, id, name, useDomainOverIp, addr, domain, pathPrefix st
s.appID = id
s.domain = domain
s.pathPrefix = pathPrefix
s.tls = true // FIXME add arg
s.port = appPort
s.ttl = time.Second * 10
@@ -68,9 +70,8 @@ func (s *Service) GetID() string {
}
func (s *Service) GetFullAddr() string {
isTLS := s.port == 443
proto := "http"
if isTLS {
if s.tls {
proto = "https"
}
return fmt.Sprintf("%s://%s:%d/", proto, s.Address, s.port)
@@ -102,32 +103,33 @@ func (s *Service) Register() error {
return nil
}
func (s *Service) Unregister() error {
// s.ttlTicker.Stop()
// s.hcTicker.Stop()
s.client.Catalog().Deregister(&consul.CatalogDeregistration{
Address: s.Address,
ServiceID: s.GetID(),
}, nil)
s.ttlTicker.Stop()
return s.agent.ServiceDeregister(s.GetID())
}
func (s *Service) RegisterHealthChecks() {
go func() { // startup register
s.hcTicker = time.NewTicker(time.Second)
for range s.hcTicker.C {
t := time.NewTicker(time.Second)
for range t.C {
if ok, _ := s.healthCheck(); ok {
s.hcTicker.Stop()
t.Stop()
}
}
}()
go func() { // TTL
s.ttlTicker = time.NewTicker(s.ttl)
for range s.ttlTicker.C {
t := time.NewTicker(s.ttl)
for range t.C {
if _, err := s.healthCheck(); err != nil {
fmt.Printf("HealthCheck endpoint not available (%s)#: %v\n", s.GetFullAddr(), err)
// s.ttlTicker.Stop()
// fmt.Printf("HealthCheck endpoint not available (%s)#: %v\n", s.GetFullAddr(), err)
t.Stop()
}
}
}()
@@ -157,6 +159,7 @@ func (s *Service) healthCheck() (bool, error) {
alive := func() bool {
client := &http.Client{}
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)
if err != nil {
return false
@@ -195,26 +198,26 @@ func (s *Service) getTags() []string {
// "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.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.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=Host(`" + s.domain + "`)",
"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 + ".tls=true",
"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 + ",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.passhostheader=true",
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=1s",
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.timeout=1s",
"traefik.http.services." + s.Name + ".loadbalancer.healthcheck.path=/health",
"traefik.tls.certificates.certfile=/certs/client.cert",
"traefik.tls.certificates.keyfile=/certs/client.key",
"traefik.tls.certificates.certfile=certs/client.cert",
"traefik.tls.certificates.keyfile=certs/client.key",
}
return tags