Skip to content

Instantly share code, notes, and snippets.

@mlafeldt
Created March 1, 2019 22:43
Show Gist options
  • Save mlafeldt/0ba38a819030e18d053cedb144839aea to your computer and use it in GitHub Desktop.
Save mlafeldt/0ba38a819030e18d053cedb144839aea to your computer and use it in GitHub Desktop.

Revisions

  1. mlafeldt created this gist Mar 1, 2019.
    31 changes: 31 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    func main() {
    lambda.StartHandler(middleware(handler))
    }

    type handlerFunc func(ctx context.Context, payload []byte) ([]byte, error)

    func (h handlerFunc) Invoke(ctx context.Context, input []byte) ([]byte, error) {
    log.Printf("[DEBUG] input = %s", string(input))
    output, err := h(ctx, input)
    log.Printf("[DEBUG] output = %s", string(output))
    return output, err
    }

    func middleware(originalHandler interface{}) lambda.Handler {
    handler := lambda.NewHandler(originalHandler)
    return handlerFunc(func(ctx context.Context, payload []byte) ([]byte, error) {
    ctx = handlertrace.NewContext(ctx, handlertrace.HandlerTrace{
    RequestEvent: func(c context.Context, e interface{}) {
    log.Printf("[DEBUG] input = %+v", e)
    },
    ResponseEvent: func(c context.Context, e interface{}) {
    log.Printf("[DEBUG] output = %+v", e)
    },
    })
    return handler.Invoke(ctx, payload)
    })
    }

    func handler(input Input) (*Output, error) {
    // ...
    }