Compare commits

...

8 Commits
main ... v0.0.2

Author SHA1 Message Date
bd2f05e922 add push-tag.sh script 2022-12-01 09:56:35 +01:00
78f3ba4318 fix 2022-12-01 09:45:23 +01:00
baf252ee0a Fix consul registration 2022-12-01 09:39:17 +01:00
e5786be62b backend server url fix 2022-12-01 09:24:11 +01:00
71379b1729 Consul update ttl fix 2022-12-01 09:08:41 +01:00
67f43f98cb update 2022-12-01 08:36:44 +01:00
840974be0c consul fix 2022-12-01 08:20:27 +01:00
4050a3a13d consul fix 2022-12-01 08:10:54 +01:00
3 changed files with 38 additions and 23 deletions

View File

@ -1,3 +1,5 @@
# go-api-pkg # go-api-pkg
Base API go packages. Base API go packages.
Remeber to tag as "dev" after changes are made in develop branch.

View File

@ -4,15 +4,17 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strconv" "os"
"time" "time"
consul "github.com/hashicorp/consul/api" consul "github.com/hashicorp/consul/api"
) )
type Service struct { type Service struct {
AppID string
Name string Name string
Address string Address string
IP string
Port int Port int
TTL time.Duration TTL time.Duration
ConsulAgent *consul.Agent ConsulAgent *consul.Agent
@ -20,10 +22,14 @@ type Service struct {
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable") var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
func NewService(serverAddr, appName, appDomain string, appPort int) (*Service, error) { func NewService(serverAddr, appID, appName, ip, domain string, appPort int) (*Service, error) {
hostname, _ := os.Hostname()
fmt.Println("hostname:" + hostname)
s := new(Service) s := new(Service)
s.AppID = appID
s.Name = appName s.Name = appName
s.Address = appDomain s.Address = domain
s.IP = ip
s.Port = appPort s.Port = appPort
s.TTL = time.Second * 15 s.TTL = time.Second * 15
@ -45,9 +51,9 @@ func newClientConfig(serverAddr string) *consul.Config {
func (s *Service) Register() error { func (s *Service) Register() error {
def := &consul.AgentServiceRegistration{ def := &consul.AgentServiceRegistration{
// ID: s.Name, ID: s.AppID,
Name: s.Name, Name: s.Name,
Address: s.Address, Address: s.IP,
Port: s.Port, Port: s.Port,
Tags: s.getTags(), Tags: s.getTags(),
Check: &consul.AgentServiceCheck{ Check: &consul.AgentServiceCheck{
@ -63,7 +69,7 @@ func (s *Service) Register() error {
return nil return nil
} }
func (s *Service) Unregister() error { func (s *Service) Unregister() error {
return s.ConsulAgent.ServiceDeregister(s.Name) return s.ConsulAgent.ServiceDeregister(s.AppID)
} }
func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) { func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
@ -71,11 +77,11 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
for range ticker.C { for range ticker.C {
ok, err := s.check() ok, err := s.check()
if !ok { if !ok {
if err := s.ConsulAgent.FailTTL("service:"+s.Name, err.Error()); err != nil { if err := s.ConsulAgent.FailTTL("service:"+s.AppID, err.Error()); err != nil {
log.Println(err) log.Println(err)
} }
} else { } else {
if err := s.ConsulAgent.PassTTL("service:"+s.Name, "OK"); err != nil { if err := s.ConsulAgent.PassTTL("service:"+s.AppID, "OK"); err != nil {
log.Println(err) log.Println(err)
} }
} }
@ -84,7 +90,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.Address, s.Port) healthUrl := fmt.Sprintf("http://%s:%d/health", s.IP, s.Port)
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
@ -105,24 +111,24 @@ func (s *Service) check() (bool, error) {
} }
func (s *Service) getTags() []string { func (s *Service) getTags() []string {
name, addr, port := s.Name, s.Address, strconv.Itoa(s.Port) 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{ tags := []string{
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers." + name + ".rule=Host(`" + addr + "`)", "traefik.http.routers." + s.Name + ".rule=Host(`" + s.Address + "`)",
"traefik.http.routers." + name + ".entryPoints=https", "traefik.http.routers." + s.Name + ".entryPoints=https",
"traefik.http.routers." + name + ".service=" + name, "traefik.http.routers." + s.Name + ".service=" + s.Name,
"traefik.http.routers." + name + ".middlewares=compress,requestid", "traefik.http.routers." + s.Name + ".middlewares=compress,requestid",
"traefik.http.routers." + name + ".tls=true", "traefik.http.routers." + s.Name + ".tls=true",
"traefik.http.services." + name + ".loadbalancer.server.scheme=http", // "traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
"traefik.http.services." + name + ".loadbalancer.server.port=" + port, // "traefik.http.services." + s.Name + ".loadbalancer.server.port=" + port,
"traefik.http.services." + name + ".loadbalancer.passhostheader=false", "traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
"traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + "=" + bFullAddr,
"traefik.http.middlewares.compress.compress=true", "traefik.http.middlewares.compress.compress=true",
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID", "traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
// "traefik.http.services." + name + ".loadbalancer.healthcheck.path=/health", // "traefik.http.services." + fullName + ".loadbalancer.healthcheck.path=/health",
// "traefik.http.services." + name + ".loadbalancer.healthcheck.interval=10s", // "traefik.http.services." + fullName + ".loadbalancer.healthcheck.interval=10s",
// "traefik.tls.certificates.certfile=/certs/client.cert",
// "traefik.tls.certificates.keyfile=/certs/client.key",
} }
return tags return tags

7
push-tag.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
git tag -d dev
git push origin develop
git tag dev
git push origin dev --force