Fixed silly error
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Piotr Biernat 2023-04-06 01:00:19 +02:00
parent 50ffd267f3
commit 524bbc7708
4 changed files with 19 additions and 32 deletions

View File

@ -17,6 +17,7 @@ func main() {
} }
c := common.NewConfig() c := common.NewConfig()
fmt.Printf("config main: %v\n", c.Base.AppID)
srv := server.New( srv := server.New(
c, c,
server.WithCache(c), server.WithCache(c),

View File

@ -14,7 +14,7 @@ import (
func (s *Server) GetBasketHandler(c *fiber.Ctx) error { func (s *Server) GetBasketHandler(c *fiber.Ctx) error {
req := new(http.GetBasketRequest) req := new(http.GetBasketRequest)
if err := c.BodyParser(req); err != nil { if err := c.BodyParser(req); err != nil {
return s.Base.Error400(c, err.Error()) return s.Base.Error(c, 400, err.Error())
} }
basketID := req.BasketID basketID := req.BasketID
@ -22,7 +22,7 @@ func (s *Server) GetBasketHandler(c *fiber.Ctx) error {
ctx := context.Background() ctx := context.Background()
basket, err := basketSrv.FetchFromDB(ctx, basketID) basket, err := basketSrv.FetchFromDB(ctx, basketID)
if err != nil { if err != nil {
return s.Base.Error400(c, "Failed to retrieve basket") return s.Base.Error(c, 400, "Failed to retrieve basket")
} }
res := &http.GetBasketResponse{ res := &http.GetBasketResponse{
@ -43,7 +43,7 @@ func (s *Server) GetBasketItemsHandler(c *fiber.Ctx) error {
ctx := context.Background() ctx := context.Background()
items, err := basketSrv.FetchItems(ctx, basketID) items, err := basketSrv.FetchItems(ctx, basketID)
if err != nil { if err != nil {
return s.Base.Error400(c, "Failed to retrieve basket items") return s.Base.Error(c, 400, "Failed to retrieve basket")
} }
var res []*http.GetBasketItemsResponse var res []*http.GetBasketItemsResponse
@ -69,14 +69,14 @@ func (s *Server) CheckoutHandler(c *fiber.Ctx) error {
reqID, _ := s.Base.GetRequestID(c) reqID, _ := s.Base.GetRequestID(c)
req := new(http.BasketCheckoutRequest) req := new(http.BasketCheckoutRequest)
if err := c.BodyParser(req); err != nil { if err := c.BodyParser(req); err != nil {
return s.Base.Error400(c, err.Error()) return s.Base.Error(c, 400, err.Error())
} }
basketID := req.BasketID basketID := req.BasketID
basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger) basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger)
res, err := ui.CheckoutBasket(basketSrv, basketID, reqID) res, err := ui.CheckoutBasket(basketSrv, basketID, reqID)
if err != nil { if err != nil {
return s.Base.Error400(c, "Failed to create order") return s.Base.Error(c, 400, "Failed to create order")
} }
return c.JSON(res) return c.JSON(res)

View File

@ -42,7 +42,8 @@ type (
func New(c *common.Config, opts ...OptionFn) *Server { func New(c *common.Config, opts ...OptionFn) *Server {
svr := &Server{ svr := &Server{
Base: srv.New(c.Base), Base: srv.New(c.Base),
Config: c,
} }
for _, opt := range opts { for _, opt := range opts {
@ -128,7 +129,6 @@ func WithLogger(c *common.Config) OptionFn {
} }
func WithRegistry(c *common.Config) OptionFn { func WithRegistry(c *common.Config) OptionFn {
// fmt.Printf("WithRegistry constructor: config: %v", c.Base)
port, _ := strconv.Atoi(c.Base.NetAddr[1:]) // FIXME: can be IP:PORT which now will cause error port, _ := strconv.Atoi(c.Base.NetAddr[1:]) // FIXME: can be IP:PORT which now will cause error
registry, err := consul.NewService(c.RegistryAddr, c.Base.AppID, c.Base.AppName, c.Base.AppID, c.Base.AppName, c.Base.PathPrefix, port) registry, err := consul.NewService(c.RegistryAddr, c.Base.AppID, c.Base.AppName, c.Base.AppID, c.Base.AppName, c.Base.PathPrefix, port)
if err != nil { if err != nil {
@ -143,19 +143,20 @@ func WithRegistry(c *common.Config) OptionFn {
return func(s *Server) error { return func(s *Server) error {
s.Registry = registry s.Registry = registry
go func(s *Server) { // Consul KV updater go func( /*s *Server*/ ) { // Consul KV updater
ticker := time.NewTicker(time.Second * 15) ticker := time.NewTicker(time.Second * 15)
for range ticker.C { for range ticker.C {
s.updateKVConfig() s.updateKVConfig()
} }
}(s) }()
go func(s *Server) { // Server metadata cache updater go func( /*s *Server*/ ) { // Server metadata cache updater
ticker := time.NewTicker(time.Second * 5) ticker := time.NewTicker(time.Second * 5)
for range ticker.C { for range ticker.C {
fmt.Printf("config: %v\n", s.Base.Config)
s.cacheMetadata() s.cacheMetadata()
} }
}(s) }()
return nil return nil
} }
@ -177,7 +178,6 @@ func WithRegistry(c *common.Config) OptionFn {
func (s *Server) Shutdown() srv.PurgeFn { func (s *Server) Shutdown() srv.PurgeFn {
return func(srv *srv.Server) error { return func(srv *srv.Server) error {
fmt.Printf("%v", s.Base)
// s.Logger.Log("Server is going down... Unregistering service: %s", s.Base.AppID) // s.Logger.Log("Server is going down... Unregistering service: %s", s.Base.AppID)
s.Logger.Log("Server is going down... Unregistering service...") s.Logger.Log("Server is going down... Unregistering service...")
@ -228,6 +228,7 @@ func (s *Server) clearMetadataCache() {
} }
func (s *Server) getMetadataIPsKey() string { func (s *Server) getMetadataIPsKey() string {
fmt.Printf("getMetadataIPsKey: %s", s.Base.Config)
return "internal__" + s.Base.Config.AppName + "__ips" return "internal__" + s.Base.Config.AppName + "__ips"
} }

View File

@ -43,7 +43,8 @@ func New(conf *Config) *Server {
WriteTimeout: conf.WriteTimeout * time.Millisecond, WriteTimeout: conf.WriteTimeout * time.Millisecond,
IdleTimeout: conf.IdleTimeout * time.Millisecond, IdleTimeout: conf.IdleTimeout * time.Millisecond,
}), }),
addr: conf.NetAddr, Config: conf,
addr: conf.NetAddr,
} }
} }
@ -56,29 +57,13 @@ func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) {
return hdr.RequestID, nil return hdr.RequestID, nil
} }
// @Refactor make single func with error message and optional http status code... func (s *Server) Error(c *fiber.Ctx, code int, msg string) error {
func (s *Server) Error400(c *fiber.Ctx, msg string) error { return c.Status(code).JSON(http.ErrorResponse{Error: msg})
return c.Status(fiber.StatusBadRequest).JSON(http.ErrorResponse{Error: msg})
// test with &(reference) before http.ErrorMessage, but probably it's gonna be erroneous
} }
func (s *Server) Error401(c *fiber.Ctx, msg string) error {
return c.Status(fiber.StatusUnauthorized).JSON(http.ErrorResponse{Error: msg})
}
func (s *Server) Error403(c *fiber.Ctx, msg string) error {
return c.Status(fiber.StatusForbidden).JSON(http.ErrorResponse{Error: msg})
}
func (s *Server) Error404(c *fiber.Ctx, msg string) error {
return c.Status(fiber.StatusNotFound).JSON(http.ErrorResponse{Error: msg})
}
// @EndRefactor
func (s *Server) Start(forever chan struct{}, prgFn PurgeFn) error { func (s *Server) Start(forever chan struct{}, prgFn PurgeFn) error {
go func() { go func() {
fmt.Println("Starting...") fmt.Printf("config at starting point: %v\n", s.Config)
sigint := make(chan os.Signal, 1) sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-sigint <-sigint