From bbfba4d6ab7c8f4761fe11611bce362ada3d3cfc Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Wed, 18 Dec 2024 16:42:03 -0600 Subject: [PATCH] cmd/server: wire up basic /v2/search --- cmd/server/main.go | 38 ++++++++++++++++++++++++++++++++++- internal/search/api_search.go | 2 +- internal/search/service.go | 33 +++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 58802c57..e060cac0 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -20,11 +20,13 @@ import ( "time" "github.com/moov-io/base/admin" - moovhttp "github.com/moov-io/base/http" "github.com/moov-io/base/http/bind" "github.com/moov-io/base/log" "github.com/moov-io/watchman" + searchv2 "github.com/moov-io/watchman/internal/search" + "github.com/moov-io/watchman/pkg/ofac" + pubsearch "github.com/moov-io/watchman/pkg/search" "github.com/gorilla/mux" ) @@ -182,6 +184,10 @@ func main() { addSearchRoutes(logger, router, searcher) addValuesRoutes(logger, router, searcher) + genericSDNs := generalizeOFACSDNs(searcher.SDNs, searcher.Addresses) + v2SearchService := searchv2.NewService[ofac.SDN](logger, genericSDNs) + addSearchV2Routes(logger, router, v2SearchService) + // Setup our web UI to be served as well setupWebui(logger, router, *flagBasePath) @@ -263,3 +269,33 @@ func handleDownloadStats(updates chan *DownloadStats, handle func(stats *Downloa } } } + +func generalizeOFACSDNs(input []*SDN, ofacAddresses []*Address) []pubsearch.Entity[ofac.SDN] { + var out []pubsearch.Entity[ofac.SDN] + for _, sdn := range input { + if sdn.SDN == nil { + continue + } + + var addresses []ofac.Address + for _, ofacAddr := range ofacAddresses { + if ofacAddr.Address == nil { + continue + } + + if sdn.EntityID == ofacAddr.Address.EntityID { + addresses = append(addresses, *ofacAddr.Address) + } + } + + entity := ofac.ToEntity(*sdn.SDN, addresses, nil) + if len(entity.Addresses) > 0 && entity.Addresses[0].Line1 != "" { + out = append(out, entity) + } + } + return out +} + +func addSearchV2Routes(logger log.Logger, r *mux.Router, service searchv2.Service) { + searchv2.NewController(logger, service).AppendRoutes(r) +} diff --git a/internal/search/api_search.go b/internal/search/api_search.go index 0bf34117..038bc6f3 100644 --- a/internal/search/api_search.go +++ b/internal/search/api_search.go @@ -37,5 +37,5 @@ func (c *controller) AppendRoutes(router *mux.Router) *mux.Router { } func (c *controller) search(w http.ResponseWriter, r *http.Request) { - // TODO(adam): + c.service.Search(r.Context()) } diff --git a/internal/search/service.go b/internal/search/service.go index 25b13347..f822032f 100644 --- a/internal/search/service.go +++ b/internal/search/service.go @@ -1,5 +1,36 @@ package search +import ( + "context" + "encoding/json" + "fmt" + + "github.com/moov-io/base/log" + "github.com/moov-io/watchman/pkg/search" +) + type Service interface { - // TODO(adam): + Search(ctx context.Context) +} + +func NewService[T any](logger log.Logger, entities []search.Entity[T]) Service { + return &service[T]{ + logger: logger, + entities: entities, + } +} + +type service[T any] struct { + logger log.Logger + entities []search.Entity[T] +} + +func (s *service[T]) Search(ctx context.Context) { + for _, entity := range s.entities { + if len(entity.Addresses) > 0 { + bs, _ := json.Marshal(entity) + fmt.Printf("\n\n %s \n", string(bs)) + return + } + } }