Skip to content

Commit

Permalink
bindioc.Handle use only Invoker as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
noobj committed Feb 7, 2023
1 parent e50b42c commit 588809e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 16 deletions.
5 changes: 2 additions & 3 deletions cmd/ahorro/fetchentries/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Invoker struct {
entryRepository EntryRepository.EntryRepository `container:"type"`
}

func (this *Invoker) Invoke(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error) {
func (this Invoker) Invoke(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error) {
user, ok := helper.GetUserFromContext(ctx)
if !ok {
return events.APIGatewayProxyResponse{Body: "please login in", StatusCode: 401}, nil
Expand Down Expand Up @@ -173,7 +173,6 @@ func (this *Invoker) Invoke(ctx context.Context, request events.APIGatewayV2HTTP

func main() {
defer mongodb.Disconnect()()
invoker := Invoker{}

lambda.Start(jwtMiddleWare.Handle(bindioc.Handle(invoker.Invoke, &invoker)))
lambda.Start(jwtMiddleWare.Handle(bindioc.Handle[events.APIGatewayV2HTTPRequest, events.APIGatewayProxyResponse](&Invoker{})))
}
7 changes: 3 additions & 4 deletions cmd/ahorro/login/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type LoginDto struct {
Password string
}

func (this *Invoker) insertNewRefreshTokenIntoLoginInfo(userId primitive.ObjectID, refreshToken string) {
func (this Invoker) insertNewRefreshTokenIntoLoginInfo(userId primitive.ObjectID, refreshToken string) {
loginInfo := LoginInfoRepository.LoginInfo{
User: userId,
RefreshToken: refreshToken,
Expand All @@ -39,7 +39,7 @@ type Invoker struct {
loginInfoRepository LoginInfoRepository.LoginInfoRepository `container:"type"`
}

func (this *Invoker) Invoke(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayV2HTTPResponse, error) {
func (this Invoker) Invoke(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayV2HTTPResponse, error) {
var requestBody LoginDto

formData, err := helper.ParseMultipartForm(request.Headers["content-type"], strings.NewReader(request.Body), request.IsBase64Encoded)
Expand Down Expand Up @@ -118,7 +118,6 @@ func (this *Invoker) Invoke(ctx context.Context, request events.APIGatewayProxyR

func main() {
defer mongodb.Disconnect()()
invoker := Invoker{}

lambda.Start(bindioc.Handle(invoker.Invoke, &invoker))
lambda.Start(bindioc.Handle[events.APIGatewayProxyRequest, events.APIGatewayV2HTTPResponse](&Invoker{}))
}
3 changes: 1 addition & 2 deletions cmd/ahorro/refresh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func (this *Invoker) Invoke(ctx context.Context, request events.APIGatewayV2HTTP

func main() {
defer mongodb.Disconnect()()
invoker := Invoker{}

lambda.Start(bindioc.Handle(invoker.Invoke, &invoker))
lambda.Start(bindioc.Handle[events.APIGatewayV2HTTPRequest, events.APIGatewayV2HTTPResponse](&Invoker{}))
}
3 changes: 1 addition & 2 deletions cmd/ahorro/sync/callback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func (this *Invoker) Invoke(ctx context.Context, request events.APIGatewayV2HTTP

func main() {
defer mongodb.Disconnect()()
invoker := Invoker{}

lambda.Start(jwtMiddleWare.Handle(bindioc.Handle(invoker.Invoke, &invoker)))
lambda.Start(jwtMiddleWare.Handle(bindioc.Handle[events.APIGatewayV2HTTPRequest, events.APIGatewayProxyResponse](&Invoker{})))
}
3 changes: 1 addition & 2 deletions cmd/ahorro/sync/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ func collateEntryItems(entryItems []EntryItem, cateIdMap map[string]primitive.Ob

func main() {
defer mongodb.Disconnect()()
invoker := Invoker{}

lambda.Start(jwtMiddleWare.Handle(bindioc.Handle(invoker.Invoke, &invoker)))
lambda.Start(jwtMiddleWare.Handle(bindioc.Handle[events.SQSEvent, events.APIGatewayProxyResponse](&Invoker{})))
}
7 changes: 4 additions & 3 deletions internal/middleware/bind-ioc/bind-ioc.middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/golobby/container/v3"

// TODO: use internal types instead
typesInternal "github.com/noobj/go-serverless-services/internal/types"
"github.com/noobj/jwtmiddleware/types"

"github.com/noobj/go-serverless-services/internal/helpers/helper"
Expand All @@ -17,10 +18,10 @@ import (
UserRepository "github.com/noobj/go-serverless-services/internal/repositories/ahorro/user"
)

func Handle[T types.ApiRequest, R types.ApiResponse](next types.HandlerFunc[T, R], invoker interface{}) types.HandlerFunc[T, R] {
func Handle[T types.ApiRequest, R types.ApiResponse](invoker typesInternal.IIvoker[T, R]) types.HandlerFunc[T, R] {
return func(ctx context.Context, request T) (R, error) {
receiverType := reflect.TypeOf(invoker)
if receiverType == nil || receiverType.Kind() == reflect.Ptr {
if receiverType == nil || receiverType.Kind() != reflect.Ptr {
log.Println("container: invalid invoker type")
return helper.GenerateErrorResponse[R](500)
}
Expand All @@ -47,6 +48,6 @@ func Handle[T types.ApiRequest, R types.ApiResponse](next types.HandlerFunc[T, R
return helper.GenerateErrorResponse[R](500)
}

return next(ctx, request)
return invoker.Invoke(ctx, request)
}
}
35 changes: 35 additions & 0 deletions internal/middleware/middleswares-loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package middleware

import (
"context"
"fmt"

typesInternal "github.com/noobj/go-serverless-services/internal/types"
"github.com/noobj/jwtmiddleware/types"
)

type middlewaresFunc[T types.ApiRequest, R types.ApiResponse] func(invoker typesInternal.IIvoker[T, R]) types.HandlerFunc[T, R]

func fakeMiddlewareFunc[T types.ApiRequest, R types.ApiResponse](invoker typesInternal.IIvoker[T, R]) types.HandlerFunc[T, R] {
return func(ctx context.Context, request T) (R, error) {
fmt.Println("Fake middleware")
return invoker.Invoke(ctx, request)
}
}

func Bootstrap[T types.ApiRequest, R types.ApiResponse](invoker typesInternal.IIvoker[T, R], middlewareFuncs ...middlewaresFunc[T, R]) types.HandlerFunc[T, R] {
result := func(mFunc middlewaresFunc[T, R]) middlewaresFunc[T, R] {
return func(invoker typesInternal.IIvoker[T, R]) types.HandlerFunc[T, R] {
return mFunc(invoker)
}
}

for _, middlewareFunc := range middlewareFuncs {
oldResult := result
result = func(mFunc middlewaresFunc[T, R]) middlewaresFunc[T, R] {
return oldResult(middlewareFunc)
}
}

return result(fakeMiddlewareFunc[T, R])(invoker)
}
6 changes: 6 additions & 0 deletions internal/types/middleware.type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package types

import (
"context"

"github.com/noobj/jwtmiddleware/types"
)

// HandlerFunc is a generic JSON Lambda handler used to chain middleware.
type HandlerFunc[T ApiRequest, R ApiResponse] func(context.Context, T) (R, error)

type IIvoker[T types.ApiRequest, R types.ApiResponse] interface {
Invoke(context.Context, T) (R, error)
}

0 comments on commit 588809e

Please sign in to comment.