Refactoring - Added Functional Option pattern - seems work fine...
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
524bbc7708
commit
00a3b5d5d5
@ -17,7 +17,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c := common.NewConfig()
|
c := common.NewConfig()
|
||||||
fmt.Printf("config main: %v\n", c.Base.AppID)
|
fmt.Println("Creating new server...")
|
||||||
srv := server.New(
|
srv := server.New(
|
||||||
c,
|
c,
|
||||||
server.WithCache(c),
|
server.WithCache(c),
|
||||||
@ -28,11 +28,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
forever := make(chan struct{})
|
forever := make(chan struct{})
|
||||||
|
|
||||||
err := srv.Base.Start(forever, srv.Shutdown())
|
err := srv.Base.Start(forever, srv.Shutdown())
|
||||||
// server.SetupMiddleware(srv)
|
|
||||||
// server.SetupRouter(srv)
|
|
||||||
|
|
||||||
<-forever
|
<-forever
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
// REFACTOR: APP DEDICATED CODE
|
// REFACTOR: APP DEDICATED CODE
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.pbiernat.dev/egommerce/api-entities/http"
|
"git.pbiernat.dev/egommerce/api-entities/http"
|
||||||
@ -19,8 +18,7 @@ func (s *Server) GetBasketHandler(c *fiber.Ctx) 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)
|
||||||
ctx := context.Background()
|
basket, err := basketSrv.FetchFromDB(c.Context(), basketID)
|
||||||
basket, err := basketSrv.FetchFromDB(ctx, basketID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s.Base.Error(c, 400, "Failed to retrieve basket")
|
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 {
|
func (s *Server) GetBasketItemsHandler(c *fiber.Ctx) error {
|
||||||
basketID := c.Params("basketId", "")
|
basketID := c.Params("basketId", "")
|
||||||
basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger)
|
basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger)
|
||||||
ctx := context.Background()
|
items, err := basketSrv.FetchItems(c.Context(), basketID)
|
||||||
items, err := basketSrv.FetchItems(ctx, basketID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s.Base.Error(c, 400, "Failed to retrieve basket")
|
return s.Base.Error(c, 400, "Failed to retrieve basket")
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,22 @@ import (
|
|||||||
"git.pbiernat.dev/egommerce/api-entities/http"
|
"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{
|
return c.JSON(&http.HealthResponse{
|
||||||
Status: "OKa",
|
Status: "OK",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +153,6 @@ func WithRegistry(c *common.Config) OptionFn {
|
|||||||
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()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -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 {
|
func (s *Server) Shutdown() srv.PurgeFn {
|
||||||
return func(srv *srv.Server) error {
|
return func(srv *srv.Server) error {
|
||||||
// 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)
|
||||||
@ -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
|
// @CHECK: merge s.Config and s.Base.Config to display all config as one array/map
|
||||||
func (s *Server) updateKVConfig() {
|
func (s *Server) updateKVConfig() {
|
||||||
config, _, err := s.Registry.KV().Get(s.kvNmspc, nil)
|
config, _, err := s.Registry.KV().Get(s.kvNmspc, nil)
|
||||||
@ -208,7 +191,7 @@ func (s *Server) updateKVConfig() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) cacheMetadata() {
|
func (s *Server) cacheMetadata() {
|
||||||
ctx := context.TODO()
|
ctx := context.Background()
|
||||||
key, address := s.getMetadataIPsKey(), s.Base.Config.AppID
|
key, address := s.getMetadataIPsKey(), s.Base.Config.AppID
|
||||||
|
|
||||||
pos := s.Cache.LPos(ctx, key, address, redis.LPosArgs{}).Val()
|
pos := s.Cache.LPos(ctx, key, address, redis.LPosArgs{}).Val()
|
||||||
@ -220,7 +203,7 @@ func (s *Server) cacheMetadata() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) clearMetadataCache() {
|
func (s *Server) clearMetadataCache() {
|
||||||
ctx := context.TODO()
|
ctx := context.Background()
|
||||||
fmt.Printf("metadata: %v", s.Config.Base)
|
fmt.Printf("metadata: %v", s.Config.Base)
|
||||||
key, address := s.getMetadataIPsKey(), s.Config.Base.AppID
|
key, address := s.getMetadataIPsKey(), s.Config.Base.AppID
|
||||||
|
|
||||||
@ -228,35 +211,5 @@ 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"
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//// 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
|
|
||||||
// }
|
|
||||||
|
@ -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 {
|
func (s *Server) Start(forever chan struct{}, prgFn PurgeFn) error {
|
||||||
go func() {
|
go func() {
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user