/
main.go
113 lines (102 loc) · 2.61 KB
/
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
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
package main
import (
"bytes"
"context"
"encoding/base64"
"log"
"net/http/httptest"
"github.com/aws/aws-lambda-go/events"
"github.com/jan-xyz/box"
strsvc "github.com/jan-xyz/box/example/strsvc/lambda"
"github.com/jan-xyz/box/example/strsvc/lambda/proto/strsvcv1"
awslambdago "github.com/jan-xyz/box/transports/github.com/aws/aws-lambda-go"
boxhttp "github.com/jan-xyz/box/transports/net/http"
"go.opentelemetry.io/otel"
"google.golang.org/protobuf/proto"
)
func main() {
// setup endpoint with it's middlewares
mw := box.Chain(
box.EndpointLogging[*strsvcv1.Request, *strsvcv1.Response](),
box.EndpointTracing[*strsvcv1.Request, *strsvcv1.Response](otel.GetTracerProvider()),
box.EndpointMetrics[*strsvcv1.Request, *strsvcv1.Response](otel.GetMeterProvider()),
)
ep := mw(strsvc.NewEndpoint())
// connect endpoint to SQS
sqsTransport := awslambdago.NewSQSTransport(
false,
strsvc.DecodeSQS,
ep,
)
// connect endpoint to APIGateway
apiGWTransport := awslambdago.NewAPIGatewayTransport(
strsvc.DecodeAPIGateway,
strsvc.EncodeAPIGateway,
strsvc.EncodeErrorAPIGateway,
ep,
)
apiGWTransport = awslambdago.NewAPIGatewayTracingMiddleware(apiGWTransport, otel.GetTracerProvider())
// connect to HTTP
httpServer := boxhttp.NewHTTPServer(
strsvc.DecodeHTTP,
strsvc.EncodeHTTP,
strsvc.EncodeErrorHTTP,
ep,
)
// test input
requests := []*strsvcv1.Request{
{
Message: &strsvcv1.Request_UpperCase{
UpperCase: &strsvcv1.UpperCase{
Input: "Foo",
},
},
},
{
Message: &strsvcv1.Request_LowerCase{
LowerCase: &strsvcv1.LowerCase{
Input: "Bar",
},
},
},
{
Message: &strsvcv1.Request_LowerCase{
LowerCase: &strsvcv1.LowerCase{
Input: "",
},
},
},
}
// simular incoming events via lambda.Start
for _, m := range requests {
marshalledM, err := proto.Marshal(m)
if err != nil {
panic(err)
}
body := base64.StdEncoding.EncodeToString(marshalledM)
// simulate SQS invocation
sqsResp, _ := sqsTransport(
context.Background(),
&events.SQSEvent{Records: []events.SQSMessage{
{Body: body, MessageId: "the message"},
}},
)
log.Printf("sqs: %#v", sqsResp)
// simulate APIGateway invocation
apiGWResp, err := apiGWTransport(
context.Background(),
&events.APIGatewayProxyRequest{Body: body},
)
if err != nil {
panic(err)
}
log.Printf("api gw: %#v", apiGWResp)
// simulate HTTP invocation
srv := httptest.NewServer(httpServer)
httpResp, err := srv.Client().Post(srv.URL, "application/octetstream", bytes.NewReader(marshalledM))
if err != nil {
panic(err)
}
log.Printf("http: %#v", httpResp)
}
}