-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
96 lines (79 loc) · 2.28 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
package main
import (
"context"
"os"
awsLambda "github.com/aws/aws-lambda-go/lambda"
cfg "github.com/kvendingoldo/aws-letsencrypt-lambda/internal/config"
"github.com/kvendingoldo/aws-letsencrypt-lambda/internal/lambda"
"github.com/kvendingoldo/aws-letsencrypt-lambda/internal/types"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetReportCaller(false)
var formatter log.Formatter
if formatterType, ok := os.LookupEnv("FORMATTER_TYPE"); ok {
if formatterType == "JSON" || formatterType == "json" {
formatter = &log.JSONFormatter{PrettyPrint: false}
}
if formatterType == "TEXT" || formatterType == "text" {
formatter = &log.TextFormatter{DisableColors: false}
}
}
if formatter == nil {
formatter = &log.TextFormatter{DisableColors: false}
}
log.SetFormatter(formatter)
var logLevel log.Level
var err error
if ll, ok := os.LookupEnv("LOG_LEVEL"); ok {
logLevel, err = log.ParseLevel(ll)
if err != nil {
logLevel = log.DebugLevel
}
} else {
logLevel = log.DebugLevel
}
log.SetLevel(logLevel)
}
func Handler(ctx context.Context, event types.Event) (types.Response, error) {
log.Infof("Handling lambda for event: %+v", event)
config, err := cfg.New(event)
if err != nil {
return types.Response{Message: "Lambda has been failed"}, err
}
var msg string
err = lambda.Execute(ctx, *config)
if err != nil {
msg = "Lambda has been failed"
} else {
msg = "Lambda has been completed successfully"
}
return types.Response{Message: msg}, err
}
func main() {
log.Info("Starting lambda execution ...")
if mode, ok := os.LookupEnv("MODE"); ok {
if !(mode == "local" || mode == "cloud") {
log.Errorf("Environment variable 'MODE' has unknown value '%v'. Value should be 'local' or 'cloud'", mode)
os.Exit(1)
}
if mode == "local" {
config, err := cfg.New(nil)
if err != nil {
log.Errorf("Lambda has been failed. Error: %s", err)
os.Exit(1)
}
err = lambda.Execute(context.TODO(), *config)
if err != nil {
log.Errorf("Lambda has been failed. Error: %s", err)
os.Exit(1)
}
log.Info("Lambda has been completed")
} else if mode == "cloud" {
awsLambda.Start(Handler)
}
} else {
log.Errorf("Environment variable 'MODE' is unspecified. Please, specify it. Value should be 'local' or 'cloud'")
os.Exit(1)
}
}