This commit is contained in:
Piotr Biernat 2023-05-22 21:51:09 +02:00
parent 58d32e702f
commit 9c40900d47
6 changed files with 43 additions and 45 deletions

View File

@ -8,6 +8,9 @@ build-image-dev:
build-image-prod: build-image-prod:
- sh ${DEPLOY_DIR}/image-build.sh - sh ${DEPLOY_DIR}/image-build.sh
push-image-dev:
- sh ${DEPLOY_DIR}/image-push.sh dev
push-image-prod: push-image-prod:
- sh ${DEPLOY_DIR}/image-push.sh - sh ${DEPLOY_DIR}/image-push.sh

View File

@ -4,7 +4,7 @@ go 1.18
require ( require (
git.pbiernat.dev/egommerce/api-entities v0.0.26 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/georgysavva/scany/v2 v2.0.0
github.com/go-pg/migrations/v8 v8.1.0 github.com/go-pg/migrations/v8 v8.1.0
github.com/go-pg/pg/v10 v10.10.7 github.com/go-pg/pg/v10 v10.10.7

View File

@ -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.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 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.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/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/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= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=

View File

@ -13,7 +13,7 @@ const (
defCacheAddr = "api-cache:6379" defCacheAddr = "api-cache:6379"
defCachePassword = "12345678" defCachePassword = "12345678"
defDbURL = "postgres://postgres:12345678@postgres-db:5432/egommerce" 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" defKVNmspc = "dev.egommerce/service/basket"
defLoggerAddr = "api-logger:24224" defLoggerAddr = "api-logger:24224"
defNetAddr = ":80" defNetAddr = ":80"

View File

@ -2,9 +2,6 @@ package server
// REFACTOR: APP DEDICATED CODE // REFACTOR: APP DEDICATED CODE
import ( import (
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/cors"
) )
@ -30,11 +27,11 @@ func SetupRouter(s *Server) {
v1.Post("/checkout", s.CheckoutHandler) v1.Post("/checkout", s.CheckoutHandler)
} }
func CORSPreflightMiddleware(c *fiber.Ctx) error { // func CORSPreflightMiddleware(c *fiber.Ctx) error {
if string(c.Request().Header.Method()) == http.MethodOptions { // if string(c.Request().Header.Method()) == http.MethodOptions {
c.Response().SetStatusCode(http.StatusOK) // c.Response().SetStatusCode(http.StatusOK)
c.Next() // c.Next()
} // }
return c.Next() // return c.Next()
} // }

View File

@ -7,7 +7,6 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"time" "time"
@ -19,11 +18,9 @@ import (
"git.pbiernat.dev/egommerce/go-api-pkg/fluentd" "git.pbiernat.dev/egommerce/go-api-pkg/fluentd"
"git.pbiernat.dev/egommerce/go-api-pkg/rabbitmq" "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" 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/internal/service"
"git.pbiernat.dev/egommerce/basket-service/pkg/database"
) )
type ( type (
@ -224,61 +221,58 @@ func (w *Worker) doWork() error {
go func() { go func() {
basketSrvc := service.NewBasketService(w.Database, w.Cache, w.Eventbus, w.Logger) basketSrvc := service.NewBasketService(w.Database, w.Cache, w.Eventbus, w.Logger)
for d := range msgs { for m := range msgs {
go func(d amqp.Delivery) { go func(m amqp.Delivery) {
w.processMsg(basketSrvc, d) w.processMsg(basketSrvc, m)
}(d) }(m)
} }
}() }()
return nil return nil
} }
func (w *Worker) processMsg(srvc *service.BasketService, d amqp.Delivery) { func (w *Worker) processMsg(srvc *service.BasketService, m amqp.Delivery) {
msg, err := rabbitmq.Deserialize(d.Body) msg, err := rabbitmq.Deserialize(m.Body)
if err != nil { if err != nil {
w.Logger.Log("Deserialization error: %v\n", err) w.Logger.Log("Deserialization error: %v\n", err)
d.Reject(false) m.Reject(false)
return return
} }
rnr := &CommandRunner{} rnr := &CommandRunner{}
name := fmt.Sprintf("%s", msg["event"]) name := fmt.Sprintf("%s", msg["event"])
data := (msg["data"]).(map[string]interface{}) 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) w.Logger.Log("Processing message \"%s\" with data: %v\n", name, data)
var ok = false var ok = false
switch true { // Refactor -> use case for polymorphism w.Logger.Log("CMD:")
case strings.Contains(name, event.EVENT_PRODUCT_ADDED_TO_BASKET): rnr.cmd = (msg["command"]).(Command)
basketID := data["basket_id"].(string) // FIXME Check input params! w.Logger.Log(": %v", rnr.cmd)
productID := data["product_id"] // FIXME Check input params! // 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} // rnr.cmd = &AddProductToBasketCommand{srvc}
w.Logger.Log("Adding product #%d to basket #%s. ReqID: #%s", productID, basketID, reqID) // w.Logger.Log("Adding product #%d to basket #%s. ReqID: #%s", productID, basketID, reqID)
case strings.Contains(name, event.EVENT_PRODUCT_REMOVED_FROM_BASKET): // case strings.Contains(name, event.EVENT_PRODUCT_REMOVED_FROM_BASKET):
basketID := data["basket_id"].(string) // basketID := data["basket_id"].(string)
productID := data["product_id"].(float64) // productID := data["product_id"].(float64)
rnr.cmd = &RemoveProductFromBasketCommand{srvc} // rnr.cmd = &RemoveProductFromBasketCommand{srvc}
w.Logger.Log("Removing product #%d from basket #%s. ReqID: #%s", productID, basketID, reqID) // w.Logger.Log("Removing product #%d from basket #%s. ReqID: #%s", productID, basketID, reqID)
} // }
ok, _ = rnr.run(data) ok, _ = rnr.run(data)
if ok { if ok {
w.Logger.Log("Successful executed message \"%s\"\n", name) w.Logger.Log("Successful executed message \"%s\"\n", name)
m.Ack(false)
return return
} }
if !ok { w.Logger.Log("Error processing \"%s\": %s (%v)", name, err.Error(), err)
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...?
d.Reject(false) // FIXME: or Nack(repeat until success - maybe message shout know...?
return
}
w.Logger.Log("Finalized processing: %s", name)
d.Ack(false)
} }