-
Notifications
You must be signed in to change notification settings - Fork 1
/
checker.go
141 lines (118 loc) · 3.49 KB
/
checker.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package proverbial
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/google/uuid"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/jboursiquot/go-proverbs"
)
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
type ddbPutter interface {
PutItemWithContext(ctx aws.Context, input *dynamodb.PutItemInput, opts ...request.Option) (*dynamodb.PutItemOutput, error)
}
// Checker checks an endpoint to verify it returns a proverb.
type Checker struct {
httpClient httpClient
ddbClient ddbPutter
}
// NewChecker returns a new Checker.
func NewChecker(hc httpClient, ddb ddbPutter) *Checker {
return &Checker{httpClient: hc, ddbClient: ddb}
}
// Handle handles the request.
func (c *Checker) Handle(ctx context.Context, request *events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
endpoint, ok := request.QueryStringParameters["endpoint"]
if !ok {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusBadRequest,
Body: "Endpoint is missing in query string.",
}, nil
}
name, ok := request.QueryStringParameters["name"]
if !ok {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusBadRequest,
Body: "Name is missing in query string.",
}, nil
}
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: fmt.Sprintf("Failed to formulate request to check endpoint: %s", err),
}, nil
}
res, err := c.httpClient.Do(req)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: fmt.Sprintf("Failed to submit request: %s", err),
}, nil
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: fmt.Sprintf("Failed to read data: %s", err),
}, nil
}
log.Printf("e=%s, n=%s", endpoint, name)
var p proverbs.Proverb
if err := json.Unmarshal(data, &p); err != nil {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: fmt.Sprintf("Failed to unmarshal data: %s", err),
}, nil
}
if p.Saying == "" || p.Link == "" {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusNotAcceptable,
}, nil
}
chk := check{Endpoint: endpoint, Name: name}
if err := c.save(ctx, chk); err != nil {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
Body: fmt.Sprintf("Failed to save check: %s", err),
}, nil
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusAccepted,
}, nil
}
type check struct {
ID string `json:"id"`
Endpoint string
Name string
}
func (c *Checker) save(ctx context.Context, chk check) error {
id, err := uuid.NewRandom()
if err != nil {
return err
}
chk.ID = id.String()
item, err := dynamodbattribute.MarshalMap(chk)
if err != nil {
return fmt.Errorf("failed to marshal item for storage: %s", err)
}
input := &dynamodb.PutItemInput{
Item: item,
TableName: aws.String(os.Getenv("TABLE_NAME")),
}
if _, err = c.ddbClient.PutItemWithContext(ctx, input); err != nil {
return fmt.Errorf("failed to save item: %s", err)
}
return nil
}