diff --git a/deploy/image-build.sh b/deploy/image-build.sh index 92a72ae..31fab96 100755 --- a/deploy/image-build.sh +++ b/deploy/image-build.sh @@ -10,7 +10,7 @@ export DOCKER_BUILDKIT=1 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..." docker build --rm -t $BUILDER_IMAGE -f Dockerfile.builder . diff --git a/deploy/image-push.sh b/deploy/image-push.sh index c8c4e20..e894638 100755 --- a/deploy/image-push.sh +++ b/deploy/image-push.sh @@ -7,6 +7,6 @@ export SERVER_IMAGE="$IMAGE_BASE-svc" 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 "$WORKER_IMAGE:$TARGET" diff --git a/src/cmd/server/main.go b/src/cmd/server/main.go index 6287dea..175e9ca 100644 --- a/src/cmd/server/main.go +++ b/src/cmd/server/main.go @@ -22,6 +22,7 @@ const ( defEventBusURL = "amqp://guest:guest@api-eventbus:5672" ebEventsExchange = "api-events" ebEventsQueue = "identity-svc" + defKVNmspc = "dev.egommerce/service/identity-svc" ) func main() { @@ -40,6 +41,7 @@ func main() { c.DbURL = config.GetEnv("DATABASE_URL", defDbURL) c.EventBusURL = config.GetEnv("EVENTBUS_URL", defEventBusURL) c.EventBusExchange = ebEventsExchange + c.KVNamespace = config.GetEnv("APP_KV_NAMESPACE", defKVNmspc) logHost, logPort := fluentd.ParseAddr(c.LoggerAddr) logger := fluentd.NewLogger(c.GetAppFullName(), logHost, logPort) diff --git a/src/go.mod b/src/go.mod index e63de6d..c41b53d 100644 --- a/src/go.mod +++ b/src/go.mod @@ -3,7 +3,7 @@ module git.pbiernat.dev/egommerce/identity-service go 1.18 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/jwt/v2 v2.2.7 github.com/golang-jwt/jwt v3.2.2+incompatible diff --git a/src/go.sum b/src/go.sum index 52a8c53..d84f3ae 100644 --- a/src/go.sum +++ b/src/go.sum @@ -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.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.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/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= diff --git a/src/internal/app/server/config.go b/src/internal/app/server/config.go index 9bec52b..9fce58d 100644 --- a/src/internal/app/server/config.go +++ b/src/internal/app/server/config.go @@ -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 { diff --git a/src/internal/app/server/health_handler.go b/src/internal/app/server/health_handler.go index 858ed19..3f8f866 100644 --- a/src/internal/app/server/health_handler.go +++ b/src/internal/app/server/health_handler.go @@ -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) +} diff --git a/src/internal/app/server/router.go b/src/internal/app/server/router.go index 8d34acc..f8e22fc 100644 --- a/src/internal/app/server/router.go +++ b/src/internal/app/server/router.go @@ -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") diff --git a/src/internal/app/server/server.go b/src/internal/app/server/server.go index 03ff34e..c7b3fb5 100644 --- a/src/internal/app/server/server.go +++ b/src/internal/app/server/server.go @@ -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())