diff --git a/src/cmd/server/main.go b/src/cmd/server/main.go index c078125..3637674 100644 --- a/src/cmd/server/main.go +++ b/src/cmd/server/main.go @@ -17,6 +17,7 @@ func main() { } c := common.NewConfig() + fmt.Printf("config main: %v\n", c.Base.AppID) srv := server.New( c, server.WithCache(c), diff --git a/src/internal/app/server/basket_handler.go b/src/internal/app/server/basket_handler.go index 10905df..e408b69 100644 --- a/src/internal/app/server/basket_handler.go +++ b/src/internal/app/server/basket_handler.go @@ -14,7 +14,7 @@ import ( func (s *Server) GetBasketHandler(c *fiber.Ctx) error { req := new(http.GetBasketRequest) 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 @@ -22,7 +22,7 @@ func (s *Server) GetBasketHandler(c *fiber.Ctx) error { ctx := context.Background() basket, err := basketSrv.FetchFromDB(ctx, basketID) 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{ @@ -43,7 +43,7 @@ func (s *Server) GetBasketItemsHandler(c *fiber.Ctx) error { ctx := context.Background() items, err := basketSrv.FetchItems(ctx, basketID) 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 @@ -69,14 +69,14 @@ func (s *Server) CheckoutHandler(c *fiber.Ctx) error { reqID, _ := s.Base.GetRequestID(c) req := new(http.BasketCheckoutRequest) 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 basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger) res, err := ui.CheckoutBasket(basketSrv, basketID, reqID) 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) diff --git a/src/internal/app/server/server.go b/src/internal/app/server/server.go index a7aa3c2..281fc8c 100644 --- a/src/internal/app/server/server.go +++ b/src/internal/app/server/server.go @@ -42,7 +42,8 @@ type ( func New(c *common.Config, opts ...OptionFn) *Server { svr := &Server{ - Base: srv.New(c.Base), + Base: srv.New(c.Base), + Config: c, } for _, opt := range opts { @@ -128,7 +129,6 @@ func WithLogger(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 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 { @@ -143,19 +143,20 @@ func WithRegistry(c *common.Config) OptionFn { return func(s *Server) error { s.Registry = registry - go func(s *Server) { // Consul KV updater + go func( /*s *Server*/ ) { // Consul KV updater ticker := time.NewTicker(time.Second * 15) for range ticker.C { 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) for range ticker.C { + fmt.Printf("config: %v\n", s.Base.Config) s.cacheMetadata() } - }(s) + }() return nil } @@ -177,7 +178,6 @@ func WithRegistry(c *common.Config) OptionFn { func (s *Server) Shutdown() srv.PurgeFn { 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...") @@ -228,6 +228,7 @@ func (s *Server) clearMetadataCache() { } func (s *Server) getMetadataIPsKey() string { + fmt.Printf("getMetadataIPsKey: %s", s.Base.Config) return "internal__" + s.Base.Config.AppName + "__ips" } diff --git a/src/pkg/server/server.go b/src/pkg/server/server.go index 86bd22a..2fbd44e 100644 --- a/src/pkg/server/server.go +++ b/src/pkg/server/server.go @@ -43,7 +43,8 @@ func New(conf *Config) *Server { WriteTimeout: conf.WriteTimeout * 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 } -// @Refactor make single func with error message and optional http status code... -func (s *Server) Error400(c *fiber.Ctx, msg string) error { - 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) Error(c *fiber.Ctx, code int, msg string) error { + return c.Status(code).JSON(http.ErrorResponse{Error: msg}) } -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 { go func() { - fmt.Println("Starting...") + fmt.Printf("config at starting point: %v\n", s.Config) sigint := make(chan os.Signal, 1) signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) <-sigint