-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/go_modules/go.uber.org/zap-1.26.0
- Loading branch information
Showing
42 changed files
with
141,619 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 0 additions & 102 deletions
102
domain/redirectdefinition/autocreateredirectdefinitions.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.