diff --git a/src/cmd/server/main.go b/src/cmd/server/main.go index 3637674..9567505 100644 --- a/src/cmd/server/main.go +++ b/src/cmd/server/main.go @@ -17,7 +17,7 @@ func main() { } c := common.NewConfig() - fmt.Printf("config main: %v\n", c.Base.AppID) + fmt.Println("Creating new server...") srv := server.New( c, server.WithCache(c), @@ -28,11 +28,7 @@ func main() { ) forever := make(chan struct{}) - err := srv.Base.Start(forever, srv.Shutdown()) - // server.SetupMiddleware(srv) - // server.SetupRouter(srv) - <-forever if err != nil { diff --git a/src/internal/app/server/basket_handler.go b/src/internal/app/server/basket_handler.go index e408b69..f13d877 100644 --- a/src/internal/app/server/basket_handler.go +++ b/src/internal/app/server/basket_handler.go @@ -2,7 +2,6 @@ package server // REFACTOR: APP DEDICATED CODE import ( - "context" "time" "git.pbiernat.dev/egommerce/api-entities/http" @@ -19,8 +18,7 @@ func (s *Server) GetBasketHandler(c *fiber.Ctx) error { basketID := req.BasketID basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger) - ctx := context.Background() - basket, err := basketSrv.FetchFromDB(ctx, basketID) + basket, err := basketSrv.FetchFromDB(c.Context(), basketID) if err != nil { return s.Base.Error(c, 400, "Failed to retrieve basket") } @@ -40,8 +38,7 @@ func (s *Server) GetBasketHandler(c *fiber.Ctx) error { func (s *Server) GetBasketItemsHandler(c *fiber.Ctx) error { basketID := c.Params("basketId", "") basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger) - ctx := context.Background() - items, err := basketSrv.FetchItems(ctx, basketID) + items, err := basketSrv.FetchItems(c.Context(), basketID) if err != nil { return s.Base.Error(c, 400, "Failed to retrieve basket") } diff --git a/src/internal/app/server/health_handler.go b/src/internal/app/server/health_handler.go index 8c0a64e..b48d702 100644 --- a/src/internal/app/server/health_handler.go +++ b/src/internal/app/server/health_handler.go @@ -7,9 +7,22 @@ import ( "git.pbiernat.dev/egommerce/api-entities/http" ) -func (s *Server) HealthHandler(c *fiber.Ctx) error { // TODO add necessary logic +func (s *Server) HealthHandler(c *fiber.Ctx) error { + // ctx := context.Background() + if s.Cache.Ping(c.Context()).Val() != "PONG" { + // TODO: log cache connection error... + return fiber.ErrBadRequest + } + + if s.Database.Ping(c.Context()) != nil { + // TODO: log database connection error... + return fiber.ErrBadRequest + } + + // TODO add rest of services checks + return c.JSON(&http.HealthResponse{ - Status: "OKa", + Status: "OK", }) } diff --git a/src/internal/app/server/server.go b/src/internal/app/server/server.go index 281fc8c..5f2319f 100644 --- a/src/internal/app/server/server.go +++ b/src/internal/app/server/server.go @@ -153,7 +153,6 @@ func WithRegistry(c *common.Config) OptionFn { 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() } }() @@ -162,20 +161,6 @@ func WithRegistry(c *common.Config) OptionFn { } } -// REFACTOR IN PROGRESS -// func (s *Server) Shutdown() error { -// s.Logger.Log("Server is going down... Unregistering service: %s", s.Discovery.GetID()) -// s.Discovery.Unregister() -// s.clearMetadataCache() - -// s.Cache.Close() -// s.Database.Close() -// s.Eventbus.Close() -// s.Logger.Close() - -// return nil -// } - func (s *Server) Shutdown() srv.PurgeFn { return func(srv *srv.Server) error { // s.Logger.Log("Server is going down... Unregistering service: %s", s.Base.AppID) @@ -191,8 +176,6 @@ func (s *Server) Shutdown() srv.PurgeFn { } } -// END: REFACTOR IN PROGRESS - // @CHECK: merge s.Config and s.Base.Config to display all config as one array/map func (s *Server) updateKVConfig() { config, _, err := s.Registry.KV().Get(s.kvNmspc, nil) @@ -208,7 +191,7 @@ func (s *Server) updateKVConfig() { } func (s *Server) cacheMetadata() { - ctx := context.TODO() + ctx := context.Background() key, address := s.getMetadataIPsKey(), s.Base.Config.AppID pos := s.Cache.LPos(ctx, key, address, redis.LPosArgs{}).Val() @@ -220,7 +203,7 @@ func (s *Server) cacheMetadata() { } func (s *Server) clearMetadataCache() { - ctx := context.TODO() + ctx := context.Background() fmt.Printf("metadata: %v", s.Config.Base) key, address := s.getMetadataIPsKey(), s.Config.Base.AppID @@ -228,35 +211,5 @@ func (s *Server) clearMetadataCache() { } func (s *Server) getMetadataIPsKey() string { - fmt.Printf("getMetadataIPsKey: %s", s.Base.Config) return "internal__" + s.Base.Config.AppName + "__ips" } - -// -// -// -//// OLD CODE TO BE REMOVED -// func NewServer(conf *Config, logger *fluentd.Logger, db *pgxpool.Pool, cache *redis.Client, ebCh *amqp.Channel) *Server { - -// cnf := fiber.Config{ -// AppName: conf.AppName, -// ServerHeader: conf.AppName, -// ReadTimeout: conf.ReadTimeout * time.Millisecond, -// WriteTimeout: conf.WriteTimeout * time.Millisecond, -// IdleTimeout: conf.IdleTimeout * time.Millisecond, -// } -// s := &Server{ -// fiber.New(cnf), -// conf, -// logger, -// db, -// cache, -// ebCh, -// consul, -// conf.AppName, -// conf.NetAddr, -// conf.KVNamespace, -// } - -// return s -// } diff --git a/src/pkg/server/server.go b/src/pkg/server/server.go index 2fbd44e..7893a74 100644 --- a/src/pkg/server/server.go +++ b/src/pkg/server/server.go @@ -63,7 +63,6 @@ func (s *Server) Error(c *fiber.Ctx, code int, msg string) error { func (s *Server) Start(forever chan struct{}, prgFn PurgeFn) error { go func() { - 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