-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#814 Add Transport Layer: AWS Lambda #815
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
c02c86e
base serving, encode, decode functions
suekto-andreas 92a7ef3
logger
suekto-andreas 4250690
add before encode options
suekto-andreas 0c20a3a
add ServerAfter functions
suekto-andreas 4d460b1
add error encoder
suekto-andreas 095df04
add finalizer
suekto-andreas a81b28e
add basic happy flow
suekto-andreas 9561ebf
add ServerBefore tests
suekto-andreas 4b8444b
test error flow on decoding stage
suekto-andreas d05a7ed
complete the test scenarios
suekto-andreas 6a3be3a
refactor into more generic support by implementing lambda.Handler
suekto-andreas d29f5a3
testing: ErrorEncoder to reflect behavior of encoding error as payloa…
suekto-andreas 4fd90b0
add wrapper
suekto-andreas 50e8961
capitalize Lambda
suekto-andreas 356f722
add particle an
suekto-andreas bceaf55
tidy up doc words as per suggested
suekto-andreas fe44f2d
Update transport/awslambda/request_response_funcs.go
peterbourgon 4c4334a
Update transport/awslambda/request_response_funcs.go
peterbourgon 9e2d959
Update transport/awslambda/server.go
peterbourgon cad1fdc
Update transport/awslambda/request_response_funcs.go
peterbourgon 3de95b5
refactor multiline to keep them 1 per line
suekto-andreas 62efc64
remove \n from test
suekto-andreas 19d5afe
defines a DefaultErrorEncoder, refactor the handling of errorEncoder …
suekto-andreas 6a69716
refactor Server into Handler
suekto-andreas 5a7cbf4
remove wrapper
suekto-andreas e831b16
refactor variable s into h
suekto-andreas 1be9848
simplify the return of errorEncoder
suekto-andreas 4f01057
Update transport/awslambda/handler.go
fahrradflucht a04bdd6
Update transport/awslambda/handler.go
fahrradflucht 40f8b1a
Update transport/awslambda/handler.go
fahrradflucht 1a31338
DefaultErrorEncoder unit test
suekto-andreas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package awslambda provides an AWS Lambda transport layer. | ||
package awslambda |
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,16 @@ | ||
package awslambda | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// DecodeRequestFunc extracts a user-domain request object from an | ||
// AWS Lambda payload. | ||
type DecodeRequestFunc func(context.Context, []byte) (interface{}, error) | ||
|
||
// EncodeResponseFunc encodes the passed response object into []byte, | ||
// ready to be sent as AWS Lambda response. | ||
type EncodeResponseFunc func(context.Context, interface{}) ([]byte, error) | ||
|
||
// ErrorEncoder is responsible for encoding an error. | ||
type ErrorEncoder func(ctx context.Context, err error) ([]byte, error) |
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,120 @@ | ||
package awslambda | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-kit/kit/endpoint" | ||
"github.com/go-kit/kit/log" | ||
) | ||
|
||
// Handler wraps an endpoint. | ||
type Handler struct { | ||
e endpoint.Endpoint | ||
dec DecodeRequestFunc | ||
enc EncodeResponseFunc | ||
before []HandlerRequestFunc | ||
after []HandlerResponseFunc | ||
errorEncoder ErrorEncoder | ||
finalizer []HandlerFinalizerFunc | ||
logger log.Logger | ||
} | ||
|
||
// NewHandler constructs a new handler, which implements | ||
// the AWS lambda.Handler interface. | ||
func NewHandler( | ||
e endpoint.Endpoint, | ||
dec DecodeRequestFunc, | ||
enc EncodeResponseFunc, | ||
options ...HandlerOption, | ||
) *Handler { | ||
h := &Handler{ | ||
e: e, | ||
dec: dec, | ||
enc: enc, | ||
logger: log.NewNopLogger(), | ||
errorEncoder: DefaultErrorEncoder, | ||
} | ||
for _, option := range options { | ||
option(h) | ||
} | ||
return h | ||
} | ||
|
||
// HandlerOption sets an optional parameter for handlers. | ||
type HandlerOption func(*Handler) | ||
|
||
// HandlerBefore functions are executed on the payload byte, | ||
// before the request is decoded. | ||
func HandlerBefore(before ...HandlerRequestFunc) HandlerOption { | ||
return func(h *Handler) { h.before = append(h.before, before...) } | ||
} | ||
|
||
// HandlerAfter functions are only executed after invoking the endpoint | ||
// but prior to returning a response. | ||
func HandlerAfter(after ...HandlerResponseFunc) HandlerOption { | ||
return func(h *Handler) { h.after = append(h.after, after...) } | ||
} | ||
|
||
// HandlerErrorLogger is used to log non-terminal errors. | ||
// By default, no errors are logged. | ||
func HandlerErrorLogger(logger log.Logger) HandlerOption { | ||
return func(h *Handler) { h.logger = logger } | ||
} | ||
|
||
// HandlerErrorEncoder is used to encode errors. | ||
func HandlerErrorEncoder(ee ErrorEncoder) HandlerOption { | ||
return func(h *Handler) { h.errorEncoder = ee } | ||
} | ||
|
||
// HandlerFinalizer sets finalizer which are called at the end of | ||
// request. By default no finalizer is registered. | ||
func HandlerFinalizer(f ...HandlerFinalizerFunc) HandlerOption { | ||
return func(h *Handler) { h.finalizer = append(h.finalizer, f...) } | ||
} | ||
|
||
// DefaultErrorEncoder defines the default behavior of encoding an error response, | ||
// where it returns nil, and the error itself. | ||
func DefaultErrorEncoder(ctx context.Context, err error) ([]byte, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this code is now uncovered by the tests? Though I obviously don't expect it to have bugs 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add it in 1a31338 |
||
return nil, err | ||
} | ||
|
||
// Invoke represents implementation of the AWS lambda.Handler interface. | ||
func (h *Handler) Invoke( | ||
ctx context.Context, | ||
payload []byte, | ||
) (resp []byte, err error) { | ||
if len(h.finalizer) > 0 { | ||
defer func() { | ||
for _, f := range h.finalizer { | ||
f(ctx, resp, err) | ||
} | ||
}() | ||
} | ||
|
||
for _, f := range h.before { | ||
ctx = f(ctx, payload) | ||
} | ||
|
||
request, err := h.dec(ctx, payload) | ||
if err != nil { | ||
h.logger.Log("err", err) | ||
return h.errorEncoder(ctx, err) | ||
} | ||
|
||
response, err := h.e(ctx, request) | ||
if err != nil { | ||
h.logger.Log("err", err) | ||
return h.errorEncoder(ctx, err) | ||
} | ||
|
||
for _, f := range h.after { | ||
ctx = f(ctx, response) | ||
} | ||
|
||
if resp, err = h.enc(ctx, response); err != nil { | ||
h.logger.Log("err", err) | ||
return h.errorEncoder(ctx, err) | ||
} | ||
|
||
return resp, err | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To what? Is the []byte returned just like a response?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are u referring to
ErrorEncoder
? if it is - then yes, the []byte is for controlling the response, for example in the context of building an API, we might want to response gracefully with a body of JSON describing detail of the error to Consumer.