-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
58 lines (52 loc) · 1.38 KB
/
run.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
package s32cs
import (
"context"
"encoding/json"
"log"
"os"
"regexp"
"strings"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/session"
)
func Run() {
var reg *regexp.Regexp
if r := os.Getenv("KEY_REGEXP"); r != "" {
reg = regexp.MustCompile(r)
} else {
reg = nil
}
client := NewClient(session.New(), os.Getenv("ENDPOINT"), reg)
handler := func(msg json.RawMessage) (interface{}, error) {
var event SQSEvent
if err := json.Unmarshal(msg, &event); err != nil {
return nil, err
}
if event.QueueURL != "" {
log.Printf("starting process sqs %s", event.QueueURL)
return nil, client.ProcessSQS(event.QueueURL)
}
var s3event S3Event
if err := json.Unmarshal(msg, &s3event); err != nil {
return nil, err
}
log.Println("starting process s3 event:", s3event.String())
if err := client.Process(s3event); err != nil {
return nil, err
}
return true, nil
}
env := os.Getenv("AWS_EXECUTION_ENV")
runtimeAPI := os.Getenv("AWS_LAMBDA_RUNTIME_API")
if strings.HasPrefix(env, "AWS_Lambda") || runtimeAPI != "" {
lambda.Start(handler)
} else if strings.HasPrefix(env, "Test_AWS_Lambda_go") {
wrappedHandler := lambda.NewHandler(handler)
_, err := wrappedHandler.Invoke(context.Background(), []byte("{}"))
if err != nil {
panic(err)
}
} else {
log.Printf("[error] Lambda execution environment %s is not supported", env)
}
}