Created
January 18, 2021 16:34
-
-
Save marioarizaj/dc8a5a988459f52cd79798226a926c07 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 ( | |
"encoding/json" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/marioarizaj/serverless-example/api/common" | |
) | |
// Response is of type APIGatewayProxyResponse since we're leveraging the | |
// AWS Lambda Proxy Request functionality (default behavior) | |
// | |
// https://serverless.com/framework/docs/providers/aws/events/apigateway/#lambda-proxy-integration | |
// Handler is our lambda handler invoked by the `lambda.Start` function call | |
func Handler() (events.APIGatewayProxyResponse, error) { | |
db, err := common.ConnectToDB() | |
if err != nil { | |
return common.InternalServerError(), err | |
} | |
articles, err := db.GetAllArticles() | |
if err != nil { | |
return common.InternalServerError(), err | |
} | |
articlesBts, err := json.Marshal(articles) | |
if err != nil { | |
return common.InternalServerError(), err | |
} | |
resp := events.APIGatewayProxyResponse{ | |
StatusCode: 200, | |
IsBase64Encoded: false, | |
Body: string(articlesBts), | |
Headers: map[string]string{ | |
"Content-Type": "application/json", | |
}, | |
} | |
return resp, nil | |
} | |
func main() { | |
lambda.Start(Handler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment