forked from mweagle/Sparta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.go
54 lines (46 loc) · 1.27 KB
/
execute.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
package sparta
import (
"fmt"
"github.com/Sirupsen/logrus"
"net/http"
"os"
"path"
"time"
)
// Port used for HTTP proxying communication
const defaultHTTPPort = 9999
// Execute creates an HTTP listener to dispatch execution. Typically
// called via Main() via command line arguments.
func Execute(lambdaAWSInfos []*LambdaAWSInfo, port int, parentProcessPID int, logger *logrus.Logger) error {
validationErr := validateSpartaPreconditions(lambdaAWSInfos, logger)
if validationErr != nil {
return validationErr
}
if port <= 0 {
port = defaultHTTPPort
}
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: NewLambdaHTTPHandler(lambdaAWSInfos, logger),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
logger.WithFields(logrus.Fields{
"ParentPID": parentProcessPID,
}).Info("Signaling parent process")
if 0 != parentProcessPID {
platformKill(parentProcessPID)
}
binaryName := path.Base(os.Args[0])
logger.WithFields(logrus.Fields{
"URL": fmt.Sprintf("http://localhost:%d", port),
}).Info(fmt.Sprintf("Starting %s server", binaryName))
err := server.ListenAndServe()
if err != nil {
logger.WithFields(logrus.Fields{
"Error": err.Error(),
}).Error("Failed to launch server")
return err
}
return nil
}