-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcore.go
More file actions
65 lines (52 loc) · 1.15 KB
/
core.go
File metadata and controls
65 lines (52 loc) · 1.15 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
package lambda
// #include <stdlib.h>
import "C"
import (
"encoding/json"
"github.com/manetu/lambda-sdk-go/internal"
"unsafe"
)
type Headers map[string]string
type Params map[string]any
type Request struct {
Headers Headers `json:"headers"`
PathInfo string `json:"path-info"`
Params Params `json:"params"`
}
type Response struct {
Status int `json:"status"`
Headers Headers `json:"headers"`
Body any `json:"body"`
}
type Lambda interface {
Handler(Request) Response
}
type context struct {
lambda Lambda
}
func (c context) HandleRequest(request string) string {
var req Request
err := json.Unmarshal([]byte(request), &req)
if err != nil {
v, err := json.Marshal(&Response{Status: 500})
if err != nil {
panic(err)
}
return string(v)
}
resp := c.lambda.Handler(req)
v, err := json.Marshal(resp)
if err != nil {
panic(err)
}
return string(v)
}
func (c context) Malloc(len uint32) uint32 {
return uint32(uintptr(C.malloc(C.ulong(len))))
}
func (c context) Free(ptr uint32) {
C.free(unsafe.Pointer(uintptr(ptr)))
}
func Init(lambda Lambda) {
internal.SetExportsManetuLambda0_0_2_Guest(&context{lambda: lambda})
}