Compare commits

..

No commits in common. "v0.0.3" and "main" have entirely different histories.
v0.0.3 ... main

3 changed files with 23 additions and 35 deletions

View File

@ -1,5 +1,3 @@
# 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,16 +4,15 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strconv"
"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
@ -21,12 +20,10 @@ type Service struct {
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable") var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
func NewService(serverAddr, appID, appName, ip, domain string, appPort int) (*Service, error) { func NewService(serverAddr, appName, appDomain string, appPort int) (*Service, error) {
s := new(Service) s := new(Service)
s.AppID = appID
s.Name = appName s.Name = appName
s.Address = domain s.Address = appDomain
s.IP = ip
s.Port = appPort s.Port = appPort
s.TTL = time.Second * 15 s.TTL = time.Second * 15
@ -48,9 +45,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.AppID, // ID: s.Name,
Name: s.Name, Name: s.Name,
Address: s.IP, Address: s.Address,
Port: s.Port, Port: s.Port,
Tags: s.getTags(), Tags: s.getTags(),
Check: &consul.AgentServiceCheck{ Check: &consul.AgentServiceCheck{
@ -66,7 +63,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.AppID) return s.ConsulAgent.ServiceDeregister(s.Name)
} }
func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) { func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
@ -74,11 +71,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.AppID, err.Error()); err != nil { if err := s.ConsulAgent.FailTTL("service:"+s.Name, err.Error()); err != nil {
log.Println(err) log.Println(err)
} }
} else { } else {
if err := s.ConsulAgent.PassTTL("service:"+s.AppID, "OK"); err != nil { if err := s.ConsulAgent.PassTTL("service:"+s.Name, "OK"); err != nil {
log.Println(err) log.Println(err)
} }
} }
@ -87,7 +84,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.IP, s.Port) healthUrl := fmt.Sprintf("http://%s:%d/health", s.Address, 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
@ -108,24 +105,24 @@ func (s *Service) check() (bool, error) {
} }
func (s *Service) getTags() []string { func (s *Service) getTags() []string {
fullName := fmt.Sprintf("%s-%s", s.Name, s.AppID) name, addr, port := s.Name, s.Address, strconv.Itoa(s.Port)
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." + s.Name + ".rule=Host(`" + s.Address + "`)", "traefik.http.routers." + name + ".rule=Host(`" + addr + "`)",
"traefik.http.routers." + s.Name + ".entryPoints=https", "traefik.http.routers." + name + ".entryPoints=https",
"traefik.http.routers." + s.Name + ".service=" + s.Name, "traefik.http.routers." + name + ".service=" + name,
"traefik.http.routers." + s.Name + ".middlewares=compress,requestid", "traefik.http.routers." + name + ".middlewares=compress,requestid",
"traefik.http.routers." + s.Name + ".tls=true", "traefik.http.routers." + name + ".tls=true",
// "traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http", "traefik.http.services." + name + ".loadbalancer.server.scheme=http",
// "traefik.http.services." + s.Name + ".loadbalancer.server.port=" + port, "traefik.http.services." + name + ".loadbalancer.server.port=" + port,
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false", "traefik.http.services." + 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." + fullName + ".loadbalancer.healthcheck.path=/health", // "traefik.http.services." + name + ".loadbalancer.healthcheck.path=/health",
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.interval=10s", // "traefik.http.services." + name + ".loadbalancer.healthcheck.interval=10s",
// "traefik.tls.certificates.certfile=/certs/client.cert",
// "traefik.tls.certificates.keyfile=/certs/client.key",
} }
return tags return tags

View File

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