Created
January 30, 2018 04:29
-
-
Save Konboi/2c5c735ce2f9f966515abc3b566c2c2e to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"time" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/aws/aws-lambda-go/lambdacontext" | |
) | |
type Request struct { | |
Body string `json:"body"` | |
} | |
type Response struct { | |
Body string `json:"body"` | |
Time time.Time `json:"time"` | |
} | |
type ErrorResponse struct { | |
Message string `json:"message"` | |
Time time.Time `json:"time"` | |
} | |
func (er *ErrorResponse) Error() string { | |
return fmt.Sprintf("error:%s at:%s", er.Message, er.Time.String()) | |
} | |
func main() { | |
lambda.Start(EchoHandler) | |
} | |
func EchoHandler(ctx context.Context, req *Request) (*Response, error) { | |
lctx, ok := lambdacontext.FromContext(ctx) | |
if !ok { | |
return nil, &ErrorResponse{ | |
Message: "FromContextError", | |
Time: time.Now(), | |
} | |
} | |
log.Printf("lambda ctx info:%+v", lctx) | |
return &Response{ | |
Body: req.Body, | |
Time: time.Now(), | |
}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment