Refactoring - Added Functional Option pattern - seems work fine...
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Piotr Biernat 2023-04-06 02:03:52 +02:00
parent 524bbc7708
commit 00a3b5d5d5
5 changed files with 20 additions and 62 deletions

View File

@ -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 {

View File

@ -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")
}

View File

@ -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",
})
}

View File

@ -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
// }

View File

@ -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