Added KV Config support and updated build scripts
All checks were successful
continuous-integration/drone/push Build is passing

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

View File

@ -10,7 +10,7 @@ export DOCKER_BUILDKIT=1
TARGET=${1:-latest} TARGET=${1:-latest}
[ ! -d \"src/vendor\" ] && sh -c "cd src; go mod vendor" [ ! -d "src/vendor" ] && sh -c "cd src; go mod vendor"
echo "Building tmp $BUILDER_IMAGE image..." echo "Building tmp $BUILDER_IMAGE image..."
docker build --rm -t $BUILDER_IMAGE -f Dockerfile.builder . docker build --rm -t $BUILDER_IMAGE -f Dockerfile.builder .

View File

@ -7,6 +7,6 @@ export SERVER_IMAGE="$IMAGE_BASE-svc"
TARGET=${1:-latest} TARGET=${1:-latest}
docker login git.pbiernat.dev -u $DOCKER_USERNAME -p $DOCKER_PASSWORD echo $DOCKER_PASSWORD | docker login git.pbiernat.dev -u $DOCKER_USERNAME --password-stdin
docker push "$SERVER_IMAGE:$TARGET" docker push "$SERVER_IMAGE:$TARGET"
# docker push "$WORKER_IMAGE:$TARGET" # docker push "$WORKER_IMAGE:$TARGET"

View File

@ -22,6 +22,7 @@ const (
defEventBusURL = "amqp://guest:guest@api-eventbus:5672" defEventBusURL = "amqp://guest:guest@api-eventbus:5672"
ebEventsExchange = "api-events" ebEventsExchange = "api-events"
ebEventsQueue = "identity-svc" ebEventsQueue = "identity-svc"
defKVNmspc = "dev.egommerce/service/identity-svc"
) )
func main() { func main() {
@ -40,6 +41,7 @@ func main() {
c.DbURL = config.GetEnv("DATABASE_URL", defDbURL) c.DbURL = config.GetEnv("DATABASE_URL", defDbURL)
c.EventBusURL = config.GetEnv("EVENTBUS_URL", defEventBusURL) c.EventBusURL = config.GetEnv("EVENTBUS_URL", defEventBusURL)
c.EventBusExchange = ebEventsExchange c.EventBusExchange = ebEventsExchange
c.KVNamespace = config.GetEnv("APP_KV_NAMESPACE", defKVNmspc)
logHost, logPort := fluentd.ParseAddr(c.LoggerAddr) logHost, logPort := fluentd.ParseAddr(c.LoggerAddr)
logger := fluentd.NewLogger(c.GetAppFullName(), logHost, logPort) logger := fluentd.NewLogger(c.GetAppFullName(), logHost, logPort)

View File

@ -3,7 +3,7 @@ module git.pbiernat.dev/egommerce/identity-service
go 1.18 go 1.18
require ( require (
git.pbiernat.dev/egommerce/go-api-pkg v0.0.106 git.pbiernat.dev/egommerce/go-api-pkg v0.0.108
github.com/gofiber/fiber/v2 v2.40.1 github.com/gofiber/fiber/v2 v2.40.1
github.com/gofiber/jwt/v2 v2.2.7 github.com/gofiber/jwt/v2 v2.2.7
github.com/golang-jwt/jwt v3.2.2+incompatible github.com/golang-jwt/jwt v3.2.2+incompatible

View File

@ -6,6 +6,10 @@ git.pbiernat.dev/egommerce/go-api-pkg v0.0.105 h1:8w4p4QNaSF58iL3YiGvqXC4UjUVeeu
git.pbiernat.dev/egommerce/go-api-pkg v0.0.105/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8= git.pbiernat.dev/egommerce/go-api-pkg v0.0.105/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.106 h1:kOqDvQfk8MzmyQonMMLmZKhW7I5YeDyw6N8YIYHIVwA= git.pbiernat.dev/egommerce/go-api-pkg v0.0.106 h1:kOqDvQfk8MzmyQonMMLmZKhW7I5YeDyw6N8YIYHIVwA=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.106/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8= git.pbiernat.dev/egommerce/go-api-pkg v0.0.106/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.107 h1:yigpHD40ocyiamWc7GUVPgeaVtmz1VQwOucagwbuQfE=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.107/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.108 h1:gr5kzKNR3sCxTz+nbqtOM7vdIely5ZWb8itSLAjTo0I=
git.pbiernat.dev/egommerce/go-api-pkg v0.0.108/go.mod h1:nAwcw2MZtn/54YKq8VQK6RJAsiuoLUtPuazXg8JcqK8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=

View File

@ -3,18 +3,24 @@ package server
import "fmt" import "fmt"
type Config struct { type Config struct {
AppID string AppID string
AppName string AppName string
AppDomain string AppDomain string
NetAddr string NetAddr string
Port int Port int
LoggerAddr string RegistryAddr string
RegistryAddr string KVNamespace string
DbURL string
MongoDbUrl string LoggerAddr string `json:"logger_addr"`
EventBusURL string DbURL string `json:"db_url"`
EventBusExchange string MongoDbUrl string `json:"mongodb_url"`
EventBusQueue string 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 { func (c *Config) GetAppFullName() string {

View File

@ -10,3 +10,7 @@ func (s *Server) HealthHandler(c *fiber.Ctx) error {
Status: "OK", 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) { func SetupRoutes(s *Server) {
s.App.Get("/health", s.HealthHandler) s.App.Get("/health", s.HealthHandler)
s.App.Get("/config", s.ConfigHandler)
api := s.App.Group("/api") api := s.App.Group("/api")
v1 := api.Group("/v1") v1 := api.Group("/v1")

View File

@ -1,6 +1,9 @@
package server package server
import ( import (
"bytes"
"encoding/json"
"fmt"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@ -15,12 +18,13 @@ import (
type Server struct { type Server struct {
*fiber.App *fiber.App
log *fluentd.Logger conf *Config
db *pgxpool.Pool log *fluentd.Logger
// ebCh *amqp.Channel db *pgxpool.Pool
discovery *discovery.Service discovery *discovery.Service
name string name string
addr string addr string
kvNmspc string
} }
type Headers struct { type Headers struct {
@ -29,13 +33,13 @@ type Headers struct {
func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool /*, ebCh *amqp.Channel*/) *Server { func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool /*, ebCh *amqp.Channel*/) *Server {
logger.Log("API_ID: %s", conf.AppID) 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 { if err != nil {
logger.Log("Error connecting to %s: %v", conf.RegistryAddr, err) logger.Log("Error connecting to %s: %v", conf.RegistryAddr, err)
} }
logger.Log("Registering service with name: %s, address: %s", discovery.Name, discovery.Address) logger.Log("Registering service with name: %s, address: %s", consul.Name, consul.Address)
err = discovery.Register() err = consul.Register()
if err != nil { if err != nil {
logger.Log("register error: %v", err) logger.Log("register error: %v", err)
} }
@ -49,14 +53,27 @@ func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool /*, ebCh *
} }
s := &Server{ s := &Server{
fiber.New(cnf), fiber.New(cnf),
conf,
logger, logger,
db, db,
/*ebCh,*/ /*ebCh,*/
discovery, consul,
conf.AppName, conf.AppName,
conf.NetAddr, 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) SetupMiddlewares(s)
SetupRoutes(s) SetupRoutes(s)
@ -98,6 +115,23 @@ func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) {
return hdr.RequestID, nil 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 { func (s *Server) gracefulShutdown() error {
s.log.Log("Server is going down...") s.log.Log("Server is going down...")
s.log.Log("Unregistering service: %s", s.discovery.GetID()) s.log.Log("Unregistering service: %s", s.discovery.GetID())