This repository has been archived by the owner on Apr 3, 2020. It is now read-only.
/
main.go
86 lines (75 loc) · 3.51 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
// Copyright 2019 Form3 Financial Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/openfaas-incubator/connector-sdk/types"
log "github.com/sirupsen/logrus"
"github.com/form3tech-oss/openfaas-sqs-connector/internal/processors"
)
func main() {
// Parse command-line flags.
endpoint := flag.String("endpoint", "", "the aws sqs endpoint url to use")
logLevel := flag.String("log-level", "info", "the log level to use")
maxNumberOfMessages := flag.Int64("max-number-of-messages", 1, "the maximum number of messages to return from the aws sqs queue per iteration")
maxWaitTime := flag.Int64("max-wait-time", 1, "the maximum amount of time (in seconds) to wait for messages to be returned from the aws sqs queue per iteration")
openfaasGatewayURL := flag.String("openfaas-gateway-url", "http://gateway.openfaas.svc:8080", "the url at which the openfaas gateway can be reached")
queueURL := flag.String("queue-url", "", "the name of the aws sqs queue to pop messages from")
region := flag.String("region", "", "the aws region to which the aws sqs queue belongs")
topicRefreshInterval := flag.Int64("topic-refresh-interval", 15, "the interval (in seconds) at which to refresh the topic map")
visibilityTimeout := flag.Int64("visibility-timeout", 30, "the amount of time (in seconds) during which received messages are unavailable to other consumers")
flag.Parse()
// Log at the requested level.
if v, err := log.ParseLevel(*logLevel); err != nil {
log.Fatalf("Failed to parse log level: %v", err)
} else {
log.SetLevel(v)
}
// Make sure that all required flags have been provided.
if *region == "" {
log.Fatal("--region must be provided")
}
if *queueURL == "" {
log.Fatal("--queue-url must be provided")
}
if *openfaasGatewayURL == "" {
log.Fatal("--openfaas-gateway-url must be provided")
}
// Initialize the controller.
controller := types.NewController(types.GetCredentials(), &types.ControllerConfig{
GatewayURL: *openfaasGatewayURL,
PrintResponse: log.IsLevelEnabled(log.DebugLevel),
PrintResponseBody: log.IsLevelEnabled(log.DebugLevel),
RebuildInterval: time.Duration(*topicRefreshInterval) * time.Second,
})
controller.BeginMapBuilder()
// Initialize the AWS SQS client.
awsSession, err := session.NewSession(&aws.Config{
Endpoint: endpoint,
CredentialsChainVerboseErrors: aws.Bool(log.IsLevelEnabled(log.DebugLevel)),
Region: region,
})
if err != nil {
log.Fatalf("Failed to initialize AWS session: %v", err)
}
awsSQSClient := sqs.New(awsSession)
// Initialize the response processor.
controller.Subscribe(processors.NewResponseProcessor(awsSQSClient, *queueURL))
// Initialize the message processor and start processing messages off the AWS SQS queue.
processors.NewMessageProcessor(awsSQSClient, *queueURL, *maxNumberOfMessages, *maxWaitTime, *visibilityTimeout, controller).Run()
}