Skip to content

Commit

Permalink
cmd/server: wire up basic /v2/search
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Dec 18, 2024
1 parent 8faae78 commit bbfba4d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
38 changes: 37 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion internal/search/api_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
33 changes: 32 additions & 1 deletion internal/search/service.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit bbfba4d

Please sign in to comment.