Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/go.uber.org/zap-1.26.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dreadl0ck authored Aug 12, 2024
2 parents 39acbe7 + fb0c3ec commit c8f0a6c
Show file tree
Hide file tree
Showing 42 changed files with 141,619 additions and 386 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

Expand All @@ -25,7 +25,7 @@ jobs:
check-latest: true
go-version-file: 'go.mod'

- uses: goreleaser/goreleaser-action@v4
- uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
Expand Down
70 changes: 56 additions & 14 deletions domain/redirectdefinition/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,66 @@ import (
"errors"

redirectcommand "github.com/foomo/redirects/domain/redirectdefinition/command"
redirectquery "github.com/foomo/redirects/domain/redirectdefinition/query"
redirectrepository "github.com/foomo/redirects/domain/redirectdefinition/repository"
redirectstore "github.com/foomo/redirects/domain/redirectdefinition/store"
redirectnats "github.com/foomo/redirects/pkg/nats"
redirectprovider "github.com/foomo/redirects/pkg/provider"
"go.uber.org/zap"
)

// API for the domain
type (
API struct {
//qry Queries
cmd Commands
repo *redirectrepository.RedirectsDefinitionRepository
l *zap.Logger
//meter *cmrccommonmetric.Meter
l *zap.Logger
qry Queries
cmd Commands
getSiteIdentifierProvider redirectprovider.SiteIdentifierProviderFunc
repo redirectrepository.RedirectsDefinitionRepository
}
Option func(api *API)
)

func NewAPI(
l *zap.Logger,
repo *redirectrepository.RedirectsDefinitionRepository,
repo redirectrepository.RedirectsDefinitionRepository,
updateSignal *redirectnats.UpdateSignal,
opts ...Option,
) (*API, error) {

inst := &API{
l: l,
repo: repo,
//meter: cmrccommonmetric.NewMeter(l, "checkout", telemetry.Meter()),
}
if inst.l == nil {
return nil, errors.New("missing logger")
}
if inst.repo == nil {
return nil, errors.New("missing cart repository")
}
inst.cmd = Commands{
CreateRedirects: redirectcommand.CreateRedirectsHandlerComposed(
redirectcommand.CreateRedirectsHandler(inst.repo),
redirectcommand.CreateRedirectsConsolidateMiddleware(repo, false),
redirectcommand.CreateRedirectsAutoCreateMiddleware(),
redirectcommand.CreateRedirectsPublishMiddleware(updateSignal),
),
CreateRedirect: redirectcommand.CreateRedirectHandlerComposed(
redirectcommand.CreateRedirectHandler(inst.repo),
redirectcommand.CreateRedirectPublishMiddleware(updateSignal),
),
UpdateRedirect: redirectcommand.UpdateRedirectHandlerComposed(
redirectcommand.UpdateRedirectHandler(inst.repo),
redirectcommand.UpdateRedirectPublishMiddleware(updateSignal),
),
DeleteRedirect: redirectcommand.DeleteRedirectHandlerComposed(
redirectcommand.DeleteRedirectHandler(inst.repo),
redirectcommand.DeleteRedirectPublishMiddleware(updateSignal),
),
}
inst.qry = Queries{
GetRedirects: redirectquery.GetRedirectsHandlerComposed(
redirectquery.GetRedirectsHandler(inst.repo),
),
Search: redirectquery.SearchHandlerComposed(
redirectquery.SearchHandler(inst.repo),
),
}

Expand All @@ -56,8 +80,26 @@ func NewAPI(
// ------------------------------------------------------------------------------------------------

func (a *API) CreateRedirects(ctx context.Context, cmd redirectcommand.CreateRedirects) (err error) {
if err := a.cmd.CreateRedirects(ctx, a.l, cmd); err != nil {
return err
}
return nil
return a.cmd.CreateRedirects(ctx, a.l, cmd)
}

func (a *API) CreateRedirect(ctx context.Context, cmd redirectcommand.CreateRedirect) (err error) {
return a.cmd.CreateRedirect(ctx, a.l, cmd)

}

func (a *API) UpdateRedirect(ctx context.Context, cmd redirectcommand.UpdateRedirect) (err error) {
return a.cmd.UpdateRedirect(ctx, a.l, cmd)
}

func (a *API) DeleteRedirect(ctx context.Context, cmd redirectcommand.DeleteRedirect) (err error) {
return a.cmd.DeleteRedirect(ctx, a.l, cmd)
}

func (a *API) GetRedirects(ctx context.Context) (map[redirectstore.Dimension]map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
return a.qry.GetRedirects(ctx, a.l)
}

func (a *API) Search(ctx context.Context, qry redirectquery.Search) (map[redirectstore.RedirectSource]*redirectstore.RedirectDefinition, error) {
return a.qry.Search(ctx, a.l, qry)
}
102 changes: 0 additions & 102 deletions domain/redirectdefinition/autocreateredirectdefinitions.go

This file was deleted.

69 changes: 69 additions & 0 deletions domain/redirectdefinition/command/createredirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package redirectcommand

import (
"context"
"reflect"
"runtime"
"strings"

redirectrepository "github.com/foomo/redirects/domain/redirectdefinition/repository"
redirectstore "github.com/foomo/redirects/domain/redirectdefinition/store"
redirectnats "github.com/foomo/redirects/pkg/nats"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)

type (
// CreateRedirect command
CreateRedirect struct {
RedirectDefinition *redirectstore.RedirectDefinition `json:"redirectDefinition"`
}
// CreateRedirectHandlerFn handler
CreateRedirectHandlerFn func(ctx context.Context, l *zap.Logger, cmd CreateRedirect) error
// CreateRedirectMiddlewareFn middleware
CreateRedirectMiddlewareFn func(next CreateRedirectHandlerFn) CreateRedirectHandlerFn
)

// CreateRedirectHandler ...
func CreateRedirectHandler(repo redirectrepository.RedirectsDefinitionRepository) CreateRedirectHandlerFn {
return func(ctx context.Context, l *zap.Logger, cmd CreateRedirect) error {
return repo.Insert(ctx, cmd.RedirectDefinition)
}
}

// CreateRedirectHandlerComposed returns the handler with middleware applied to it
func CreateRedirectHandlerComposed(handler CreateRedirectHandlerFn, middlewares ...CreateRedirectMiddlewareFn) CreateRedirectHandlerFn {
composed := func(next CreateRedirectHandlerFn) CreateRedirectHandlerFn {
for _, middleware := range middlewares {
localNext := next
middlewareName := strings.Split(runtime.FuncForPC(reflect.ValueOf(middleware).Pointer()).Name(), ".")[2]
next = middleware(func(ctx context.Context, l *zap.Logger, cmd CreateRedirect) error {
trace.SpanFromContext(ctx).AddEvent(middlewareName)
return localNext(ctx, l, cmd)
})
}
return next
}
handlerName := strings.Split(runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name(), ".")[2]
return composed(func(ctx context.Context, l *zap.Logger, cmd CreateRedirect) error {
trace.SpanFromContext(ctx).AddEvent(handlerName)
return handler(ctx, l, cmd)
})
}

// CreateRedirectPublishMiddleware ...
func CreateRedirectPublishMiddleware(updateSignal *redirectnats.UpdateSignal) CreateRedirectMiddlewareFn {
return func(next CreateRedirectHandlerFn) CreateRedirectHandlerFn {
return func(ctx context.Context, l *zap.Logger, cmd CreateRedirect) error {
err := next(ctx, l, cmd)
if err != nil {
return err
}
err = updateSignal.Publish()
if err != nil {
return err
}
return nil
}
}
}
Loading

0 comments on commit c8f0a6c

Please sign in to comment.