diff --git a/Makefile b/Makefile index 2d234f9..86e36c3 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ build-image-dev: build-image-prod: - sh ${DEPLOY_DIR}/image-build.sh +push-image-dev: + - sh ${DEPLOY_DIR}/image-push.sh dev + push-image-prod: - sh ${DEPLOY_DIR}/image-push.sh diff --git a/src/go.mod b/src/go.mod index 35f246b..0d055c7 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( git.pbiernat.dev/egommerce/api-entities v0.0.26 - git.pbiernat.dev/egommerce/go-api-pkg v0.0.151 + git.pbiernat.dev/egommerce/go-api-pkg v0.0.153 github.com/georgysavva/scany/v2 v2.0.0 github.com/go-pg/migrations/v8 v8.1.0 github.com/go-pg/pg/v10 v10.10.7 diff --git a/src/go.sum b/src/go.sum index 7acd140..dff0365 100644 --- a/src/go.sum +++ b/src/go.sum @@ -9,6 +9,10 @@ git.pbiernat.dev/egommerce/go-api-pkg v0.0.150 h1:DMM3Kxb6HNw4BExA7Ss7P9ivs+TIeO git.pbiernat.dev/egommerce/go-api-pkg v0.0.150/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= git.pbiernat.dev/egommerce/go-api-pkg v0.0.151 h1:MKf+tka3Bhh4Zbn5cLqO6H39gsf7el/GUT8ittaIujM= git.pbiernat.dev/egommerce/go-api-pkg v0.0.151/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= +git.pbiernat.dev/egommerce/go-api-pkg v0.0.152 h1:FORgrSr+0g0oZqO8rqGgZ38Eeb4Z9RAAxarYclYjK7o= +git.pbiernat.dev/egommerce/go-api-pkg v0.0.152/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= +git.pbiernat.dev/egommerce/go-api-pkg v0.0.153 h1:Hop3RNnkUHEtT78TFWV9WJHOoARE499665f7YEllN3k= +git.pbiernat.dev/egommerce/go-api-pkg v0.0.153/go.mod h1:w2N79aoumjrrcrGPJLkCwxAHtrLd7G4Uj8VOxvPooa0= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= diff --git a/src/internal/config/config.go b/src/internal/config/config.go index 43253ab..10eb6a6 100644 --- a/src/internal/config/config.go +++ b/src/internal/config/config.go @@ -13,7 +13,7 @@ const ( defCacheAddr = "api-cache:6379" defCachePassword = "12345678" defDbURL = "postgres://postgres:12345678@postgres-db:5432/egommerce" - defEventBusURL = "amqp://guest:guest@api-eventbus:56721" + defEventBusURL = "amqp://guest:guest@api-gateway:5672" defKVNmspc = "dev.egommerce/service/basket" defLoggerAddr = "api-logger:24224" defNetAddr = ":80" diff --git a/src/internal/server/router.go b/src/internal/server/router.go index df12031..73ccb17 100644 --- a/src/internal/server/router.go +++ b/src/internal/server/router.go @@ -2,9 +2,6 @@ package server // REFACTOR: APP DEDICATED CODE import ( - "net/http" - - "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" ) @@ -30,11 +27,11 @@ func SetupRouter(s *Server) { v1.Post("/checkout", s.CheckoutHandler) } -func CORSPreflightMiddleware(c *fiber.Ctx) error { - if string(c.Request().Header.Method()) == http.MethodOptions { - c.Response().SetStatusCode(http.StatusOK) - c.Next() - } +// func CORSPreflightMiddleware(c *fiber.Ctx) error { +// if string(c.Request().Header.Method()) == http.MethodOptions { +// c.Response().SetStatusCode(http.StatusOK) +// c.Next() +// } - return c.Next() -} +// return c.Next() +// } diff --git a/src/internal/worker/worker.go b/src/internal/worker/worker.go index 9502417..a27fe7d 100644 --- a/src/internal/worker/worker.go +++ b/src/internal/worker/worker.go @@ -7,7 +7,6 @@ import ( "log" "os" "os/signal" - "strings" "syscall" "time" @@ -19,11 +18,9 @@ import ( "git.pbiernat.dev/egommerce/go-api-pkg/fluentd" "git.pbiernat.dev/egommerce/go-api-pkg/rabbitmq" - "git.pbiernat.dev/egommerce/basket-service/pkg/database" - cnf "git.pbiernat.dev/egommerce/basket-service/internal/config" - "git.pbiernat.dev/egommerce/basket-service/internal/event" "git.pbiernat.dev/egommerce/basket-service/internal/service" + "git.pbiernat.dev/egommerce/basket-service/pkg/database" ) type ( @@ -224,61 +221,58 @@ func (w *Worker) doWork() error { go func() { basketSrvc := service.NewBasketService(w.Database, w.Cache, w.Eventbus, w.Logger) - for d := range msgs { - go func(d amqp.Delivery) { - w.processMsg(basketSrvc, d) - }(d) + for m := range msgs { + go func(m amqp.Delivery) { + w.processMsg(basketSrvc, m) + }(m) } }() return nil } -func (w *Worker) processMsg(srvc *service.BasketService, d amqp.Delivery) { - msg, err := rabbitmq.Deserialize(d.Body) +func (w *Worker) processMsg(srvc *service.BasketService, m amqp.Delivery) { + msg, err := rabbitmq.Deserialize(m.Body) if err != nil { w.Logger.Log("Deserialization error: %v\n", err) - d.Reject(false) + m.Reject(false) return } rnr := &CommandRunner{} + name := fmt.Sprintf("%s", msg["event"]) data := (msg["data"]).(map[string]interface{}) - reqID := (data["request_id"]).(string) // FIXME Check input params! + // reqID := (data["request_id"]).(string) // FIXME Check input params! w.Logger.Log("Processing message \"%s\" with data: %v\n", name, data) var ok = false - switch true { // Refactor -> use case for polymorphism - case strings.Contains(name, event.EVENT_PRODUCT_ADDED_TO_BASKET): - basketID := data["basket_id"].(string) // FIXME Check input params! - productID := data["product_id"] // FIXME Check input params! + w.Logger.Log("CMD:") + rnr.cmd = (msg["command"]).(Command) + w.Logger.Log(": %v", rnr.cmd) + // case strings.Contains(name, event.EVENT_PRODUCT_ADDED_TO_BASKET): + // basketID := data["basket_id"].(string) // FIXME Check input params! + // productID := data["product_id"] // FIXME Check input params! - rnr.cmd = &AddProductToBasketCommand{srvc} - w.Logger.Log("Adding product #%d to basket #%s. ReqID: #%s", productID, basketID, reqID) - case strings.Contains(name, event.EVENT_PRODUCT_REMOVED_FROM_BASKET): - basketID := data["basket_id"].(string) - productID := data["product_id"].(float64) + // rnr.cmd = &AddProductToBasketCommand{srvc} + // w.Logger.Log("Adding product #%d to basket #%s. ReqID: #%s", productID, basketID, reqID) + // case strings.Contains(name, event.EVENT_PRODUCT_REMOVED_FROM_BASKET): + // basketID := data["basket_id"].(string) + // productID := data["product_id"].(float64) - rnr.cmd = &RemoveProductFromBasketCommand{srvc} - w.Logger.Log("Removing product #%d from basket #%s. ReqID: #%s", productID, basketID, reqID) - } + // rnr.cmd = &RemoveProductFromBasketCommand{srvc} + // w.Logger.Log("Removing product #%d from basket #%s. ReqID: #%s", productID, basketID, reqID) + // } ok, _ = rnr.run(data) if ok { w.Logger.Log("Successful executed message \"%s\"\n", name) + m.Ack(false) return } - if !ok { - w.Logger.Log("Error processing \"%s\": %s (%v)", name, err.Error(), err) - d.Reject(false) // FIXME: or Nack(repeat until success - maybe message shout know...? - - return - } - - w.Logger.Log("Finalized processing: %s", name) - d.Ack(false) + w.Logger.Log("Error processing \"%s\": %s (%v)", name, err.Error(), err) + m.Reject(false) // FIXME: or Nack(repeat until success - maybe message shout know...? }