Added KV Config support and updated build scripts

This commit is contained in:
PB
2022-12-05 21:34:45 +01:00
parent 38d7d1ab86
commit 09ecea4d1a
9 changed files with 73 additions and 22 deletions

View File

@@ -3,18 +3,24 @@ package server
import "fmt"
type Config struct {
AppID string
AppName string
AppDomain string
NetAddr string
Port int
LoggerAddr string
RegistryAddr string
DbURL string
MongoDbUrl string
EventBusURL string
EventBusExchange string
EventBusQueue string
AppID string
AppName string
AppDomain string
NetAddr string
Port int
RegistryAddr string
KVNamespace string
LoggerAddr string `json:"logger_addr"`
DbURL string `json:"db_url"`
MongoDbUrl string `json:"mongodb_url"`
EventBusURL string `json:"eventbus_url"`
EventBusExchange string `json:"eventbus_exchange"`
EventBusQueue string `json:"eventbus_queue"`
HttpReadTimeout int `json:"http_read_timeout"`
HttpWriteTimeout int `json:"http_write_timeout"`
HttpIdleTimeout int `json:"http_idle_timeout"`
// Fields with json mapping are available trough ConsulKV
}
func (c *Config) GetAppFullName() string {

View File

@@ -10,3 +10,7 @@ func (s *Server) HealthHandler(c *fiber.Ctx) error {
Status: "OK",
})
}
func (s *Server) ConfigHandler(c *fiber.Ctx) error {
return c.JSON(s.conf)
}

View File

@@ -9,6 +9,7 @@ import (
func SetupRoutes(s *Server) {
s.App.Get("/health", s.HealthHandler)
s.App.Get("/config", s.ConfigHandler)
api := s.App.Group("/api")
v1 := api.Group("/v1")

View File

@@ -1,6 +1,9 @@
package server
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/signal"
"syscall"
@@ -15,12 +18,13 @@ import (
type Server struct {
*fiber.App
log *fluentd.Logger
db *pgxpool.Pool
// ebCh *amqp.Channel
conf *Config
log *fluentd.Logger
db *pgxpool.Pool
discovery *discovery.Service
name string
addr string
kvNmspc string
}
type Headers struct {
@@ -29,13 +33,13 @@ type Headers struct {
func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool /*, ebCh *amqp.Channel*/) *Server {
logger.Log("API_ID: %s", conf.AppID)
discovery, err := discovery.NewService(conf.RegistryAddr, conf.AppID, conf.AppName, conf.AppID, conf.AppDomain, conf.Port)
consul, err := discovery.NewService(conf.RegistryAddr, conf.AppID, conf.AppName, conf.AppID, conf.AppDomain, conf.Port)
if err != nil {
logger.Log("Error connecting to %s: %v", conf.RegistryAddr, err)
}
logger.Log("Registering service with name: %s, address: %s", discovery.Name, discovery.Address)
err = discovery.Register()
logger.Log("Registering service with name: %s, address: %s", consul.Name, consul.Address)
err = consul.Register()
if err != nil {
logger.Log("register error: %v", err)
}
@@ -49,14 +53,27 @@ func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool /*, ebCh *
}
s := &Server{
fiber.New(cnf),
conf,
logger,
db,
/*ebCh,*/
discovery,
consul,
conf.AppName,
conf.NetAddr,
conf.KVNamespace,
}
go func(s *Server) { // Consul KV config updater
interval := time.Second * 30
ticker := time.NewTicker(interval)
for range ticker.C {
err := s.updateKVConfig()
if err != nil {
logger.Log("KV config update error (skipping): %v\n", err)
}
}
}(s)
SetupMiddlewares(s)
SetupRoutes(s)
@@ -98,6 +115,23 @@ func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) {
return hdr.RequestID, nil
}
func (s *Server) updateKVConfig() error {
config, _, err := s.discovery.KV().Get(s.kvNmspc, nil)
if err != nil {
fmt.Println(err)
return err
}
buf := bytes.NewBuffer(config.Value)
decoder := json.NewDecoder(buf)
if err := decoder.Decode(&s.conf); err != nil {
return err
}
return nil
}
func (s *Server) gracefulShutdown() error {
s.log.Log("Server is going down...")
s.log.Log("Unregistering service: %s", s.discovery.GetID())