forked from affix/openfaas-sqs-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (64 loc) · 2.07 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
// Copyright (c) Keiran Smith 2019. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"log"
"github.com/architsmat38/golang-aws-sqs/poller"
SqsService "github.com/architsmat38/golang-aws-sqs/sqs"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/openfaas-incubator/connector-sdk/types"
"os"
"strconv"
"time"
)
var (
accessKeyId = os.Getenv("AWS_ACCESS_KEY_ID")
secretKey = os.Getenv("AWS_ACCESS_ACCESS_KEY_ID")
region = os.Getenv("AWS_REGION")
queueName = os.Getenv("AWS_SQS_QUEUE_NAME")
)
func InitializePollerSQS() {
log.Printf("accessKeyId: %s", accessKeyId)
log.Printf("secretKey: %s", secretKey)
log.Printf("region: %s", region)
log.Printf("queueName: %s", queueName)
printResponse, _ := strconv.ParseBool(os.Getenv("PRINT_RESPONSE"))
printResponseBody, _ := strconv.ParseBool(os.Getenv("PRINT_RESPONSE_BODY"))
creds := types.GetCredentials()
config := &types.ControllerConfig{
RebuildInterval: time.Millisecond * 1000,
GatewayURL: os.Getenv("GATEWAY_URL"),
PrintResponse: printResponse,
PrintResponseBody: printResponseBody,
}
controller := types.NewController(creds, config)
receiver := ResponseReceiver{}
controller.Subscribe(&receiver)
controller.BeginMapBuilder()
go poller.Start(poller.HandlerFunc(func(msg *sqs.Message) error {
var queueMessage = aws.StringValue(msg.Body)
decoded, err := SqsService.Decode([]byte(queueMessage))
if err != nil {
return err
}
controller.Invoke(queueName, &decoded)
return nil
}))
}
func main() {
SqsService.Initialize(
SqsService.New(queueName, region, accessKeyId, secretKey, ""),
SqsService.SetWaitSeconds(20),
)
InitializePollerSQS()
}
type ResponseReceiver struct {
}
func (ResponseReceiver) Response(res types.InvokerResponse) {
if res.Error != nil {
log.Printf("tester got error: %s", res.Error.Error())
} else {
log.Printf("tester got result: [%d] %s => %s (%d) bytes", res.Status, res.Topic, res.Function, len(*res.Body))
}
}