From 9307c80b2b94c275c239b69bca2844898b86c310 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Mon, 30 May 2022 17:19:47 +0300 Subject: [PATCH] MSP-625 reimplement routes --- routes/routes.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ routing/routing.go | 35 ---------------------------- 2 files changed, 58 insertions(+), 35 deletions(-) create mode 100644 routes/routes.go delete mode 100644 routing/routing.go diff --git a/routes/routes.go b/routes/routes.go new file mode 100644 index 0000000..5cbe066 --- /dev/null +++ b/routes/routes.go @@ -0,0 +1,58 @@ +package routes + +import ( + "context" + + "github.com/integration-system/isp-kit/cluster" +) + +type Routes struct { + innerMethods map[string]bool + allMethods map[string]bool + authUserMethods map[string]bool +} + +func NewRoutes() *Routes { + return &Routes{ + innerMethods: make(map[string]bool), + allMethods: make(map[string]bool), + authUserMethods: make(map[string]bool), + } +} + +func (s *Routes) ReceiveRoutes(ctx context.Context, routes cluster.RoutingConfig) error { + newAddressMap := make(map[string]bool) + newInnerAddressMap := make(map[string]bool) + newAuthUserAddressMap := make(map[string]bool) + for _, backend := range routes { + if backend.Address.IP == "" || backend.Address.Port == "" || len(backend.Endpoints) == 0 { + continue + } + for _, v := range backend.Endpoints { + newAddressMap[v.Path] = true + if v.Inner { + newInnerAddressMap[v.Path] = true + } + if v.UserAuthRequired { + newAuthUserAddressMap[v.Path] = true + } + } + } + s.allMethods = newAddressMap + s.innerMethods = newInnerAddressMap + s.authUserMethods = newAuthUserAddressMap + + return nil +} + +func (s *Routes) IsAvailableMethod(path string) bool { + return s.allMethods[path] +} + +func (s *Routes) IsAuthUserMethod(path string) bool { + return s.authUserMethods[path] +} + +func (s *Routes) IsInnerMethod(path string) bool { + return s.innerMethods[path] +} diff --git a/routing/routing.go b/routing/routing.go deleted file mode 100644 index 9aa406b..0000000 --- a/routing/routing.go +++ /dev/null @@ -1,35 +0,0 @@ -package routing - -import ( - "github.com/integration-system/isp-lib/v2/structure" -) - -var ( - InnerMethods = make(map[string]bool) - AllMethods = make(map[string]bool) - AuthUserMethods = make(map[string]bool) -) - -func InitRoutes(configs structure.RoutingConfig) { - newAddressMap := make(map[string]bool) - newInnerAddressMap := make(map[string]bool) - newAuthUserAddressMap := make(map[string]bool) - for i := range configs { - backend := &configs[i] - if backend.Address.IP == "" || backend.Address.Port == "" || len(backend.Endpoints) == 0 { - continue - } - for _, v := range backend.Endpoints { - newAddressMap[v.Path] = true - if v.Inner { - newInnerAddressMap[v.Path] = true - } - if v.UserAuthRequired { - newAuthUserAddressMap[v.Path] = true - } - } - } - AllMethods = newAddressMap - InnerMethods = newInnerAddressMap - AuthUserMethods = newAuthUserAddressMap -}