-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
41 lines (33 loc) · 971 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func resp(body string, status int) (*events.APIGatewayProxyResponse, error) {
return &events.APIGatewayProxyResponse{
StatusCode: status,
Headers: map[string]string{"Content-Type": "text/html"},
Body: body,
}, nil
}
func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
const requiredQueryParameter = "url"
// parse the request
url, ok := request.QueryStringParameters[requiredQueryParameter]
if !ok {
err := fmt.Sprintf(`Query parameter "%s" is missing or incorrect`, requiredQueryParameter)
return resp(err, 400)
}
// get the page's html
html, err := getPageHTML(url)
if err != nil {
err := fmt.Sprintf("Failed to fetch page HTML. Error: %s", err.Error())
return resp(err, 500)
}
return resp(html, 200)
}
func main() {
lambda.Start(handler)
}