diff --git a/domain/redirectdefinition/api.go b/domain/redirectdefinition/api.go index 3090419..ef083ca 100644 --- a/domain/redirectdefinition/api.go +++ b/domain/redirectdefinition/api.go @@ -31,7 +31,6 @@ func NewAPI( updateSignal *redirectnats.UpdateSignal, opts ...Option, ) (*API, error) { - inst := &API{ l: l, repo: repo, @@ -79,20 +78,19 @@ func NewAPI( // ~ Public methods // ------------------------------------------------------------------------------------------------ -func (a *API) CreateRedirects(ctx context.Context, cmd redirectcommand.CreateRedirects) (err error) { +func (a *API) CreateRedirects(ctx context.Context, cmd redirectcommand.CreateRedirects) error { return a.cmd.CreateRedirects(ctx, a.l, cmd) } -func (a *API) CreateRedirect(ctx context.Context, cmd redirectcommand.CreateRedirect) (err error) { +func (a *API) CreateRedirect(ctx context.Context, cmd redirectcommand.CreateRedirect) error { return a.cmd.CreateRedirect(ctx, a.l, cmd) - } -func (a *API) UpdateRedirect(ctx context.Context, cmd redirectcommand.UpdateRedirect) (err error) { +func (a *API) UpdateRedirect(ctx context.Context, cmd redirectcommand.UpdateRedirect) error { return a.cmd.UpdateRedirect(ctx, a.l, cmd) } -func (a *API) DeleteRedirect(ctx context.Context, cmd redirectcommand.DeleteRedirect) (err error) { +func (a *API) DeleteRedirect(ctx context.Context, cmd redirectcommand.DeleteRedirect) error { return a.cmd.DeleteRedirect(ctx, a.l, cmd) } diff --git a/domain/redirectdefinition/command/createredirect.go b/domain/redirectdefinition/command/createredirect.go index 8f71699..edc671d 100644 --- a/domain/redirectdefinition/command/createredirect.go +++ b/domain/redirectdefinition/command/createredirect.go @@ -26,7 +26,7 @@ type ( // CreateRedirectHandler ... func CreateRedirectHandler(repo redirectrepository.RedirectsDefinitionRepository) CreateRedirectHandlerFn { - return func(ctx context.Context, l *zap.Logger, cmd CreateRedirect) error { + return func(ctx context.Context, _ *zap.Logger, cmd CreateRedirect) error { return repo.Insert(ctx, cmd.RedirectDefinition) } } diff --git a/domain/redirectdefinition/command/createredirectsfromcontentserver.go b/domain/redirectdefinition/command/createredirectsfromcontentserver.go index d811794..e465b7a 100644 --- a/domain/redirectdefinition/command/createredirectsfromcontentserver.go +++ b/domain/redirectdefinition/command/createredirectsfromcontentserver.go @@ -170,7 +170,7 @@ func softDeleteStrategy( idsToDelete []redirectstore.EntityID, newRedirects []*redirectstore.RedirectDefinition, currentDefinitions map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, -) (redirectsToUpsert []*redirectstore.RedirectDefinition) { +) []*redirectstore.RedirectDefinition { additionalRedirects := []*redirectstore.RedirectDefinition{} for _, id := range idsToDelete { for _, def := range newRedirects { diff --git a/domain/redirectdefinition/command/deleteredirect.go b/domain/redirectdefinition/command/deleteredirect.go index a61e669..145f904 100644 --- a/domain/redirectdefinition/command/deleteredirect.go +++ b/domain/redirectdefinition/command/deleteredirect.go @@ -26,7 +26,7 @@ type ( // DeleteRedirectHandler ... func DeleteRedirectHandler(repo redirectrepository.RedirectsDefinitionRepository) DeleteRedirectHandlerFn { - return func(ctx context.Context, l *zap.Logger, cmd DeleteRedirect) error { + return func(ctx context.Context, _ *zap.Logger, cmd DeleteRedirect) error { return repo.Delete(ctx, cmd.ID) } } diff --git a/domain/redirectdefinition/command/updateredirect.go b/domain/redirectdefinition/command/updateredirect.go index dd9d248..7953825 100644 --- a/domain/redirectdefinition/command/updateredirect.go +++ b/domain/redirectdefinition/command/updateredirect.go @@ -26,7 +26,7 @@ type ( // UpdateRedirectHandler ... func UpdateRedirectHandler(repo redirectrepository.RedirectsDefinitionRepository) UpdateRedirectHandlerFn { - return func(ctx context.Context, l *zap.Logger, cmd UpdateRedirect) error { + return func(ctx context.Context, _ *zap.Logger, cmd UpdateRedirect) error { return repo.Update(ctx, cmd.RedirectDefinition) } } diff --git a/domain/redirectdefinition/query/getredirects.go b/domain/redirectdefinition/query/getredirects.go index 13b30b1..58f1285 100644 --- a/domain/redirectdefinition/query/getredirects.go +++ b/domain/redirectdefinition/query/getredirects.go @@ -24,7 +24,7 @@ type ( // GetRedirectsHandler ... func GetRedirectsHandler(repo redirectrepository.RedirectsDefinitionRepository) GetRedirectsHandlerFn { - return func(ctx context.Context, l *zap.Logger) (map[redirectstore.Dimension]map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) { + return func(ctx context.Context, _ *zap.Logger) (map[redirectstore.Dimension]map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) { return repo.FindAll(ctx, true) } } diff --git a/domain/redirectdefinition/query/search.go b/domain/redirectdefinition/query/search.go index 1fdd438..2758caa 100644 --- a/domain/redirectdefinition/query/search.go +++ b/domain/redirectdefinition/query/search.go @@ -27,7 +27,7 @@ type ( // SearchHandler ... func SearchHandler(repo redirectrepository.RedirectsDefinitionRepository) SearchHandlerFn { - return func(ctx context.Context, l *zap.Logger, qry Search) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) { + return func(ctx context.Context, _ *zap.Logger, qry Search) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) { return repo.FindMany(ctx, string(qry.Source), string(qry.Dimension), qry.OnlyActive) } } diff --git a/domain/redirectdefinition/repository/redirectdefinition.go b/domain/redirectdefinition/repository/redirectdefinition.go index 55a28a4..94ec9e8 100644 --- a/domain/redirectdefinition/repository/redirectdefinition.go +++ b/domain/redirectdefinition/repository/redirectdefinition.go @@ -50,7 +50,7 @@ func NewBaseRedirectsDefinitionRepository(l *zap.Logger, persistor *keelmongo.Pe ), ) - _, err = collection.Col().Indexes().DropOne(context.TODO(), "source_1") + _, _ = collection.Col().Indexes().DropOne(context.TODO(), "source_1") if cErr != nil { return nil, cErr @@ -123,7 +123,7 @@ func (rs BaseRedirectsDefinitionRepository) FindAll(ctx context.Context, onlyAct func (rs BaseRedirectsDefinitionRepository) Insert(ctx context.Context, def *redirectstore.RedirectDefinition) error { if def.ID == "" { - def.ID = redirectstore.EntityID(redirectstore.NewEntityID()) + def.ID = redirectstore.NewEntityID() } _, err := rs.collection.Col().InsertOne(ctx, def) return err @@ -135,12 +135,10 @@ func (rs BaseRedirectsDefinitionRepository) Update(ctx context.Context, def *red _, err := rs.collection.Col().UpdateOne(ctx, filter, update) return err - } // maybe will be needed for migrating manual redirections? func (rs BaseRedirectsDefinitionRepository) UpsertMany(ctx context.Context, defs []*redirectstore.RedirectDefinition) error { - var operations []mongo.WriteModel for _, def := range defs { diff --git a/domain/redirectdefinition/repository/repository_test.go b/domain/redirectdefinition/repository/repository_test.go index bc5674b..7819367 100644 --- a/domain/redirectdefinition/repository/repository_test.go +++ b/domain/redirectdefinition/repository/repository_test.go @@ -42,7 +42,6 @@ func TestGetAllRedirects(t *testing.T) { redirectDefinitions, err := repo.FindAll(context.Background(), false) assert.Equal(t, 1, len(redirectDefinitions)) assert.NoError(t, err) - } func TestGenerateAutoRedirects(t *testing.T) { l := zap.L() diff --git a/domain/redirectdefinition/service.go b/domain/redirectdefinition/service.go index 3979730..51a4577 100644 --- a/domain/redirectdefinition/service.go +++ b/domain/redirectdefinition/service.go @@ -37,8 +37,8 @@ func NewService(l *zap.Logger, api *API, options ...ServiceOption) *Service { func (rs *Service) CreateRedirectsFromContentserverexport( _ http.ResponseWriter, r *http.Request, - old, - new map[string]*content.RepoNode, + oldState, + newState map[string]*content.RepoNode, ) error { rs.l.Info("CreateRedirectsFromContentserverexport called ") if !rs.enableCreationOfAutomaticRedirects() { @@ -47,8 +47,8 @@ func (rs *Service) CreateRedirectsFromContentserverexport( } return rs.api.CreateRedirects(r.Context(), redirectcommand.CreateRedirects{ - OldState: old, - NewState: new, + OldState: oldState, + NewState: newState, }) } diff --git a/domain/redirectdefinition/service/service.go b/domain/redirectdefinition/service/service.go index 7bd94a8..a1c6e29 100644 --- a/domain/redirectdefinition/service/service.go +++ b/domain/redirectdefinition/service/service.go @@ -21,6 +21,6 @@ type AdminService interface { // the service is responsible for the internal endpoints // will not be exposed only to other backend services type InternalService interface { - CreateRedirectsFromContentserverexport(w http.ResponseWriter, r *http.Request, old, new map[string]*content.RepoNode) error + CreateRedirectsFromContentserverexport(w http.ResponseWriter, r *http.Request, oldState, newState map[string]*content.RepoNode) error GetRedirects(w http.ResponseWriter, r *http.Request) (map[redirectstore.Dimension]map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) } diff --git a/domain/redirectdefinition/utils/autocreateredirectdefinitions.go b/domain/redirectdefinition/utils/autocreateredirectdefinitions.go index 81d796a..761580e 100644 --- a/domain/redirectdefinition/utils/autocreateredirectdefinitions.go +++ b/domain/redirectdefinition/utils/autocreateredirectdefinitions.go @@ -11,7 +11,7 @@ import ( // AutoCreateRedirectDefinitions generates automatic redirects based on the difference between the old and new content tree. // find new.ID to old.ID and check if the URI is different, if it is different, create a redirect func AutoCreateRedirectDefinitions( - l *zap.Logger, + _ *zap.Logger, oldMap, newMap map[string]*content.RepoNode, dimension redirectstore.Dimension, ) ([]*redirectstore.RedirectDefinition, error) { diff --git a/domain/redirectdefinition/utils/autocreateredirectdefinitions_test.go b/domain/redirectdefinition/utils/autocreateredirectdefinitions_test.go index 7d6f137..4429406 100644 --- a/domain/redirectdefinition/utils/autocreateredirectdefinitions_test.go +++ b/domain/redirectdefinition/utils/autocreateredirectdefinitions_test.go @@ -1,4 +1,4 @@ -package redirectdefinitionutils +package redirectdefinitionutils_test import ( _ "embed" @@ -7,6 +7,7 @@ import ( "testing" "github.com/foomo/contentserver/content" + rdutils "github.com/foomo/redirects/domain/redirectdefinition/utils" "github.com/stretchr/testify/assert" "go.uber.org/zap" ) @@ -30,10 +31,10 @@ func Test_AutoCreateRedirectDefinitionsParse(t *testing.T) { if err != nil { fmt.Println(err) } - redirects, err := AutoCreateRedirectDefinitions( + redirects, err := rdutils.AutoCreateRedirectDefinitions( zap.L(), - CreateFlatRepoNodeMap(p["de"], make(map[string]*content.RepoNode)), - CreateFlatRepoNodeMap(pChanged["de"], make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(p["de"], make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(pChanged["de"], make(map[string]*content.RepoNode)), "HMD-de", ) assert.NoError(t, err) @@ -131,9 +132,9 @@ func Test_AutoCreateRedirectDefinitionsCoverAll(t *testing.T) { }, }, } - redirects, err := AutoCreateRedirectDefinitions(zap.L(), - CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), - CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), + redirects, err := rdutils.AutoCreateRedirectDefinitions(zap.L(), + rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), "HMD-de", ) assert.NoError(t, err) @@ -199,10 +200,10 @@ func Test_AutoCreateRedirectDefinitions(t *testing.T) { }, }, } - redirects, err := AutoCreateRedirectDefinitions( + redirects, err := rdutils.AutoCreateRedirectDefinitions( zap.L(), - CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), - CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), "HMD-de", ) if err != nil { @@ -291,10 +292,10 @@ func Test_AutoCreateRedirectDefinitionsExg1(t *testing.T) { }, }, } - redirects, err := AutoCreateRedirectDefinitions( + redirects, err := rdutils.AutoCreateRedirectDefinitions( zap.L(), - CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), - CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), "HMD-de", ) if err != nil { @@ -383,10 +384,10 @@ func Test_AutoCreateRedirectDefinitionsExg2(t *testing.T) { }, }, } - redirects, err := AutoCreateRedirectDefinitions( + redirects, err := rdutils.AutoCreateRedirectDefinitions( zap.L(), - CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), - CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), "HMD-de", ) if err != nil { @@ -475,10 +476,10 @@ func Test_AutoCreateRedirectDefinitionsExg3(t *testing.T) { }, }, } - redirects, err := AutoCreateRedirectDefinitions( + redirects, err := rdutils.AutoCreateRedirectDefinitions( zap.L(), - CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), - CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), "HMD-de", ) if err != nil { @@ -491,10 +492,10 @@ func Test_AutoCreateRedirectDefinitionsExg3(t *testing.T) { func Test_AutoCreateRedirectDefinitionsEmptyAndNilArgs(t *testing.T) { oldNodes := &content.RepoNode{} newNodes := &content.RepoNode{} - redirects, err := AutoCreateRedirectDefinitions( + redirects, err := rdutils.AutoCreateRedirectDefinitions( zap.L(), - CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), - CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), "HMD-de", ) if err != nil { @@ -504,10 +505,10 @@ func Test_AutoCreateRedirectDefinitionsEmptyAndNilArgs(t *testing.T) { assert.Equal(t, len(redirects), 0) oldNodes = nil newNodes = nil - redirects, err = AutoCreateRedirectDefinitions( + redirects, err = rdutils.AutoCreateRedirectDefinitions( zap.L(), - CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), - CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(oldNodes, make(map[string]*content.RepoNode)), + rdutils.CreateFlatRepoNodeMap(newNodes, make(map[string]*content.RepoNode)), "HMD-de", ) assert.Error(t, err) diff --git a/domain/redirectdefinition/utils/consolidateredirectdefinitions.go b/domain/redirectdefinition/utils/consolidateredirectdefinitions.go index 0306b6c..b0c263e 100644 --- a/domain/redirectdefinition/utils/consolidateredirectdefinitions.go +++ b/domain/redirectdefinition/utils/consolidateredirectdefinitions.go @@ -11,12 +11,11 @@ import ( // * Making list for deleting for definitions with empty target id // * If target of one is source to another one, consolidate those into one definition to prevent multiple redirections func ConsolidateRedirectDefinitions( - l *zap.Logger, - new []*redirectstore.RedirectDefinition, + _ *zap.Logger, + newDefinitions []*redirectstore.RedirectDefinition, current redirectstore.RedirectDefinitions, newNodeMap map[string]*content.RepoNode, ) ([]*redirectstore.RedirectDefinition, []redirectstore.EntityID) { - upsertRedirectDefinitions := []*redirectstore.RedirectDefinition{} deletedIDs := []redirectstore.EntityID{} @@ -33,7 +32,7 @@ func ConsolidateRedirectDefinitions( } // iterate over the incoming redirects and add the new redirects to the list that should be upserted - for _, redirectDefinition := range new { + for _, redirectDefinition := range newDefinitions { upsertRedirectDefinitions = append(upsertRedirectDefinitions, redirectDefinition) // check if the ID of the new redirect is already in the current list of redirects currentDefinitions, ok := currentRedirectsByID[redirectDefinition.ContentID] diff --git a/domain/redirectdefinition/utils/consolidateredirectdefinitions_test.go b/domain/redirectdefinition/utils/consolidateredirectdefinitions_test.go index edca356..da34f56 100644 --- a/domain/redirectdefinition/utils/consolidateredirectdefinitions_test.go +++ b/domain/redirectdefinition/utils/consolidateredirectdefinitions_test.go @@ -1,10 +1,11 @@ -package redirectdefinitionutils +package redirectdefinitionutils_test import ( "testing" "github.com/foomo/contentserver/content" redirectstore "github.com/foomo/redirects/domain/redirectdefinition/store" + rdutils "github.com/foomo/redirects/domain/redirectdefinition/utils" "github.com/stretchr/testify/assert" "go.uber.org/zap" ) @@ -29,7 +30,7 @@ func Test_ConsolidateRedirectDefinitions(t *testing.T) { deletedExpected := []redirectstore.RedirectSource{} - updatedDefs, deletedSources := ConsolidateRedirectDefinitions( + updatedDefs, deletedSources := rdutils.ConsolidateRedirectDefinitions( zap.L(), newRedirects, oldRedirects, @@ -38,7 +39,7 @@ func Test_ConsolidateRedirectDefinitions(t *testing.T) { assert.Equal(t, len(updatedExpected), len(updatedDefs)) assert.Equal(t, len(deletedExpected), len(deletedSources)) - //make sure that consolidated definitions exist in expected + // make sure that consolidated definitions exist in expected for source, definition := range updatedDefs { assert.NotNil(t, updatedDefs[source]) assert.Equal(t, definition, updatedDefs[source]) diff --git a/pkg/middleware/redirects.go b/pkg/middleware/redirects.go index c0ff163..96fe185 100644 --- a/pkg/middleware/redirects.go +++ b/pkg/middleware/redirects.go @@ -5,13 +5,13 @@ import ( keellog "github.com/foomo/keel/log" "github.com/foomo/keel/net/http/middleware" - "github.com/foomo/redirects/pkg/provider" + redirectprovider "github.com/foomo/redirects/pkg/provider" "go.uber.org/zap" ) // Redirects middleware func Redirects(provider redirectprovider.RedirectsProviderInterface) middleware.Middleware { - return func(l *zap.Logger, name string, next http.Handler) http.Handler { + return func(l *zap.Logger, _ string, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // only get request will ever be in need of redirects if r.Method == http.MethodGet { diff --git a/pkg/nats/updatesignal.go b/pkg/nats/updatesignal.go index f07ccb0..bb43c29 100644 --- a/pkg/nats/updatesignal.go +++ b/pkg/nats/updatesignal.go @@ -20,11 +20,11 @@ type UpdateSignal struct { func NewUpdateSignalSubscribeChannel( ctx context.Context, l *zap.Logger, - natsUri, + natsURI, clientID, topic string, ) (chan *nats.Msg, error) { - updateSignal, err := NewUpdateSignal(ctx, l, natsUri, clientID, topic) + updateSignal, err := NewUpdateSignal(ctx, l, natsURI, clientID, topic) if err != nil { return nil, err } @@ -36,14 +36,14 @@ func NewUpdateSignalSubscribeChannel( return channel, nil } -func NewUpdateSignal(ctx context.Context, l *zap.Logger, natsUri, clientID, topic string) (*UpdateSignal, error) { +func NewUpdateSignal(ctx context.Context, l *zap.Logger, natsURI, clientID, topic string) (*UpdateSignal, error) { var err error c := &UpdateSignal{ topic: topic, l: l, messages: make(chan *nats.Msg), } - c.connection, err = nats.Connect(natsUri, DefaultConnectOptions(clientID)...) + c.connection, err = nats.Connect(natsURI, DefaultConnectOptions(clientID)...) if err != nil { keellog.WithError(c.l, err).Error("error when connecting to nats") return nil, err diff --git a/pkg/provider/redirectsprovider.go b/pkg/provider/redirectsprovider.go index 34f9069..f419388 100644 --- a/pkg/provider/redirectsprovider.go +++ b/pkg/provider/redirectsprovider.go @@ -54,9 +54,11 @@ func (p *RedirectsProvider) loadRedirects(ctx context.Context) error { redirectDefinitions, err, clientErr := p.redirectsProviderFunc(ctx) if err != nil { return err - } else if clientErr != nil { + } + if clientErr != nil { return clientErr - } else if redirectDefinitions != nil { + } + if redirectDefinitions != nil { p.Lock() p.redirects = redirectDefinitions p.Unlock() @@ -84,7 +86,7 @@ func (p *RedirectsProvider) Start(ctx context.Context) error { return nil } -func (p *RedirectsProvider) Close(ctx context.Context) error { +func (p *RedirectsProvider) Close(_ context.Context) error { return nil } @@ -149,7 +151,6 @@ func (p *RedirectsProvider) Process(r *http.Request) (*store.Redirect, error) { // matchRedirectDefinition checks if there is a redirect definition matching the request func (p *RedirectsProvider) matchRedirectDefinition(r *http.Request, dimension store.Dimension) (*store.RedirectDefinition, error) { - // 1. full url from cache definition := p.definitionForDimensionAndSource(dimension, store.RedirectSource(r.URL.RequestURI())) if definition != nil {