-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathlambdaServer.go
More file actions
100 lines (82 loc) · 2.59 KB
/
lambdaServer.go
File metadata and controls
100 lines (82 loc) · 2.59 KB
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
97
98
99
100
package server
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/open-lambda/open-lambda/ol/common"
"github.com/open-lambda/open-lambda/ol/worker/lambda"
)
// LambdaServer is a worker server that listens to run lambda requests and forward
// these requests to its sandboxes.
type LambdaServer struct {
lambdaMgr *lambda.LambdaMgr
}
// getURLComponents parses request URL into its "/" delimated components
func getURLComponents(r *http.Request) []string {
path := r.URL.Path
// trim prefix
if strings.HasPrefix(path, "/") {
path = path[1:]
}
// trim trailing "/"
if strings.HasSuffix(path, "/") {
path = path[:len(path)-1]
}
components := strings.Split(path, "/")
return components
}
// RunLambda expects POST requests like this:
//
// curl localhost:8080/run/<lambda-name>
// curl -X POST localhost:8080/run/<lambda-name> -d '{}'
// ...
func (s *LambdaServer) RunLambda(w http.ResponseWriter, r *http.Request) {
t := common.T0("web-request")
defer t.T1()
// TODO re-enable logging once it is configurable
// log.Printf("Received request to %s\n", r.URL.Path)
// components represent run[0]/<name_of_sandbox>[1]/<extra_things>...
// ergo we want [1] for name of sandbox
urlParts := getURLComponents(r)
if len(urlParts) < 2 {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("expected invocation format: /run/<lambda-name>"))
} else {
// components represent run[0]/<name_of_sandbox>[1]/<extra_things>...
// ergo we want [1] for name of sandbox
urlParts := getURLComponents(r)
if len(urlParts) == 2 {
img := urlParts[1]
s.lambdaMgr.Get(img).Invoke(w, r)
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("expected invocation format: /run/<lambda-name>"))
}
}
}
func (s *LambdaServer) Debug(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(s.lambdaMgr.Debug()))
}
func (s *LambdaServer) cleanup() {
s.lambdaMgr.Cleanup()
}
// NewLambdaServer creates a server based on the passed config."
func NewLambdaServer() (*LambdaServer, error) {
log.Printf("Starting new lambda server")
lambdaMgr, err := lambda.NewLambdaMgr()
if err != nil {
return nil, err
}
server := &LambdaServer{
lambdaMgr: lambdaMgr,
}
log.Printf("Setups Handlers")
port := fmt.Sprintf(":%s", common.Conf.Worker_port)
http.HandleFunc(RUN_PATH, server.RunLambda)
http.HandleFunc(DEBUG_PATH, server.Debug)
log.Printf("Execute handler by POSTing to localhost%s%s%s\n", port, RUN_PATH, "<lambda>")
log.Printf("Get status by sending request to localhost%s%s\n", port, STATUS_PATH)
return server, nil
}