forked from LeanerCloud/AutoSpotting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautospotting.go
66 lines (51 loc) · 1.44 KB
/
autospotting.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
// Copyright (c) 2016-2019 Cristian Măgherușan-Stanciu
// Licensed under the Open Software License version 3.0
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
autospotting "github.com/AutoSpotting/AutoSpotting/core"
"github.com/aws/aws-lambda-go/lambda"
)
var as *autospotting.AutoSpotting
var conf autospotting.Config
// Version represents the build version being used
var Version = "number missing"
var eventFile string
func main() {
eventFile = conf.EventFile
if os.Getenv("AWS_LAMBDA_FUNCTION_NAME") != "" {
lambda.Start(Handler)
} else if eventFile != "" {
parseEvent, err := ioutil.ReadFile(eventFile)
if err != nil {
log.Fatal(err)
}
Handler(context.TODO(), parseEvent)
} else {
eventHandler(nil)
}
}
func eventHandler(event *json.RawMessage) {
log.Println("Starting autospotting agent, build", Version)
log.Printf("Configuration flags: %#v", conf)
as.EventHandler(event)
log.Println("Execution completed, nothing left to do")
}
// this is the equivalent of a main for when running from Lambda, but on Lambda
// the runFromCronEvent() is executed within the handler function every time we have an event
func init() {
as = &autospotting.AutoSpotting{}
conf = autospotting.Config{
Version: Version,
}
autospotting.ParseConfig(&conf)
as.Init(&conf)
}
// Handler implements the AWS Lambda handler interface
func Handler(ctx context.Context, rawEvent json.RawMessage) {
eventHandler(&rawEvent)
}